Constantly reading from a serial port with a background thread(使用后台线程不断从串口读取)
问题描述
由于串行端口通信是异步的,我很早就发现我的项目涉及与 RS 232 设备的通信,我必须有一个后台线程不断读取端口以接收数据.现在,我正在使用 IronPython (.NET 4.0),因此我可以访问 .NET 中内置的光滑 SerialPort 类.这让我可以编写这样的代码:
As serial port communication is asynchronous, I figured out early on into my project involving communication with a RS 232 device that I will have to have a background thread constantly reading the port for data received. Now, I'm using IronPython (.NET 4.0) so I have access to the slick SerialPort class built into .NET. This lets me write code like this:
self.port = System.IO.Ports.SerialPort('COM1', 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One)
self.port.Open()
reading = self.port.ReadExisting() #grabs all bytes currently sitting in the input buffer
足够简单.但正如我提到的,我希望在新数据到达时不断检查该端口是否有新数据.理想情况下,只要有数据等待,我就会让操作系统告诉我.Whaddaya 知道,我的祈祷得到了回应,提供了一个 DataReceived
事件!
Simple enough. But as I mentioned I want to be constantly checking this port for new data as it arrives. Ideally, I would have the OS tell me anytime there's data waiting. Whaddaya know, my prayers have been answered, there is a DataReceived
event provided!
self.port.DataReceived += self.OnDataReceived
def OnDataReceived(self, sender, event):
reading = self.port.ReadExisting()
...
可惜这毫无价值,因为不保证会引发此事件!
不保证每收到一个字节都会引发 DataReceived 事件.
The DataReceived event is not guaranteed to be raised for every byte received.
那么回到编写侦听器线程.我已经用一个 BackgroundWorker
很快地完成了这一点,它只是一遍又一遍地调用 port.ReadExisting()
.它在字节进入时读取它们,当它看到行结束时 (
),它将读取的内容放入 linebuffer
.然后我程序的其他部分查看 linebuffer
看是否有完整的行等待使用.
So back to writing a listener thread, then. I've accomplished this rather quickly with a BackgroundWorker
that just calls port.ReadExisting()
over and over again. It reads bytes as they come in, and when it sees a line ending (
), it places what it read into a linebuffer
. Then other parts of my program look at the linebuffer
to see if there are any complete lines waiting to be used.
现在,这显然是一个经典的生产者-消费者问题.生产者是 BackgroundWorker
,将完整的行放入 linebuffer
.消费者是一些代码,它尽可能快地使用 linebuffer
中的这些行.
Now, this is a classic producer-consumer problem, obviously. The producer is the BackgroundWorker
, placing complete lines into linebuffer
. The consumer is some code that consumes those lines from the linebuffer
as fast as possible.
但是,消费者有点低效.现在他一直在检查linebuffer
,每次都发现它是空的而失望;虽然偶尔会发现里面有一条线等着.优化这一点的最佳方法是什么,以便消费者只有在有可用线路时才醒来?这样,消费者就不会不停地访问 linebuffer
,这可能会引入一些并发问题.
However, the consumer is sort of inefficient. Right now he's constantly checking the linebuffer
, getting disappointed each time to find it empty; though every once in a while does find a line waiting inside. What's the best way to optimize this so that the consumer only wakes up when there is a line available? That way the consumer isn't spinning around constantly accessing the linebuffer
, which might introduce some concurrency issues.
另外,如果有更简单/更好的方式从串行端口不断读取,我愿意接受建议!
Also, if there is a simpler/better way of reading constantly from a serial port, I'm open to suggestions!
推荐答案
我不明白为什么你不能使用 DataReceived
事件.正如文档所述
I don't see why you can't use the DataReceived
event. As the docs state
DataReceived 事件不是保证每个字节都被提升已收到.使用 BytesToRead 属性确定剩下多少数据在缓冲区中读取.
The DataReceived event is not guaranteed to be raised for every byte received. Use the BytesToRead property to determine how much data is left to be read in the buffer.
这就是说,您不能保证为每个单独的数据字节获得一个单独的事件.您可能需要使用 BytesToRead
属性检查并查看端口上是否有多个可用字节并读取相应的字节数.
All that is saying is that you aren't guaranteed to get a separate event for each individual byte of data. You may need to check and see if there more than one byte available on the port using the BytesToRead
property and read the corresponding number of bytes.
这篇关于使用后台线程不断从串口读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用后台线程不断从串口读取
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01