| Asynchronous message receiver This program waits for messages enclosed in STX and ETX bytes and outputs the text coming between them. Everything else is discarded. On write buffer overflow it stops. On other errors it reset input and output buffers and waits for further messages. The code illustrates: Asynchronous serial port reading (see SerialPort.DataReceived usage). Serial port error processing (see SerialPort.ErrorReceived usage). Non-text message-based protocol implementation.
- Partial message reading.
The SerialPort.DataReceived event may happen earlier than entire message (up to ETX) comes. The entire message may also not be available in the input buffer (SerialPort.Read(..., ..., port.BytesToRead) reads only a part of the message). In this case we stash the received part (unprocessedBuffer) and carry on waiting for further data. The SerialPort.DataReceived event may happen only after several messages have been sent by the other end.
|