title: Python Pyserial read data form multiple serial ports at same time
body: <p>I'm trying to read out multiple serial ports at the same time with Python 2.7 and PySerial.<br>
  Features should be:</p>
  
  <ol>
  <li>in the main program I get all open serial ports, open them and append the serial object to serialobjects</li>
  <li>I want to read each serial port data in one subprocess for parallelization    </li>
  </ol>
  
  <p>The big problem is: how do I pass the serial port object to the subprocess?<br>
  OR:<br>
  Does another (and maybe better) solution exist to this?
  (Maybe <a href="https://stackoverflow.com/questions/16255807/pyserial-is-there-a-way-to-select-on-multiple-ports-at-once">this</a>: How do I apply twisted serial ports to my problem?)  </p>
  
  <h2>EDIT</h2>
  
  <p>I think I wasn't totally clear what i want to achieve.<br>
  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.<br>
  The following approach</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>doesn't serve my needs.  </p>
  
  <p>Another approch is to parallelize the serial readings in subprocesses.</p>
  
  <h2>main.py</h2>
  
  <pre><code>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
  </code></pre>
  
  <h2>ReadCOM.py  (taken from <a href="https://stackoverflow.com/questions/8260502/python-multiple-threads-multiple-processes-for-reading-serial-ports">related post</a> and edited)</h2>
  
  <pre><code>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()
  </code></pre>
  
  <p>Thanks in advance!</p>