]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blob - tools/pyalpha/pyalpha/serial/tools/list_ports_linux.py
Add Debug, Release, and SDF build profiles. This allows normal build all and SDF...
[processor-sdk/performance-audio-sr.git] / tools / pyalpha / pyalpha / serial / tools / list_ports_linux.py
1 #!/usr/bin/env python
2 #
3 # This is a module that gathers a list of serial ports including details on
4 # GNU/Linux systems.
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 import glob
12 import os
13 from serial.tools import list_ports_common
16 class SysFS(list_ports_common.ListPortInfo):
17     """Wrapper for easy sysfs access and device info"""
19     def __init__(self, device):
20         super(SysFS, self).__init__(device)
21         self.name = os.path.basename(device)
22         self.usb_device_path = None
23         if os.path.exists('/sys/class/tty/{}/device'.format(self.name)):
24             self.device_path = os.path.realpath('/sys/class/tty/{}/device'.format(self.name))
25             self.subsystem = os.path.basename(os.path.realpath(os.path.join(self.device_path, 'subsystem')))
26         else:
27             self.device_path = None
28             self.subsystem = None
29         # check device type
30         if self.subsystem == 'usb-serial':
31             self.usb_device_path = os.path.dirname(os.path.dirname(self.device_path))
32         elif self.subsystem == 'usb':
33             self.usb_device_path = os.path.dirname(self.device_path)
34         else:
35             self.usb_device_path = None
36         # fill-in info for USB devices
37         if self.usb_device_path is not None:
38             self.vid = int(self.read_line(self.usb_device_path, 'idVendor'), 16)
39             self.pid = int(self.read_line(self.usb_device_path, 'idProduct'), 16)
40             self.serial_number = self.read_line(self.usb_device_path, 'serial')
41             self.location = os.path.basename(self.usb_device_path)
42             self.manufacturer = self.read_line(self.usb_device_path, 'manufacturer')
43             self.product = self.read_line(self.usb_device_path, 'product')
44             self.interface = self.read_line(self.device_path, 'interface')
46         if self.subsystem in ('usb', 'usb-serial'):
47             self.apply_usb_info()
48         #~ elif self.subsystem in ('pnp', 'amba'):  # PCI based devices, raspi
49         elif self.subsystem == 'pnp':  # PCI based devices
50             self.description = self.name
51             self.hwid = self.read_line(self.device_path, 'id')
52         elif self.subsystem == 'amba':  # raspi
53             self.description = self.name
54             self.hwid = os.path.basename(self.device_path)
56     def read_line(self, *args):
57         """\
58         Helper function to read a single line from a file.
59         One or more parameters are allowed, they are joined with os.path.join.
60         Returns None on errors..
61         """
62         try:
63             with open(os.path.join(*args)) as f:
64                 line = f.readline().strip()
65             return line
66         except IOError:
67             return None
70 def comports():
71     devices = glob.glob('/dev/ttyS*')           # built-in serial ports
72     devices.extend(glob.glob('/dev/ttyUSB*'))   # usb-serial with own driver
73     devices.extend(glob.glob('/dev/ttyACM*'))   # usb-serial with CDC-ACM profile
74     devices.extend(glob.glob('/dev/ttyAMA*'))   # ARM internal port (raspi)
75     devices.extend(glob.glob('/dev/rfcomm*'))   # BT serial devices
76     return [info
77             for info in [SysFS(d) for d in devices]
78             if info.subsystem != "platform"]    # hide non-present internal serial ports
80 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81 # test
82 if __name__ == '__main__':
83     for port, desc, hwid in sorted(comports()):
84         print("{}: {} [{}]".format(port, desc, hwid))