USB controler programming help (Multi Threading Error)

Supporting Visual Studio on Windows
patchchristensen

USB controler programming help (Multi Threading Error)

Postby patchchristensen » Tue Aug 23, 2011 6:01 pm

Has anyone been able to get a USB controller to work with a Phidget board? I keep getting multi-threading errors when attempting to take the controller signal and act on the phidget board.

psuedocode:

press up on controller
event handler - controller signal sent
get signal = "up" (numerical value for "up" from USB device)
when signal "up" received
send signal "high" to digital input 1

[It is at this point the multi-threading error occurs. I can't use the USB device and the Phidget device at the same time while sharing signals back and forth.]

If someone could point me in the right direction for fixing this issue, I would be much appreciative.

Thanks.

User avatar
Patrick
Lead Developer
Posts: 3155
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: USB controler programming help (Multi Threading Error)

Postby Patrick » Wed Aug 24, 2011 9:04 am

what do you mean by a 'usb controller'?

-Patrick

patchchristensen

Re: USB controler programming help (Multi Threading Error)

Postby patchchristensen » Wed Aug 24, 2011 12:20 pm

Like a game pad or joy stick.

Thanks for the help!!!

User avatar
Patrick
Lead Developer
Posts: 3155
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: USB controler programming help (Multi Threading Error)

Postby Patrick » Wed Aug 24, 2011 2:41 pm

Do you have some actual code we can look at? How are you interfacing with the USB controller?

-Patrick

patchchristensen

Re: USB controler programming help (Multi Threading Error)

Postby patchchristensen » Wed Aug 24, 2011 9:10 pm

I will post it tomorrow. I don't have access to it right at this moment.
Thanks for the help.

patchchristensen

Re: USB controler programming help (Multi Threading Error)

Postby patchchristensen » Thu Aug 25, 2011 6:24 pm

The code is a mess from attempting to solve this threading issue so I apologize. I don't think it is going to give you any help. My next thought is that I need to separate the USB gamepad program from the phidgets form/interface and use a .txt or database to share data between the two. As you can see, I'm fairly new to programming.


public void startRead()
{

sendStartCMD();
this.usbint.enableUsbBufferEvent(new System.EventHandler(myEventCacher));
Application.DoEvents();
Thread.Sleep(5);
usbint.startRead();
Thread.Sleep(5);
for (int i = 0; i < 200; i++)
{
Application.DoEvents();
//Assert.IsNotNull(USBHIDDRIVER.USBInterface.usbBuffer);
Thread.Sleep(2);
}
usbint.stopRead();
sendStopCMD();
}

public void userDefinedeventHandling()
{
sendStartCMD();

usbint.enableUsbBufferEvent(new System.EventHandler(myEventCacher));
usbint.startRead();

//wait a little bit
for (int i = 0; i < 100; i++)
{
Application.DoEvents();
Thread.Sleep(4);
}

sendStopCMD();
}

public void sendStopCMD()
{
byte[] stopCMD = new byte[75];
//Stop
stopCMD[0] = 128;

stopCMD[64] = 8;

usbint.write(stopCMD);
}


public void myEventCacher(object sender, System.EventArgs e)
{
Console.Out.WriteLine("Event caught");
if (USBHIDDRIVER.USBInterface.usbBuffer.Count > 0)
{
byte[] currentRecord = null;
int counter = 0;
while ((byte[])USBHIDDRIVER.USBInterface.usbBuffer[counter] == null)
{
//Remove this report from list
lock (USBHIDDRIVER.USBInterface.usbBuffer.SyncRoot)
{
USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0);
}
}
//since the remove statement at the end of the loop take the first element
currentRecord = (byte[])USBHIDDRIVER.USBInterface.usbBuffer[0];
lock (USBHIDDRIVER.USBInterface.usbBuffer.SyncRoot)
{
USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0);
}

//MessageBox.Show(Convert2HStr(currentRecord, (short)currentRecord.Length));
//this.SetText(Convert2HStr(currentRecord, (short)currentRecord.Length));
//TODO:
lock (test2)
{
test2 = Convert2HStr(currentRecord, (short)currentRecord.Length);
ConvertUSBToCommand(test2);
}

lock (textBox1)
{
MessageBox.Show(test2);
}
//test1 = Convert2HStr(currentRecord, (short)currentRecord.Length);
//= ConvertUSBToCommand(Convert2HStr(currentRecord, (short)currentRecord.Length));
//counter = 0;
}
//ConvertUSBToCommand(test2);
}

// This method demonstrates a pattern for making thread-safe
// calls on a Windows Forms control.
//
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// SetTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly.




//static variable to hold current position
//lock variable update and get value
//adjust value and update text box
//change text box event handler to trigger on text change.






public void ConvertUSBToCommand(string USBHexRecord)
{
//Int16 myvalue = 0;

lock (target_positionTxt)
{
MessageBox.Show(target_positionTxt.Text);
myvalue = Convert.ToInt16(target_positionTxt.Text);
}

//Update target text

switch (USBHexRecord)
{
case "01807F7F80000000":
//myvalue = myvalue + 1;
MessageBox.Show(myvalue.ToString());


UpdatePosition(1);
break;
case "01807F7F80040000":
MessageBox.Show("Down");
//test1 = "Down";

UpdatePosition(-1);
break;
}

//return USBcommand;
}

private void UpdatePosition(Int16 Adjustment)
{

double currentposition;//= Convert.ToDouble(advServo.servos[(int)servoCmb.SelectedItem].Position);
double newposition;//= Convert.ToInt16(currentposition) + Convert.ToInt16(Adjustment);

currentposition = Convert.ToDouble(advServo.servos[(int)servoCmb.SelectedItem].Position);
newposition = Convert.ToInt16(currentposition) + Convert.ToInt16(Adjustment);
advServo.servos[(int)servoCmb.SelectedItem].Position = newposition;
positionTrk.Value = Convert.ToInt16(newposition) * 100;
}

private string Convert2HStr( byte[] sBin, short sNo )
{
short i;
short j = 0;
String dStr = null;

for( i = 0 ; i < sNo ; i++, j++ )
{
dStr += String.Format( "{0:X2}", sBin[ i ] );
}

return dStr;
}


private void sendStartCMD()
{
byte[] startCMD = new byte[8];
//Start
startCMD[0] = 255;
//Mode
startCMD[1] = 0;
//USync
startCMD[2] = 28;
//ULine
startCMD[3] = 20;
//tSync
startCMD[4] = 20;
//tRepeat - High
startCMD[5] = 0;
//tRepeat - Low
startCMD[6] = 0x01;
//BusMode
startCMD[7] = 0xF4;
//send the command

usbint.Connect();
usbint.write(startCMD);
}

private void target_positionTxt_TextChanged(object sender, EventArgs e)
{
try
{
if (IsNumeric(target_positionTxt.Text.Trim()))
{
advServo.servos[(int)servoCmb.SelectedItem].Position = Convert.ToInt16(target_positionTxt.Text);
positionTrk.Value = Convert.ToInt16(target_positionTxt.Text) * 100;
}
}
catch (PhidgetException ex)
{
MessageBox.Show(ex.Description);
}
}



}


Return to “C#.NET”

Who is online

Users browsing this forum: No registered users and 2 guests