What is the cross-platform method of enumerating serial ports in Python (including virtual ports)?

Chris Laplante picture Chris Laplante · Jul 3, 2012 · Viewed 14.3k times · Source

Note: I'm using Python 2.7, and pySerial for serial communications.

I found this article which lists two ways: http://www.zaber.com/wiki/Software/Python#Displaying_a_list_of_available_serial_ports

This method works on Windows and Linux, but sometimes misses virtual ports on Linux:

import serial

def scan():
   # scan for available ports. return a list of tuples (num, name)
   available = []
   for i in range(256):
       try:
           s = serial.Serial(i)
           available.append( (i, s.portstr))
           s.close()
       except serial.SerialException:
           pass
   return available

print "Found ports:"
for n,s in scan(): print "(%d) %s" % (n,s)

And this one that only works on Linux, but includes virtual ports:

import serial, glob

def scan():
   # scan for available ports. return a list of device names.
   return glob.glob('/dev/ttyS*') + glob.glob('/dev/ttyUSB*')

print "Found ports:"
for name in scan(): print name

I suppose I could do platform detection to use the second method (the one that includes virtual ports) when running on Linux, and the first method when running Windows, but what about Mac?

How should I enumerate serial ports (virtual too) regardless of platform?

Edit

I found a few pertinent questions:

Answer

Chris Laplante picture Chris Laplante · Jul 9, 2012

This is what I've been using. It's a mashup of the methods I posted above. I'd still like to see better solutions, though.

# A function that tries to list serial ports on most common platforms
def list_serial_ports():
    system_name = platform.system()
    if system_name == "Windows":
        # Scan for available ports.
        available = []
        for i in range(256):
            try:
                s = serial.Serial(i)
                available.append(i)
                s.close()
            except serial.SerialException:
                pass
        return available
    elif system_name == "Darwin":
        # Mac
        return glob.glob('/dev/tty*') + glob.glob('/dev/cu*')
    else:
        # Assume Linux or something else
        return glob.glob('/dev/ttyS*') + glob.glob('/dev/ttyUSB*')