• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# This is a module that gathers a list of serial ports on POSIXy systems.
4# For some specific implementations, see also list_ports_linux, list_ports_osx
5#
6# This file is part of pySerial. https://github.com/pyserial/pyserial
7# (C) 2011-2015 Chris Liechti <cliechti@gmx.net>
8#
9# SPDX-License-Identifier:    BSD-3-Clause
10
11"""\
12The ``comports`` function is expected to return an iterable that yields tuples
13of 3 strings: port name, human readable description and a hardware ID.
14
15As currently no method is known to get the second two strings easily, they are
16currently just identical to the port name.
17"""
18
19from __future__ import absolute_import
20
21import glob
22import sys
23import os
24from serial.tools import list_ports_common
25
26# try to detect the OS so that a device can be selected...
27plat = sys.platform.lower()
28
29if plat[:5] == 'linux':    # Linux (confirmed)  # noqa
30    from serial.tools.list_ports_linux import comports
31
32elif plat[:6] == 'darwin':   # OS X (confirmed)
33    from serial.tools.list_ports_osx import comports
34
35elif plat == 'cygwin':       # cygwin/win32
36    # cygwin accepts /dev/com* in many contexts
37    # (such as 'open' call, explicit 'ls'), but 'glob.glob'
38    # and bare 'ls' do not; so use /dev/ttyS* instead
39    def comports(include_links=False):
40        devices = glob.glob('/dev/ttyS*')
41        if include_links:
42            devices.extend(list_ports_common.list_links(devices))
43        return [list_ports_common.ListPortInfo(d) for d in devices]
44
45elif plat[:7] == 'openbsd':    # OpenBSD
46    def comports(include_links=False):
47        devices = glob.glob('/dev/cua*')
48        if include_links:
49            devices.extend(list_ports_common.list_links(devices))
50        return [list_ports_common.ListPortInfo(d) for d in devices]
51
52elif plat[:3] == 'bsd' or plat[:7] == 'freebsd':
53    def comports(include_links=False):
54        devices = glob.glob('/dev/cua*[!.init][!.lock]')
55        if include_links:
56            devices.extend(list_ports_common.list_links(devices))
57        return [list_ports_common.ListPortInfo(d) for d in devices]
58
59elif plat[:6] == 'netbsd':   # NetBSD
60    def comports(include_links=False):
61        """scan for available ports. return a list of device names."""
62        devices = glob.glob('/dev/dty*')
63        if include_links:
64            devices.extend(list_ports_common.list_links(devices))
65        return [list_ports_common.ListPortInfo(d) for d in devices]
66
67elif plat[:4] == 'irix':     # IRIX
68    def comports(include_links=False):
69        """scan for available ports. return a list of device names."""
70        devices = glob.glob('/dev/ttyf*')
71        if include_links:
72            devices.extend(list_ports_common.list_links(devices))
73        return [list_ports_common.ListPortInfo(d) for d in devices]
74
75elif plat[:2] == 'hp':       # HP-UX (not tested)
76    def comports(include_links=False):
77        """scan for available ports. return a list of device names."""
78        devices = glob.glob('/dev/tty*p0')
79        if include_links:
80            devices.extend(list_ports_common.list_links(devices))
81        return [list_ports_common.ListPortInfo(d) for d in devices]
82
83elif plat[:5] == 'sunos':    # Solaris/SunOS
84    def comports(include_links=False):
85        """scan for available ports. return a list of device names."""
86        devices = glob.glob('/dev/tty*c')
87        if include_links:
88            devices.extend(list_ports_common.list_links(devices))
89        return [list_ports_common.ListPortInfo(d) for d in devices]
90
91elif plat[:3] == 'aix':      # AIX
92    def comports(include_links=False):
93        """scan for available ports. return a list of device names."""
94        devices = glob.glob('/dev/tty*')
95        if include_links:
96            devices.extend(list_ports_common.list_links(devices))
97        return [list_ports_common.ListPortInfo(d) for d in devices]
98
99else:
100    # platform detection has failed...
101    import serial
102    sys.stderr.write("""\
103don't know how to enumerate ttys on this system.
104! I you know how the serial ports are named send this information to
105! the author of this module:
106
107sys.platform = {!r}
108os.name = {!r}
109pySerial version = {}
110
111also add the naming scheme of the serial ports and with a bit luck you can get
112this module running...
113""".format(sys.platform, os.name, serial.VERSION))
114    raise ImportError("Sorry: no implementation for your platform ('{}') available".format(os.name))
115
116# test
117if __name__ == '__main__':
118    for port, desc, hwid in sorted(comports()):
119        print("{}: {} [{}]".format(port, desc, hwid))
120