C#.Net 中的任何事件告诉我串行端口 dataReceived 事件停止接收数据
我正在用 C# 开发串行端口实现。 我正在从 SerialPort 获取数据,并将该数据添加到集合的队列类型(全局声明)中。 我正在使用BackgroundWorker线程来处理队列中的数据。 问题是这样的。 如果串行端口停止发送数据和接收数据停止接收数据,我的队列中仍有一些数据需要处理。 有什么方法可以告诉我没有来自 SerialPort 的数据,或者换句话说 Port_dataReceived 停止接收任何数据。 这样我就可以处理队列中的任何数据。 因为这次我将确定现在不会有任何数据来自串行端口,并且队列中的数据都是最后要处理的剩余数据。 提前致谢 - 切坦
I am Working on a Serial Port implementation in C#.
I am Getting data from SerialPort and I am adding that data to Queue type of Collection (Globally Declared). I am using BackgroundWorker thread to process data in Queue. The Problem is that. If Serial Port Stop Sending data and data Received stop Receiving data There will be still some data remain in my Queue which required processing. Is there any way Which tell me there is no data coming from SerialPort or in other words Port_dataReceived is stopped receiving any data. So that I can process whatever data is there in Queue. Because this time i will be sure that there will not be any data coming from Serial Port now and whatever the data in the queue is the last remaining data to be processed.
Thanks in Advance
- Chetan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是 System.IO.Ports.SerialPort,那么我不相信有内置的东西。您需要自己编写代码。
您是否有理由不能仅使用 DataReceived 事件并处理传入的数据?
这很棘手,因为您必须处理 SerialPort 侦听器在不同线程上运行的事实,因此您可能需要担心线程安全,但您可以设置一个全局 DateTime 变量并在 DataReceived 事件中更新它,然后检查在代码中查看上次接收数据的时间是否超过n秒。
If you're using the System.IO.Ports.SerialPort, then I don't believe there is something built in. You'd need to code it yourself.
Is there a reason you can't just use the DataReceived event and process the data as it comes in?
This is tricky because you have to deal with the fact that the SerialPort listener operates on a different thread, so you may need to worry about thread safety, but you could set a global DateTime variable and update it in the DataReceived event, and then check that in your code to see if the last time data has been received has been more than n seconds.