11 si, so = os.popen4(
' '.join(argv))
12 return so.read().strip()
14 raise IOError(
'lsusb failed')
18 return subprocess.check_output(argv, stderr=subprocess.STDOUT).strip()
20 raise IOError(
'lsusb failed')
30 plat = sys.platform.lower()
33 """help function to read a single line from a file. returns none"""
36 line = f.readline().strip()
43 """search for regexp in text, return 1st group on match"""
44 m = re.search(regexp, text)
45 if m:
return m.group(1)
48 if plat[:5] ==
'linux':
53 """given a path to a usb device in sysfs, return a string describing it"""
54 bus, dev = os.path.basename(os.path.realpath(sysfs_path)).split(
'-')
57 snr_txt =
' SNR=%s' % (snr,)
60 return 'USB VID:PID=%s:%s%s' % (
67 bus, dev = os.path.basename(os.path.realpath(sysfs_path)).split(
'-')
69 desc =
popen([
'lsusb',
'-v',
'-s',
'%s:%s' % (bus, dev)])
71 iManufacturer =
re_group(
'iManufacturer\s+\w+ (.+)', desc)
72 iProduct =
re_group(
'iProduct\s+\w+ (.+)', desc)
73 iSerial =
re_group(
'iSerial\s+\w+ (.+)', desc)
or ''
75 idVendor =
re_group(
'idVendor\s+0x\w+ (.+)', desc)
76 idProduct =
re_group(
'idProduct\s+0x\w+ (.+)', desc)
78 return '%s %s %s' % (iManufacturer
or idVendor, iProduct
or idProduct, iSerial)
84 Get a human readable description.
85 For USB-Serial devices try to run lsusb to get a human readable description.
86 For USB-CDC devices read the description from sysfs.
88 base = os.path.basename(device)
90 sys_dev_path =
'/sys/class/tty/%s/device/driver/%s' % (base, base)
91 if os.path.exists(sys_dev_path):
92 sys_usb = os.path.dirname(os.path.dirname(os.path.realpath(sys_dev_path)))
95 sys_dev_path =
'/sys/class/tty/%s/device/interface' % (base,)
96 if os.path.exists(sys_dev_path):
101 """Try to get a HW identification using sysfs"""
102 base = os.path.basename(device)
103 if os.path.exists(
'/sys/class/tty/%s/device' % (base,)):
105 sys_id_path =
'/sys/class/tty/%s/device/id' % (base,)
106 if os.path.exists(sys_id_path):
109 sys_dev_path =
'/sys/class/tty/%s/device/driver/%s' % (base, base)
110 if os.path.exists(sys_dev_path):
111 sys_usb = os.path.dirname(os.path.dirname(os.path.realpath(sys_dev_path)))
114 if base.startswith(
'ttyACM'):
115 sys_dev_path =
'/sys/class/tty/%s/device' % (base,)
116 if os.path.exists(sys_dev_path):
121 devices = glob.glob(
'/dev/ttyS*') + glob.glob(
'/dev/ttyUSB*') + glob.glob(
'/dev/ttyACM*')
124 elif plat ==
'cygwin':
126 devices = glob.glob(
'/dev/com*')
127 return [(d, d, d)
for d
in devices]
129 elif plat ==
'openbsd3':
131 devices = glob.glob(
'/dev/ttyp*')
132 return [(d, d, d)
for d
in devices]
134 elif plat[:3] ==
'bsd' or \
135 plat[:7] ==
'freebsd' or \
136 plat[:7] ==
'openbsd':
139 devices = glob.glob(
'/dev/cuad*')
140 return [(d, d, d)
for d
in devices]
142 elif plat[:6] ==
'darwin':
144 """scan for available ports. return a list of device names."""
145 devices = glob.glob(
'/dev/tty.*')
146 return [(d, d, d)
for d
in devices]
148 elif plat[:6] ==
'netbsd':
150 """scan for available ports. return a list of device names."""
151 devices = glob.glob(
'/dev/dty*')
152 return [(d, d, d)
for d
in devices]
154 elif plat[:4] ==
'irix':
156 """scan for available ports. return a list of device names."""
157 devices = glob.glob(
'/dev/ttyf*')
158 return [(d, d, d)
for d
in devices]
160 elif plat[:2] ==
'hp':
162 """scan for available ports. return a list of device names."""
163 devices = glob.glob(
'/dev/tty*p0')
164 return [(d, d, d)
for d
in devices]
166 elif plat[:5] ==
'sunos':
168 """scan for available ports. return a list of device names."""
169 devices = glob.glob(
'/dev/tty*c')
170 return [(d, d, d)
for d
in devices]
172 elif plat[:3] ==
'aix':
174 """scan for available ports. return a list of device names."""
175 devices = glob.glob(
'/dev/tty*')
176 return [(d, d, d)
for d
in devices]
180 sys.stderr.write(
"""\
181 don't know how to enumerate ttys on this system.
182 ! I you know how the serial ports are named send this information to
183 ! the author of this module:
187 pySerial version = %s
189 also add the naming scheme of the serial ports and with a bit luck you can get
190 this module running...
191 """ % (sys.platform, os.name, serial.VERSION))
192 raise ImportError(
"Sorry: no implementation for your platform ('%s') available" % (os.name,))
195 if __name__ ==
'__main__':
196 for port, desc, hwid
in sorted(
comports()):
197 print "%s: %s [%s]" % (port, desc, hwid)