Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check if serial port is already open (by another process) in Linux, using Python 2.7 (and possibly pyserial)?
    primarykey
    data
    text
    <p>I know there are other questions quite similar to mine, but none of them address the issue I'm having. </p> <p>I'd like to use <code>pyserial</code> to access a serial port (<code>/dev/tty...</code>), but only on the condition that another process hasn't already opened it.</p> <p>The following snippet returns four available ports on my Ubuntu 12.04 machine, when run once. If I run it a second time I would expect no ports to be available. Sadly, the same list of ports are returned. It seems <code>pyserial</code> cannot identify that another process has already opened the port.</p> <p>I'd expect a <code>SerialException</code> to be thrown, or the <code>isOpen()</code> method to return False, but <code>pyserial</code> happily opens the multiple times.</p> <pre><code>import serial from serial import tools from serial.tools import list_ports def available_ttys(): for tty in serial.tools.list_ports.comports(): try: port = serial.Serial(port=tty[0]) if port.isOpen(): yield port except serial.SerialException as ex: print 'Port {0} is unavailable: {1}'.format(tty, ex) def main(): ttys = [] for tty in available_ttys(): ttys.append(tty) print tty input('waiting ...') if __name__ == '__main__': main() </code></pre> <p>This is the output regardless of how many times I run it in parallel:</p> <pre><code>Port ('/dev/ttyS31', 'ttyS31', 'n/a') is unavailable: Could not configure port: (5, 'Input/output error') ... Port ('/dev/ttyS0', 'ttyS0', 'n/a') is unavailable: Could not configure port: (5, 'Input/output error') Serial&lt;id=0x7fca9d9f1c90, open=True&gt;(port='/dev/ttyUSB1', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False) Serial&lt;id=0x7fca9d9f1cd0, open=True&gt;(port='/dev/ttyACM2', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False) Serial&lt;id=0x7fca9d9f1e50, open=True&gt;(port='/dev/ttyACM1', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False) Serial&lt;id=0x7fca9d9f1ed0, open=True&gt;(port='/dev/ttyACM0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False) waiting ... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload