C# – Serial port communication: polling serial port vs using serial port DataReceived event

cmultithreadingserial-port

I am just reviewing some code I wrote to communicate with the serial port in C# on CF2.0.
I am not using DataReceived event since it's not reliable. MSDN states that:

The DataReceived event is not
gauranteed to be raised for every byte
received. Use the BytesToRead property
to determine how much data is left to
be read in the buffer.

I poll the port with read() and have a delegate that processes the data when it is read. I also read somewhere that "polling is bad"(no explanation given).

Any ideas why polling might be bad? aside from the usual threading cautions – I have a separate thread (background thread) that polls the port, the thread is exited after the data is read, all tested and works well.

Best Answer

The way I read that, you might get one event for multiple bytes, rather than one event per byte. I would still expect to get an event when data is ready, and not have it "skip" some bytes entirely.

I've always used this event, and have not had any trouble with it.

Related Topic