Home | History | Annotate | Download | only in tools
      1 #!/usr/bin/env python
      2 
      3 # portable serial port access with python
      4 
      5 # This is a module that gathers a list of serial ports on POSIXy systems.
      6 # For some specific implementations, see also list_ports_linux, list_ports_osx
      7 #
      8 # this is a wrapper module for different platform implementations of the
      9 # port enumeration feature
     10 #
     11 # (C) 2011-2013 Chris Liechti <cliechti (at] gmx.net>
     12 # this is distributed under a free software license, see license.txt
     13 
     14 """\
     15 The ``comports`` function is expected to return an iterable that yields tuples
     16 of 3 strings: port name, human readable description and a hardware ID.
     17 
     18 As currently no method is known to get the second two strings easily, they are
     19 currently just identical to the port name.
     20 """
     21 
     22 import glob
     23 import sys
     24 import os
     25 
     26 # try to detect the OS so that a device can be selected...
     27 plat = sys.platform.lower()
     28 
     29 if   plat[:5] == 'linux':    # Linux (confirmed)
     30     from serial.tools.list_ports_linux import comports
     31 
     32 elif plat == 'cygwin':       # cygwin/win32
     33     def comports():
     34         devices = glob.glob('/dev/com*')
     35         return [(d, d, d) for d in devices]
     36 
     37 elif plat[:7] == 'openbsd':    # OpenBSD
     38     def comports():
     39         devices = glob.glob('/dev/cua*')
     40         return [(d, d, d) for d in devices]
     41 
     42 elif plat[:3] == 'bsd' or  \
     43         plat[:7] == 'freebsd':
     44 
     45     def comports():
     46         devices = glob.glob('/dev/cuad*')
     47         return [(d, d, d) for d in devices]
     48 
     49 elif plat[:6] == 'darwin':   # OS X (confirmed)
     50     from serial.tools.list_ports_osx import comports
     51 
     52 elif plat[:6] == 'netbsd':   # NetBSD
     53     def comports():
     54         """scan for available ports. return a list of device names."""
     55         devices = glob.glob('/dev/dty*')
     56         return [(d, d, d) for d in devices]
     57 
     58 elif plat[:4] == 'irix':     # IRIX
     59     def comports():
     60         """scan for available ports. return a list of device names."""
     61         devices = glob.glob('/dev/ttyf*')
     62         return [(d, d, d) for d in devices]
     63 
     64 elif plat[:2] == 'hp':       # HP-UX (not tested)
     65     def comports():
     66         """scan for available ports. return a list of device names."""
     67         devices = glob.glob('/dev/tty*p0')
     68         return [(d, d, d) for d in devices]
     69 
     70 elif plat[:5] == 'sunos':    # Solaris/SunOS
     71     def comports():
     72         """scan for available ports. return a list of device names."""
     73         devices = glob.glob('/dev/tty*c')
     74         return [(d, d, d) for d in devices]
     75 
     76 elif plat[:3] == 'aix':      # AIX
     77     def comports():
     78         """scan for available ports. return a list of device names."""
     79         devices = glob.glob('/dev/tty*')
     80         return [(d, d, d) for d in devices]
     81 
     82 else:
     83     # platform detection has failed...
     84     sys.stderr.write("""\
     85 don't know how to enumerate ttys on this system.
     86 ! I you know how the serial ports are named send this information to
     87 ! the author of this module:
     88 
     89 sys.platform = %r
     90 os.name = %r
     91 pySerial version = %s
     92 
     93 also add the naming scheme of the serial ports and with a bit luck you can get
     94 this module running...
     95 """ % (sys.platform, os.name, serial.VERSION))
     96     raise ImportError("Sorry: no implementation for your platform ('%s') available" % (os.name,))
     97 
     98 # test
     99 if __name__ == '__main__':
    100     for port, desc, hwid in sorted(comports()):
    101         print "%s: %s [%s]" % (port, desc, hwid)
    102