5

I'm trying to read out multiple serial ports at the same time with Python 2.7 and PySerial.
Features should be:

  1. in the main program I get all open serial ports, open them and append the serial object to serialobjects
  2. I want to read each serial port data in one subprocess for parallelization

The big problem is: how do I pass the serial port object to the subprocess?
OR:
Does another (and maybe better) solution exist to this? (Maybe this: How do I apply twisted serial ports to my problem?)

EDIT

I think I wasn't totally clear what i want to achieve.
I want to read out 2 or more serial ports at the same time. Because of timeout and readout times it isn't possible to read them out at the same time in one process.
The following approach

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

doesn't serve my needs.

Another approch is to parallelize the serial readings in subprocesses.

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 (taken from related post and edited)

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

Thanks in advance!

Community
  • 1
  • 1
user3116388
  • 53
  • 1
  • 1
  • 5
  • Passing opened file descriptor between process is very non portable. The only interest can be if you execute the process from an user with less privileged. If you don't need that, I think it's lot easier to do the opening in the subprocess. (And you should look to the multiprocessing module, should be more easier, more cleaner and more adapt to your problem then Popen and PIPE...) – Cld Dec 15 '14 at 12:52
  • OK, understood. But how do I close the serial ports at any time I want to close them from the main program? Can you give an example on multiprocessing in this particular case? – user3116388 Dec 15 '14 at 14:55

1 Answers1

3

First change ReadCOM.py to receive arguments

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

and after pass it in 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
Michele d'Amico
  • 22,111
  • 8
  • 69
  • 76
  • Thank you for your answer, i'll try it now. Last Question: how to close the open ports after i read out 10 times? – user3116388 Dec 15 '14 at 13:56
  • @user3116388 use `for _ in xrange(10)` instead of `while(True)` in `ReadCOM.py` and before exit use `ser.close()` to close it – Michele d'Amico Dec 15 '14 at 14:08
  • Hi Michele, sorry, but i meant how do I close the open ports at any time I want to close them from the main program (10 times was only the value of the "for i in range(10):" loop) – user3116388 Dec 15 '14 at 14:13
  • @user3116388 You cannot control the serial port out the `ReadCOM.py` application. You must find a way to send a asynchronous message. Maybe you had chose the wrong architecture: take a look to `multiprocess` because I think that `threading` if not suitable for that ... but that is an other question – Michele d'Amico Dec 15 '14 at 14:29