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

一笑奈何 提交于 2019-12-18 10:19:38

问题


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?

回答1:


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



回答2:


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.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/11303850/what-is-the-cross-platform-method-of-enumerating-serial-ports-in-python-includi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!