Hi, I am using visual studio to get data packet from xbee coordinator.I am using delay to give more time serial port to receive data, to avoid it receive incomplete data, but however, sometimes it is still receive incomplete packet information. My coding as below:
Thread.Sleep(sec); // give time for serial port to read all data.
int numBytes = serialPort1.BytesToRead;
GlobalVar.ATAR_R = new byte[numBytes];
serialPort1.Read(GlobalVar.ATAR_R, 0, GlobalVar.ATAR_R.Length);
anyone know how to avoid receiving incomplete data?
No, the radio does not offer an IO line for that function. Think of the radio as nothing more than a serial cable with a buffer. How would you get data from a Serial cable?
Try this, Hold off RTS flow control.
Assert RTS flow control and read the serial port.
If no data, De-assert RTS flow control. If there is data, continue reading serial port till their is no more data to read. Then De-assert RTS flow control.
You can put this in a while loop so your code can do other things while you keep checking the port.