[processor-sdk/performance-audio-sr.git] / tools / pyalpha / pyalpha / serial / tools / list_ports_posix.py
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
11 """\
12 The ``comports`` function is expected to return an iterable that yields tuples
13 of 3 strings: port name, human readable description and a hardware ID.
15 As currently no method is known to get the second two strings easily, they are
16 currently just identical to the port name.
17 """
19 import glob
20 import sys
21 import os
22 from serial.tools import list_ports_common
24 # try to detect the OS so that a device can be selected...
25 plat = sys.platform.lower()
27 if plat[:5] == 'linux': # Linux (confirmed) # noqa
28 from serial.tools.list_ports_linux import comports
30 elif plat[:6] == 'darwin': # OS X (confirmed)
31 from serial.tools.list_ports_osx import comports
33 elif plat == 'cygwin': # cygwin/win32
34 # cygwin accepts /dev/com* in many contexts
35 # (such as 'open' call, explicit 'ls'), but 'glob.glob'
36 # and bare 'ls' do not; so use /dev/ttyS* instead
37 def comports():
38 devices = glob.glob('/dev/ttyS*')
39 return [list_ports_common.ListPortInfo(d) for d in devices]
41 elif plat[:7] == 'openbsd': # OpenBSD
42 def comports():
43 devices = glob.glob('/dev/cua*')
44 return [list_ports_common.ListPortInfo(d) for d in devices]
46 elif plat[:3] == 'bsd' or plat[:7] == 'freebsd':
47 def comports():
48 devices = glob.glob('/dev/cua*[!.init][!.lock]')
49 return [list_ports_common.ListPortInfo(d) for d in devices]
51 elif plat[:6] == 'netbsd': # NetBSD
52 def comports():
53 """scan for available ports. return a list of device names."""
54 devices = glob.glob('/dev/dty*')
55 return [list_ports_common.ListPortInfo(d) for d in devices]
57 elif plat[:4] == 'irix': # IRIX
58 def comports():
59 """scan for available ports. return a list of device names."""
60 devices = glob.glob('/dev/ttyf*')
61 return [list_ports_common.ListPortInfo(d) for d in devices]
63 elif plat[:2] == 'hp': # HP-UX (not tested)
64 def comports():
65 """scan for available ports. return a list of device names."""
66 devices = glob.glob('/dev/tty*p0')
67 return [list_ports_common.ListPortInfo(d) for d in devices]
69 elif plat[:5] == 'sunos': # Solaris/SunOS
70 def comports():
71 """scan for available ports. return a list of device names."""
72 devices = glob.glob('/dev/tty*c')
73 return [list_ports_common.ListPortInfo(d) for d in devices]
75 elif plat[:3] == 'aix': # AIX
76 def comports():
77 """scan for available ports. return a list of device names."""
78 devices = glob.glob('/dev/tty*')
79 return [list_ports_common.ListPortInfo(d) for d in devices]
81 else:
82 # platform detection has failed...
83 import serial
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:
89 sys.platform = {!r}
90 os.name = {!r}
91 pySerial version = {}
93 also add the naming scheme of the serial ports and with a bit luck you can get
94 this module running...
95 """.format(sys.platform, os.name, serial.VERSION))
96 raise ImportError("Sorry: no implementation for your platform ('{}') available".format(os.name))
98 # test
99 if __name__ == '__main__':
100 for port, desc, hwid in sorted(comports()):
101 print("{}: {} [{}]".format(port, desc, hwid))