<small id='VAYTJ'></small><noframes id='VAYTJ'>

    <bdo id='VAYTJ'></bdo><ul id='VAYTJ'></ul>

    <i id='VAYTJ'><tr id='VAYTJ'><dt id='VAYTJ'><q id='VAYTJ'><span id='VAYTJ'><b id='VAYTJ'><form id='VAYTJ'><ins id='VAYTJ'></ins><ul id='VAYTJ'></ul><sub id='VAYTJ'></sub></form><legend id='VAYTJ'></legend><bdo id='VAYTJ'><pre id='VAYTJ'><center id='VAYTJ'></center></pre></bdo></b><th id='VAYTJ'></th></span></q></dt></tr></i><div id='VAYTJ'><tfoot id='VAYTJ'></tfoot><dl id='VAYTJ'><fieldset id='VAYTJ'></fieldset></dl></div>
    1. <tfoot id='VAYTJ'></tfoot>

      1. <legend id='VAYTJ'><style id='VAYTJ'><dir id='VAYTJ'><q id='VAYTJ'></q></dir></style></legend>
      2. 使用后台线程不断从串口读取

        时间:2023-11-10
            <tbody id='4kUYP'></tbody>
        • <tfoot id='4kUYP'></tfoot>
          <i id='4kUYP'><tr id='4kUYP'><dt id='4kUYP'><q id='4kUYP'><span id='4kUYP'><b id='4kUYP'><form id='4kUYP'><ins id='4kUYP'></ins><ul id='4kUYP'></ul><sub id='4kUYP'></sub></form><legend id='4kUYP'></legend><bdo id='4kUYP'><pre id='4kUYP'><center id='4kUYP'></center></pre></bdo></b><th id='4kUYP'></th></span></q></dt></tr></i><div id='4kUYP'><tfoot id='4kUYP'></tfoot><dl id='4kUYP'><fieldset id='4kUYP'></fieldset></dl></div>

                <bdo id='4kUYP'></bdo><ul id='4kUYP'></ul>
              • <legend id='4kUYP'><style id='4kUYP'><dir id='4kUYP'><q id='4kUYP'></q></dir></style></legend>
                • <small id='4kUYP'></small><noframes id='4kUYP'>

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

                  问题描述

                  由于串行端口通信是异步的,我很早就发现我的项目涉及与 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.

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

                  上一篇:使用 Async await 方法从串口异步读取 下一篇:SerialPort 类和 DataReceived 事件...获取字节.使用 ReadLine 还是 ReadExis

                  相关文章

                  <legend id='LHzGN'><style id='LHzGN'><dir id='LHzGN'><q id='LHzGN'></q></dir></style></legend>
                • <tfoot id='LHzGN'></tfoot>

                  <small id='LHzGN'></small><noframes id='LHzGN'>

                  • <bdo id='LHzGN'></bdo><ul id='LHzGN'></ul>

                    <i id='LHzGN'><tr id='LHzGN'><dt id='LHzGN'><q id='LHzGN'><span id='LHzGN'><b id='LHzGN'><form id='LHzGN'><ins id='LHzGN'></ins><ul id='LHzGN'></ul><sub id='LHzGN'></sub></form><legend id='LHzGN'></legend><bdo id='LHzGN'><pre id='LHzGN'><center id='LHzGN'></center></pre></bdo></b><th id='LHzGN'></th></span></q></dt></tr></i><div id='LHzGN'><tfoot id='LHzGN'></tfoot><dl id='LHzGN'><fieldset id='LHzGN'></fieldset></dl></div>