PySerial: open multiple ports at once

EDIT : found the problem: I tried to reference the variable, but mixed up its name, so instead I declared a new variable. It turns out that pySerial is not limited to one open sequential point at a time.

I try to open two serial ports at once using the following code

ser0 = serial.Serial( port = port_list[0], baudrate = 115200, timeout = 0.1 ) ser1 = serial.Serial( port = port_list[1], baudrate = 115200, timeout = 0.1 ) 

But it seems that I am opening the second, the first is closing. Is there a limit to one serial port opened at the same time using pySerial?

Thanks TG

EDIT: I had to post this to start with

 while not (comm_port0_open and comm_port1_open): print 'COM ports available:' port_list = [] i = 0 for port in __EnumSerialPortsWin32(): port_list.append(port[0]) print '%i:' % i, port[0] i+=1 print 'Connect to which port? (0, 1, 2, ...)' comm_port_str = sys.stdin.readline() try: if len(comm_port_str)>0: if comm_port0_open: ser1 = serial.Serial( port = port_list[int(comm_port_str)], baudrate = 115200, timeout = 0.1 ) comm1_port_open = True print '%s opened' % port_list[int(comm_port_str)] else: ser0 = serial.Serial( port = port_list[int(comm_port_str)], baudrate = 115200, timeout = 0.1 ) comm0_port_open = True print '%s opened' % port_list[int(comm_port_str)] else: print 'Empty input' except: print 'Failed to open comm port, try again' 
+1
source share
3 answers

Not seeing the context of the code, this is just an assumption.

After garbage collection is complete, the serial ports will be closed and __del__ will __del__ . Uner CPython, if your link count ser0 drops to zero after this block of code is started, but somehow ser1 gone, it will open the external port when another port opens.

But write more code!

0
source

The variables specified when declaring open com ports do not match those that were marked in the while state. Unfortunately.

0
source

In your code, you test comm_port0_open and comm_port1_open , but set them using comm0_port_open = True and comm1_port_open . Different names!

Another point: do not use bare 'except', it can hide all kinds of errors.

0
source

All Articles