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*')