-
Notifications
You must be signed in to change notification settings - Fork 385
/
ioutil.py
40 lines (35 loc) · 883 Bytes
/
ioutil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from detect import LINUX, WINDOWS
from glob import glob
def find_udev(check, product_id):
ports = []
if LINUX:
try:
import pyudev
context = pyudev.Context()
for device in context.list_devices(subsystem='tty', ID_MODEL=product_id):
if check(device.device_node):
ports.append(device.device_node)
except ImportError:
pass
return ports
def find_serial_by_id(check, product_id):
ports = []
if LINUX:
for port in glob('/dev/serial/by-id/*' + product_id + '*'):
if check(port):
ports.append(port)
return ports
def find_com_ports(check):
ports = []
if WINDOWS:
from serial.tools import list_ports
com_ports = [p[0] for p in list_ports.comports()]
com_ports.sort()
for port in com_ports:
if check(port, False):
ports.append(port)
else:
for port in glob('/dev/ttyUSB*'):
if check(port):
ports.append(port)
return ports