I'm having a problem with Python and passing a serial object as a parameter to a separate process. The program is being run in Windows 8, so using global variables isn't an option.
from multiprocessing import Queue
from multiprocessing import Process
import os
import serial
from serial.tools import list_ports
from time import sleep
displayMessages = Queue()
modemPort = None
def processDisplayMessages(displayMessages):
while True:
msg = displayMessages.get() #should halt until message in queue
print msg
def processIncomingSerialMessages(modemPort, displayMessages):
while True:
line = modemPort.readline()
displayMessages.put(line)
def main():
print "Serial Send Test"
Process(target=processDisplayMessages, args = (displayMessages,)).start()
modemPort = serial.Serial('COM5', 57600, timeout=0.9) # open first serial port
Process(target=processIncomingSerialMessages, args = (modemPort, displayMessages)).start()
print "Back from launch"
sleep(0.1)
if __name__ == '__main__':
main()
When the program runs I get the following error:
Process Process-2:
Traceback (most recent call last):
File "c:\python27\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
File "c:\python27\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\matthew.parets\Documents\..Development\RaspberryPi\windows\seri
alRecvPrototype.py", line 27, in processIncomingSerialMessages
line = modemPort.readline()
File "c:\python27\lib\site-packages\serial\serialwin32.py", line 246, in read
if not self.hComPort: raise portNotOpenError
AttributeError: 'Serial' object has no attribute 'hComPort'
If I place the open for the serial port (modemPort) as the first line of processIncomingSerialMessages the program works fine. The issue is that I need to decouple input and output from the modem so I need to pass the serial object as a parameter. And Python doesn't seem to like that.
Can anyone see the mistake I am making, or can anyone suggest an alternative?