'Read multiple serial port at one time using python

I'm trying to read out multiple serial ports at the same time with Python. I found some code to make it, but its not going well. The code not read the serial print in my arduino and just read some empty string (?). This is my code

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', "COM14", "9600"], stdin=PIPE, stdout=PIPE, stderr=PIPE) # read COM1 permanently
p2 = Popen(['python', './ReadCOM.py', "COM9", "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

Does another (and maybe better) solution exist to this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source