forked from Kiv/poclbm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ioutil.py
36 lines (31 loc) · 796 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
from glob import glob
from serial.tools import list_ports
from detect import LINUX, WINDOWS
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:
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)
return ports