简体   繁体   English

pyserial - 可以从线程a写入串口,从线程b做阻塞读取吗?

[英]pyserial - possible to write to serial port from thread a, do blocking reads from thread b?

I tried googling this, couldn't find an answer, searched here, couldn't find an answer. 我试过谷歌搜索,找不到答案,在这里搜索,找不到答案。 Has anyone looked into whether it's thread safe to write to a Serial() object (pyserial) from thread a and do blocking reads from thread b? 有没有人研究从线程a写入Serial()对象(pyserial)是否是线程安全并从线程b执行阻塞读取?

I know how to use thread synchronization primitives and thread-safe data structures, and in fact my current form of this program has a thread dedicated to reading/writing on the serial port and I use thread-safe data structures to coordinate activities in the app. 我知道如何使用线程同步原语和线程安全的数据结构,事实上我这个程序的当前形式有一个专用于读取/写入串口的线程,我使用线程安全的数据结构来协调应用程序中的活动。

My app would benefit greatly if I could write to the serial port from the main thread (and never read from it), and read from the serial port using blocking reads in the second thread (and never write to it). 如果我可以从主线程写入串口(并且从不读取它),我的应用程序将受益匪浅,并使用第二个线程中的阻塞读取从串行端口读取(并且永远不会写入它)。 If someone really wants me to go into why this would benefit the app I can add my reasons. 如果有人真的希望我进入为什么这会使应用程序受益,我可以添加我的理由。 In my mind there would be just one instance of Serial() and even while thread B sits in a blocking read on the Serial object, thread A would be safe to use write methods on the Serial object. 在我看来,只有一个Serial()实例,即使线程B位于Serial对象的阻塞读取中,线程A也可以安全地在Serial对象上使用write方法。

Anyone know whether the Serial class can be used this way? 有人知道Serial类是否可以这样使用?

EDIT: It occurs to me that the answer may be platform-dependent. 编辑:我觉得答案可能是平台依赖的。 If you have any experience with a platform like this, it'd be good to know which platform you were working on. 如果您对此类平台有任何经验,最好知道您正在使用哪个平台。

EDIT: There's only been one response but if anyone else has tried this, please leave a response with your experience. 编辑:只有一个回复,但如果有其他人尝试过此,请留下您的经验回复。

I have done this with pyserial. 我用pyserial完成了这个。 Reading from one thread and writing from another should not cause problems in general, since there isn't really any kind of resource arbitration problem. 从一个线程读取并从另一个线程写入不应该引起一般问题,因为实际上没有任何类型的资源仲裁问题。 Serial ports are full duplex, so reading and writing can happen completely independently and at the same time. 串行端口是全双工的,因此读写可以完全独立地同时进行。

我在Linux(和Windows)上以这种方式使用了pyserial,没问题!

I would recommend to modify Thread B from "blocking read" to "non blocking read/write". 我建议将线程B从“阻塞读取”修改为“非阻塞读/写”。 Thread B would become your serial port "Daemon". 线程B将成为您的串口“守护进程”。

Thread A could run at full speed for a friendly user interface or perform any real time operation. 线程A可以全速运行以获得友好的用户界面或执行任何实时操作。

Thread A would write a message to Thread B instead of trying to write directly to the serial port. 线程A会向线程B写入消息,而不是尝试直接写入串行端口。 If the size/frequency of the messages is low, a simple shared buffer for the message itself and a flag to indicate that a new message is present would work. 如果消息的大小/频率很低,则消息本身的简单共享缓冲区和指示新消息存在的标志将起作用。 If you need higher performance, you should use a stack. 如果您需要更高的性能,则应使用堆栈。 This is actually implemented simply using an array large enough to accumulate many message to be sent and two pointers. 实际上,这实际上只是使用一个足够大的数组来累积许多要发送的消息和两个指针。 The write pointer is updated only by Thread A. The read pointer is updated only by Thread B. 写指针仅由线程A更新。读指针仅由线程B更新。

Thread B would grab the message and sent it to the serial port. 线程B将获取消息并将其发送到串行端口。 The serial port should use the timeout feature so that the read serial port function release the CPU, allowing you to poll the shared buffer and, if any new message is present, send it to the serial port. 串行端口应使用超时功能,以便读取串行端口功能释放CPU,允许您轮询共享缓冲区,如果存在任何新消息,则将其发送到串行端口。 I would use a sleep at that point to limit the CPU time used by Thread B.. Then, you can make Thread B loop to the read serial port function. 我会在此时使用休眠来限制线程B使用的CPU时间。然后,您可以使线程B循环到读取串行端口功能。 If the serial port timeout is not working right, like if the USB-RS232 cable get unplugged, the sleep function will make the difference between a good Python code versus the not so good one. 如果串口超时不能正常工作,就像USB-RS232电缆被拔掉一样,睡眠功能将使好的Python代码与不太好的Python代码区别开来。

I wrote a script which is doing this. 我写了一个脚本,这样做。 See: https://code.google.com/p/rpicopter/source/browse/RPiQuadroServer.py 请参阅: https//code.google.com/p/rpicopter/source/browse/RPiQuadroServer.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM