31

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:

Community
  • 1
  • 1
Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
  • 4
    I've been doing cross-platform serial stuff for years, and I always hack together the same silly platform detection, globbing, manually tweaked monster every time. I'll be interested to see if you get any nice answers. – detly Jul 04 '12 at 00:20
  • Well, I guess that's what it's going to have to be then :(. When I have time I'll test it on Linux and Mac, and see if I can't wrap it up into a library. – Chris Laplante Jul 04 '12 at 00:26
  • Give it a couple of days, someone might have a more elegant solution :) – detly Jul 04 '12 at 02:51
  • Just thought I'll add it here. bitpim had quite a bit of code for comscan on multiple platforms. Probably useful to get some code out of there to build a cross platform serial port enumerator. http://bitpim.svn.sourceforge.net/viewvc/bitpim/trunk/bitpim/src/comscan.py?revision=4835&view=markup – Nandeep Mali Sep 20 '12 at 18:43
  • @n9986: That looks really promising, thanks! You should make it an answer. – Chris Laplante Sep 20 '12 at 21:40
  • fyi, I'm looking for same. Interesting comparison is how Arduino IDE/Processing does it's serial port enumeration for the Tools menu: https://github.com/arduino/Arduino/blob/master/arduino-core/src/processing/app/SerialPortList.java#L40 tldr: regex city! – tardate Oct 15 '15 at 06:56

5 Answers5

8

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*')
Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
  • 1
    You might want to use `serial.tools.list_ports_posix` if the platform is non-Windows. It supports several Posix platforms besides OSX and Linux. – Abe Karplus Jul 16 '12 at 19:39
  • serial port devices for Mac and Linux don't have to follow those patterns anyway, do they? I think there can be udev rules that give them any name in `/dev/` – Mu Mind Sep 21 '12 at 05:26
  • On OS X things come sometimes as '/dev/tty...' not matching the pattern above. E.g. 2 different USB stick modems from Huawei can come as '/dev/cu.HUAWEIMobile-' or '/dev/tty.HUAWEIMobile-' – fmalina Sep 21 '12 at 12:41
  • @Mu Mind Could you elaborate on the udev rules? – fmalina Sep 21 '12 at 12:48
  • You can add rules files in /etc/udev/rules.d/ and override the `NAME` field. Here's a [full example](http://hintshop.ludvig.co.nz/show/persistent-names-usb-serial-devices/) (using `SYMLINK` instead of `NAME`), and here's a [shorter example](http://www-uxsup.csx.cam.ac.uk/pub/doc/suse/sles9/adminguide-sles9/ch15.html#id2574232) using `NAME`. – Mu Mind Sep 21 '12 at 14:46
  • (Or someone else could add them, e.g. they could come with your distro or a package you install, so you can't safely assume) – Mu Mind Sep 21 '12 at 14:56
  • My method probably isn't reliable (or correct?) for detecting on Linux/MacOS. For that reason I'm going to give @n9986 the bounty (but not accept it just yet). When I can verify it I will accept it. – Chris Laplante Sep 21 '12 at 15:02
7

bitpim had quite a bit of code for comscan on multiple platforms. Probably useful to get some code out of there to build a cross platform serial port enumerator. You can run the detection code directly in command line to test it out.

Link to source file comscan.py.

In Linux, it didn't seem to detect '/dev/ttyS' ports. Adding the following line below line #378 worked:

("/dev/ttyS", "Standard Serial Port", "serial"),

The author has made it easy to add different kind of identifiers for serial ports.

In Mac, it worked just fine. (Had an Arduino handy to test)

In Windows, it successfully detected the connected port (for the connected FTDI connector). Requires pywin32.

With pyserial, I tried the following:

python -m serial.tools.list_ports

Does not seem to work on Windows. Works on Mac. Works on Linux.

It is interesting to see the Platform section in the documentation for this command:

Platform :  Posix (/dev files)
Platform :  Linux (/dev files, sysfs and lsusb)
Platform :  Windows (setupapi, registry)

I think an amalgamation of the two should give an (almost) 100% reliable com port enumerator.

Edit: Tried all of the above in Python 2.7.

Nandeep Mali
  • 4,456
  • 1
  • 25
  • 34
  • Thanks for posting all this! I've given you the bounty, since I think this is the most promising answer so far. When I (or someone else) gets a chance to verify it, I will accept it. – Chris Laplante Sep 21 '12 at 15:05
  • Thanks! I'll see if I can dig up some more information for this (web or from my notes) later. Will add it here. – Nandeep Mali Sep 23 '12 at 06:38
  • 1
    `serial.tools.list_ports` worked on python 3.4 on windows for detecting an FTDI cable for me. :) – Sam Finnigan Nov 11 '15 at 13:11
4

Does the pyserial function, serial.tools.list_ports, give you what you want?

Gerrat
  • 28,863
  • 9
  • 73
  • 101
  • Unfortunately, on Windows, `comports()` gave me nothing. The first method above, on the other hand, correctly listed the (single) port I had active. – Chris Laplante Jul 03 '12 at 20:27
  • 1
    It doesn't seem to work for virtual USB com ports (which are essentially the norm in new computers) – Jason S May 29 '13 at 22:47
0

I don't know if you're still looking for answers to this, but since I have a working solution I thought I'd post it. Here is the getports package as part of my Arduino Data Logger project. I've tested it to work on Mac, at least some flavors of Linux, and Windows 7. Unlike bitpim's comscan, it does not use any modules not from the standard library. Also, it does not use /dev globbing on Linux or Mac, so renaming rules should not be a problem. (On Linux it uses sysfs and on Mac it uses IOKit.) I don't know if it detects virtual serial ports on Linux, but if it doesn't, try replacing

sys_suffix = '/device/'

with

sys_suffix = ''

in linuxgetports.py

Abe Karplus
  • 8,350
  • 2
  • 27
  • 24
0

I have this solution that works on both, windows and Linux. Thought I would add it here in case it will help any one. The desired port number is sent as an command line argument in pytest_configure.

def pytest_configure(config):
global tty
if sys.platform == "win32":           # OS is Windows
    # use case - send a comma-separated list of COM ports e.g. 3,6 or /dev/ttyUSB0,/dev/ttyACM1
    if ',' in str(config.getoption('--tty')):
        params = str(config.getoption('--tty')).split(",")
        tty = [('COM'+str(params[0])),('COM'+str(params[1]))]
    else:
        tty = 'COM' + str(config.getoption('--tty'))
elif sys.platform == "linux":      # OS is linux/debian
    if ',' in config.getoption('--tty'):
        tty = (config.getoption('--tty')).split(",")
    else:
        tty = config.getoption('--tty')



def openPort(portNum):
try:
    from serial.tools.list_ports import comports
except ImportError:
    return None
if comports:
    ports_list = list(comports())
for port in ports_list:
    if portNum in port:
        try:
            serialData = serial.Serial(port=portNum, baudrate=115200, parity=serial.PARITY_NONE,
                                       stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
            return serialData
        except serial.serialutil.SerialException:
            return -1

One can always use a glob statement to search for available ports (example for Linux):

TTYs = glob.glob("/dev/ttyACM*") + glob.glob("/dev/ttyUSB*")
Archie
  • 153
  • 9