experimenting wit the rs232 module from GHI i only get zeros sending three bytes to it reading the port like;
void serialPort_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
{
HostorClient = 1;
// Create a buffer that is the length of the data that is available
// the BytesToRead variable tells us how many bytes are waiting in a buffer.
byte[] readData = new byte[rs232.serialPort.BytesToRead];
// Now we read the data into the buffer we just created, starting at the
// first byte.
// Use the data that you just read however you need to
if (Hconnected)
{
rs232.serialPort.Read(readData, 0, readData.Length);
if (readData.Length> 0)
{
serialUSBHost.Write(readData, 0, readData.Length);
serialUSBHost.Flush();
cdcwrite1(readData, readData.Length, HostorClient);
}
}
but using this as a thread with a while loop functions well.
wath is wrong with the serialdatareceived void.
}
For a start, flush() is a very bad thing to put in your code. If you think you need flush(), you’re wrong… Read all the data available in the code block, put it in your external queue, and then exit the handler.
Make your experimenting code simpler. Get rid of CDC and get rid of Hconnected test. Read Just the data that is there, debug.print it, and exit the handler and see if that works.
One other thing I would not do that you have done, is use the global serial port object; you probably should use “sender” as it will always be correct for what port you’re meant to use.
I also notice that you’re using Gadgeteer interface, which might change things (they override some of the netmf methods). Might be worth seeing if your code works differently when just using netmf.
Yes, ok, Flush() has it’s uses but certainly not in DataRecieved events - flush() is useful when you’re sending data not receiving data. Often though you might need to be considering DiscardInBuffer or DiscardOutBuffer.