I am reading data sent to a serial port (that is, COM3) using code like this:
_serialPort.PortName = "COM3";
_serialPort.BaudRate = 9600;
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
_serialPort.Open();
The data coming from that serial port are commands and can be a string of any length ending in '\0'. I'm looking for best practices on how to efficiently read such data. I'm thinking a producer/consumer pattern might be best, where:
- Every time the DataReceived event fires, I read the string from the port and append to a stringbuffer or some other structure
- I then have another thread that constantly inspects the stringbuffer or structure for char '\0' and if found, parses the and remove the command string.
Would this be the most efficient way to handle this? Is there any sort of blocking serial read function that doesn't rely on the dataReceived event?