我正在尝试使用 Python 2.7 和 PySerial 同时读取多个串行端口。
特点应该是:
最大的问题是:如何将串口对象传递给子进程?
或者:
是否存在另一种(也许更好)的解决方案?(也许是这样:如何将扭曲的串行端口应用于我的问题?)
我想我并不完全清楚我想要实现什么。
我想同时读取2个或更多串口。由于超时和读出时间的原因,不可能在一个进程中同时读出它们。
下面的方法
ser1 = serial.Serial(port="COM1",baudrate=9600)
ser2 = serial.Serial(port="COM2",baudrate=9600)
ser1.write('command for reading out device 1')
output1 = ser1.readline()
ser2.write('command for reading out device 2')
# now you have to wait at least 100ms for device 2 to respond
output2 = ser2.readline()
Run Code Online (Sandbox Code Playgroud)
不能满足我的需求。
另一种方法是并行化子进程中的串行读取。
import serial # serial communication
from subprocess import Popen, PIPE
ports = ["COM1", "COM2"]
for port in ports:
ser = serial.Serial() …Run Code Online (Sandbox Code Playgroud) python serialization serial-port nonblocking python-multiprocessing