Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My issue is the SerialPort DataReceived event appears delayed advanced or simply out of order.

I have tested this by sending data on 2 serial ports and receiving on 2 serial ports and the DataReceived events fire in the wrong order.

The problem is in my application one serial ports tells me when to expect data on the other serial port and thus if it is in the wrong order the data on the second port is ignored due to business rules.

The application is reading data from a number of hardware devices which are programmed to send their data at a certain point/time which they all do correctly. I have to log the data and store the data from each device in one record, if the data isn't there at the correct time it must be ignored for data integrity, as the system bills customers on this data.

As I understand the fact that the SerialPort component uses threads from the thread pool to wait for the events I have no control over when the events fire. i.e. port1 receives characters and then port 2 receives characters shortly after. So the port1 thread looses control to port2 thread just before the data received event would fire for port1 and then while port2 thread has control it receives the data received event and processes the data then looses control to port1 thread which then receives the data received event and processes the data. These are now out of order.

This is not the only reason they fire out of order in my application as there are many threads doing many things and if my SQL Server save thread (background worker) is running it affects the serial port threads as well to the extent I can process a number of port1 data received event before processing the port2 received data events because

while bytes to read
read byte
process byte

will read all the characters regardless of the number of separate packets were received and even if I code to return from the first packet the likely hood is that as there is a second thread already running for the second packet it will fire as soon as I return.

So guys how do I fix this problem?
Posted

1 solution

You can't affect the order in which threads will be processed (except by altering thread priority, but that won't allow the physical data receive times to control which thread is running at any specific time)
If you want to process data strictly in the order ti is received, then you need a real time operating system, and Windows is not even close to that!

You can do it - just about - by writing your own specific device drivers for the serial ports you are using and handling it there, since the driver is interrupt driven (a hardware event) instead of thread based (a round-robin software scheme) - but that's some serious work and not something to approach in C#!

What are you trying to do that you need this?
 
Share this answer
 
Comments
Member 11745687 6-Jun-15 8:45am    
Tracking, weighing, dimensioning boxes, scanning barcodes, sorting on a conveyor at a speed of 2.5 metres per second. The problem arises when the speed goes over 1.2 metres per second.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900