不断从具有后台线程的串行端口读取 [英] Constantly reading from a serial port with a background thread

查看:68
本文介绍了不断从具有后台线程的串行端口读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于串行端口通信是异步的,所以我很早就想到了与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

足够简单.但是正如我提到的,我想在到达该端口时不断检查该端口是否有新数据.理想情况下,只要有数据等待,我就会让操作系统 tell 出现. 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()
    ...

可惜的是,这很没用,但因为不能保证会引发此事件

Too bad this is worthless, though, because this event isn't guaranteed to be raised!

不保证每个接收到的字节都会引发DataReceived事件.

The DataReceived event is not guaranteed to be raised for every byte received.

然后回到编写侦听器线程.我已经用BackgroundWorker很快地完成了这个任务,只需反复调用port.ReadExisting()即可.它读取传入的字节,并在看到一行结束符(\r\n)时将其读取的内容放入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 (\r\n), 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.

这篇关于不断从具有后台线程的串行端口读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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