如何确保在不同端口上接收的串行端口数据以正确的顺序触发每个端口的数据接收事件? [英] How do I ensure serial port data received on different ports fire the data received events for each port in the correct order?

查看:104
本文介绍了如何确保在不同端口上接收的串行端口数据以正确的顺序触发每个端口的数据接收事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是SerialPort DataReceived事件出现延迟提前或简单无序。



我通过在2个串口上发送数据并在2上接收测试了这个串口和DataReceived事件以错误的顺序触发。



问题出在我的应用程序中,一个串口告诉我何时期望其他串口上的数据因此如果它的顺序错误,则由于业务规则而忽略第二个端口上的数据。



应用程序正在从许多编程的硬件设备读取数据在他们都正确执行的特定时间点发送他们的数据。我必须记录数据并将来自每个设备的数据存储在一个记录中,如果数据不在正确的时间,则必须忽略数据完整性,因为系统会向客户收取此数据的费用。



据我所知,SerialPort组件使用线程池中的线程来等待事件,我无法控制事件何时触发。即port1接收字符,然后端口2接收字符后不久。所以port1线程在数据接收事件触发port1之前就失去了对port2线程的控制,然后当port2线程有控制它接收数据接收事件并处理数据然后失去对port1线程的控制然后接收数据接收事件和处理数据。这些现在已经无序了。



这不是他们在我的应用程序中乱序乱序的唯一原因,因为有很多线程正在做很多事情,如果我的SQL Server保存线程(后台工作器)正在运行它影响串口线程以及我可以在处理port2接收数据事件之前处理多个port1数据接收事件的程度因为



同时读取字节

读取字节

进程字节



将读取所有字符而不管收到单独数据包的数量,即使我编码从第一个数据包返回,可能的情况是因为第二个数据包已经运行第二个数据包,它会在我返回时立即触发。



所以我们如何解决这个问题呢?

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?

推荐答案

你不能影响处理线程的顺序(除了通过改变线程优先级,但那将不允许物理数据接收时间来控制在任何特定时间运行的线程。

如果您想严格按照接收的顺序处理数据,那么您需要一个实时操作系统,Windows甚至都不接近!



您可以通过为您正在使用和处理的串行端口编写自己的特定设备驱动程序来实现它。它在那里,因为驱动程序是中断驱动(硬件事件)而不是基于线程(循环软件方案) - 但这是一些严肃的工作,而不是在C#中接近的东西!



你想要做什么,你需要这个?
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?


这篇关于如何确保在不同端口上接收的串行端口数据以正确的顺序触发每个端口的数据接收事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆