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:
Python provides an easy way to do cross platform serial communication using pySerial module. The modules hides all the OS specific peculiarities and presents a uniform simple interface for programming the serial port. The code written with pySerial can run unmodified on Windows and Linux systems.
Now lets open a serial port and write some values to it. The code in python is quite straight forward, you can just read it like plain English. Open a text editor and type the following lines of code into it .Save the file with a ” .py” extension. The Below code writes character 'A' to the serial port.
The code in python is quite straight forward, you can just read it like plain English. Open a text editor and type the following lines of code into it .Save the file with a ” .py” extension. The Below code writes character 'A' to the serial port. The first line import serial imports the pySerial module so that your program can use it.
The first line import serial imports the pySerial module so that your program can use it. ComPort = serial.Serial ('COM24') opens the serial port named COM24.
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*')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With