Only one byte is read when reading just now opened SerialPort

后端 未结 3 704
傲寒
傲寒 2021-01-22 10:30

Strange issue.

When I read from com-port with SerialPort.Read(), then if data arrive, only one byte is read on the first call, disregards of count

3条回答
  •  甜味超标
    2021-01-22 11:09

    There are several ways to handle this problem. If you are expecting a reply from a command you send you should sleep for at least 250 msec after calling the serialPort.Write() Before calling serilPort.Read(). Or if you know how many bytes your device is sending back to you can use the serialPort.BytesToRead property to delay reading until the number of bytes you expect are available. If you are using the DataReceived Event you can set the serialPort.ReceivedBytesThreshold (default is 1) to prevent the event from being raised until the expected number of bytes are available. Keep in mind that the serialPort.Read method is not guaranteed to return the number of bytes requested. That is why the serialPort.Read method returns an integer which is the actual number of bytes read.

提交回复
热议问题