Python Pyserial 同时从多个串口读取数据

标签 python serialization serial-port nonblocking python-multiprocessing

<分区>

我正在尝试使用 Python 2.7 和 PySerial 同时读取多个串行端口。
特征应该是:

  1. 在主程序中,我获取所有打开的串行端口,打开它们并将串行对象附加到 serialobjects
  2. 我想在一个子进程中并行读取每个串口数据

最大的问题是:如何将串口对象传递给子进程?
或者:
是否存在另一个(也许更好)的解决方案? (可能是 this:如何将双绞串口应用于我的问题?)

编辑

我想我并不完全清楚我想要实现的目标。
我想同时读出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()

不满足我的需求。

另一种方法是并行化子进程中的串行读数。

主.py
import serial   # serial communication
from subprocess import Popen, PIPE

ports = ["COM1", "COM2"]
for port in ports:
    ser = serial.Serial()
    ser.port=port
    ser.baudrate=9600
    # set parity and ...

    serialobjects.append(ser)

# call subprocess
# pass the serial object to subprocess
# read out serial port


# HOW TO PASS SERIAL OBJECT HERE to stdin
p1 = Popen(['python', './ReadCOM.py'], stdin=PIPE, stdout=PIPE, stderr=PIPE) # read COM1 permanently
p2 = Popen(['python', './ReadCOM.py'], stdin=PIPE, stdout=PIPE, stderr=PIPE) # read COM2 permanently

for i in range(10):
    print "received from COM1: %s" % p1.stdout.readline() # print output from ReadCOM.py for COM1
    print "received from COM2: %s" % p2.stdout.readline() # print output from ReadCOM.py for COM2

ReadCOM.py(取自 related post 并编辑)

import sys

while True:  # The program never ends... will be killed when master is over.
    # sys.stdin.readline()

    ser.write('serial command here\n') # send command to serial port
    output = ser.readline() # read output

    sys.stdout.write(output) # write output to stdout
    sys.stdout.flush()

提前致谢!

最佳答案

首先修改ReadCOM.py来接收参数

import sys
import serial

ser = serial.Serial(port=sys.argv[1],baudrate=int(sys.argv[2]))
while True:  # The program never ends... will be killed when master is over.
    # sys.stdin.readline()

    ser.write('serial command here\n') # send command to serial port
    output = ser.readline() # read output

    sys.stdout.write(output) # write output to stdout
    sys.stdout.flush()

然后将其传递到 main.py 中:

from subprocess import Popen, PIPE

# call subprocess
# pass the serial object to subprocess
# read out serial port


# HOW TO PASS SERIAL OBJECT HERE to stdin
p1 = Popen(['python', './ReadCOM.py', "COM1", "9600"], stdin=PIPE, stdout=PIPE, stderr=PIPE) # read COM1 permanently
p2 = Popen(['python', './ReadCOM.py', "COM2", "9600"], stdin=PIPE, stdout=PIPE, stderr=PIPE) # read COM2 permanently

for i in range(10):
    print "received from COM1: %s" % p1.stdout.readline() # print output from ReadCOM.py for COM1
    print "received from COM2: %s" % p2.stdout.readline() # print output from ReadCOM.py for COM2

关于Python Pyserial 同时从多个串口读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27484250/

相关文章:

Python - 加入换行符

python - Django Rest 嵌套序列化器的 AttributeError

Java Jackson从多个模块中选择注册模块将JSON序列化为字符串?

linux - 如何在 gnu screen 中切换 CR/LF?

java - 从 JFrame 类调用串行端口类

python - 如何使用 PyOpenGL 渲染文本?

python - 如何在 MayaVi 中绘制正确的 3D 轴,就像在 Matplotlib 中找到的那样

python - Pandas 数据框中元素和子集的总长度

C# 自定义序列化/反序列化以及 DeflateStreams

Java 和 USB 转 COM