Hello, I’m new to this forum. I’m working with a FEZ Panda, my application must receive data from xbee modules through a COM port and then must send data to a PC through another COM port.
I tried separately communication and working properly, the problem is when I want to use the two COM ports, using a first the other does not respond and vice versa. someone could provide a code example to use both ports at the same time?
Or have any suggestions of what could be the problem?
First thing to try is to Open the port first and only after that subscribe to the events. There was a known issue on some of the ports - the order is important.
Also remember that there is only one tread doing the DataReceived event handling. For example if FTDI_DataReceivedHandler gets called, DataReceivedHandler is not going to get called until FTDI_DataReceivedHandler exits. Specifically, the Thread.Sleep(100) in one method essentially blocks processing of both serial ports for 100ms. If you are going to use the DataReceived event handler, quickly and efficiently get the data from the port and then get out. Process it on the main thread or different thread.
For multiple serial ports on a Panda II (and Cobra II) I had the best luck having a dedicated thread getting the data from each. Looping on a port.Read method:
while (true)
{
int read = port.Read(byteBuffer, 0, byteBuffer.Length);
if (read > 0)
{
// process byteBuffer
Debug.Print(Encoding.UTF8.GetChars(byteBuffer,0,read));
}
}
The best way to use the data received event handlers is just to de-queue data, not to process it. I would suggest you spend time to move the handling of the data to a separate action that you trigger.
You haven’t really articulated what doesn’t work. Is it just that you can’t keep up at the data rates you’re seeing? What is the data rate you’re trying to push from both ends?