Permalink
Fetching contributors…
Cannot retrieve contributors at this time
40 lines (35 sloc) 883 Bytes
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