Only one byte is read when reading just now opened SerialPort

后端 未结 3 642
傲寒
傲寒 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:32

    I've found my solution I hpe this helps:

    ireadCount = serialPort.Read(readBuffer, 0, serialPort.ReadBufferSize);
    Thread.Sleep(1);//This can be removed, just use it in case need time to complete the reception.
    ireadCount = serialPort.Read(readBuffer, 1, serialPort.ReadBufferSize-1);
    ireadCount +=1;
    

    This way I can have the whole response from the external device without losing the first byte.

    Regards.-

提交回复
热议问题