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

时间:2014-12-15 12:35:44

标签: python serialization serial-port nonblocking python-multiprocessing

我正在尝试使用Python 2.7和PySerial同时读出多个串行端口 功能应该是:

  1. 在主程序中我得到所有打开的串口,打开它们并将串行对象追加到serialobjects
  2. 我想在一个子进程中读取每个串口数据以进行并行化
  3. 最大的问题是:如何将串口对象传递给子进程?
    OR:
    是否存在另一个(也许是更好的)解决方案? (也许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()
    

    无法满足我的需求。

    另一个方法是并行化子进程中的序列读数。

    main.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()
    

    提前致谢!

1 个答案:

答案 0 :(得分:2)

首先更改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