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: