Polling multiple inputs
Up to .NET C#
I'm continuing to have trouble polling my analogins. I need to loop through the ports to keep an eye on what's going on on my sensors (shaft encoder, buttons etc). But I'm having threading issues and port issues.
Is there a thread safe/synchronous way to poll multiple ports? The example projects use only one port, and the documentation is somewhat light on C#
I've included my code for reference. I have a pot hooked up to analogin 0, a pushbutton on analogin 1, and the standard trimpot on 7
///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using MakingThings;
using System.Windows.Forms;
namespace MakeControler_Test
{
public partial class MakeTest : Form
{
public Int32 SPEEDVALUE;
public Int32 SPEEDLOOP;
public Int32 PUSHCOUNTER;
public bool WASOFF;
public MakeTest()
{
InitializeComponent();
usbPacket = new UsbPacket();
usbPacket.Open();
osc = new Osc(usbPacket);
//set address handlers for incoming objects from sensors
osc.SetAddressHandler("/analogin/7/value", TrimPot);
osc.SetAddressHandler("/analogin/0/value", SpeedPot);
osc.SetAddressHandler("/analogin/1/value", PushButton);
SPEEDLOOP = 0;
PUSHCOUNTER = 0;
PUSHCOUNT.Text = Convert.ToString(PUSHCOUNTER);
}
//private UsbPacket usbPacket;
//private Osc osc;
delegate void IndicatorCallback(int value);
public static UsbPacket usbPacket = new UsbPacket();
public Osc osc = new Osc(usbPacket);
void TrimPot(OscMessage oscM)
{
int value = (int)oscM.Values[0];
TrimIndicator(value);
}
public void TrimIndicator(int value)
{
if (trimvalue.InvokeRequired)
{
IndicatorCallback d = new IndicatorCallback(TrimIndicator);
this.Invoke(d, new object[] { value });
}
else
{
trimvalue.Value = value;
}
}
void SpeedPot(OscMessage oscM)
{
int value = (int)oscM.Values[0];
SpeedIndicator(value);
}
public void SpeedIndicator(int value)
{
if (potvalue.InvokeRequired)
{
IndicatorCallback d = new IndicatorCallback(SpeedIndicator);
this.Invoke(d, new object[] { value });
}
else
{
potvalue.Value = value;
SPEEDVALUE = Convert.ToInt32((value+1) / 32);
speedlabel.Text = Convert.ToString(SPEEDVALUE);
}
}
void PushButton(OscMessage oscM)
{
int value = (int)oscM.Values[0];
PushIndicator(value);
}
public void PushIndicator(int value)
{
if (potvalue.InvokeRequired)
{
IndicatorCallback d = new IndicatorCallback(PushIndicator);
this.Invoke(d, new object[] { value });
}
else
{
if (value 10)
{
motor1.BackColor = Color.Red;
if (WASOFF == true)
{
PUSHCOUNTER = PUSHCOUNTER + 1;
PUSHCOUNT.Text = Convert.ToString(PUSHCOUNTER);
}
WASOFF = false;
}
else
{
motor1.BackColor = Color.White;
WASOFF = true;
}
}
}
}
}
What's the problem you're having exactly? Can you describe what you're expecting to happen, and what actually happens?
The best way to poll multiple analogin channels is by sending the /analogin/*/value request, which will send back a bundle with all the analogin values, then your message handlers will get called with the appropriate values.
Polling all inputs at once.... Why didn't i think of that????
I guess i didn't specify the problem well enough.
I'm getting a severe latency and eventually the port closes. It will run for about 5 seconds with latency and then just stop responding.
I suppose my biggest question is whether i'm handling threading correctly in the above code.
Sending a wildcard should tighten up my loop a bit and hopefully reduce the lag, !but the hanging and port closing issues are still giving me a headache.
Totally appreciate the help
Fixed issue
Switching to ethernet solved all my problems. Apparently something's screwy with the USB code or the C# asynchronous protocols
Powered by
Ploneboard

