Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

  • MacPython: programmatically finding all serial ports
  • MacOS: what's the difference between /dev/tty.* and /dev/cu.*?
  • How to find all serial devices (ttyS, ttyUSB, ..) on Linux without opening them?
like image 685
Chris Laplante Avatar asked Jul 03 '12 02:07

Chris Laplante


People also ask

How to do cross platform serial communication in Python?

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.

How to write to a serial port in Python?

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.

How to use pyserial in Python?

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.

What does the first line import serial do in pyserial?

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.


1 Answers

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*') 
like image 164
Chris Laplante Avatar answered Oct 14 '22 17:10

Chris Laplante