serial ports are tricky, and the best approach depends very much on what exactly it is you need.
A few facts that may help you find the best way for your situation:
1. the DataReceived event isn't very reliable, what I mean is for N bytes coming in on the serial port it may fire an arbitrary number of times, anywhere from 1 to N.
2. Asynchronous event handlers get executed on threads from the ThreadPool, not on the main (or "UI") thread, so you cannot directly manipulate your Form's Controls.
3. We all hope only one DatReceived event will be active at any one time, although I have never seen that being promised...
4. incoming data is buffered several times before it reaches your C# code (hardware in the serial port, buffers in the driver, etc).
5. when asking for a delay (e.g. by using Thread.Sleep) what you get will not exactly be what you asked for, the system clock may have limited resolution (e.g. 15 msec) and the system may not immediately put your thread back in charge.
Here is one concept that has worked for me on several occasions:
if you are sure all data will arrive in a period of M milliseconds once the first byte is sent, then have a very simple DataReceived handler that starts by sleeping slightly more than M milliseconds (so all the message data is guaranteed to have gotten time to be received), and then perform a ReadExisting or a Read(chararray) when working with text, or a Read(bytearray) when not working with text.
The one advantage of it is simplicity. However there are a few drawbacks to this approach:
1. there is artificial latency, i.e. nothing happens while the handler is sleeping, e.g. you don't see the data slowly coming in.
2. you are not immediately ready to get the next burst of data, as your sleep period must be slightly larger than your worst-case time required to get one message, and then you still need to process the data.
3. Your sleep period depends on your port settings (baud rate, number of stop bits, etc).
If the above isn't suitable, I tend to go to the other extreme:
1. consider it binary data (even when it is text) so you don't get confused by newline handling, multibyte Unicode characters and the like.
2. set up a mechanism that reads into a byte array; you can do this in the DataReceived handler; I don't, I prefer to do it periodically (e.g. in a BackGroundWorker that uses a loop
with a Thread.Sleep in there).
3. You can set a ReadTimeout to the serialport to keep the loop spinning, and to make an abort or application exit slightly easier.
4. Make sure to use the return value of the Read(bytearray) method, indicating the number of bytes actually read. Append the newly received bytes to a large buffer holding all bytes not yet processed.
5. Do whatever it takes to detect message boundaries in that larger buffer, then process and remove the messages when appropriate.
Luc Pattyn [My Articles]
If you can't find it on YouTube try TikTok...
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.