2018-03-29 21:39:43 +00:00
|
|
|
# coding=utf-8
|
2010-09-13 03:53:46 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
DEV_TREE = '/dev'
|
|
|
|
USB_DEVICE_TREE = '/sys/bus/usb/devices'
|
|
|
|
|
2018-03-29 21:39:43 +00:00
|
|
|
|
2010-09-13 03:53:46 +00:00
|
|
|
def findEiBotBoards():
|
2018-03-30 04:45:11 +00:00
|
|
|
"""Find only those USB devices that declare themselves to be EiBotBoards"""
|
2010-09-13 03:53:46 +00:00
|
|
|
|
2018-03-30 04:45:11 +00:00
|
|
|
# find all USB devices whose product name is 'EiBotBoard'
|
|
|
|
with os.popen('fgrep -l EiBotBoard %s/*/product' % USB_DEVICE_TREE) as pipe:
|
|
|
|
for path in [os.path.split(path)[0] for path in pipe.readlines()]:
|
|
|
|
device = os.path.split(path)[1]
|
2010-09-13 03:53:46 +00:00
|
|
|
|
2018-03-30 04:45:11 +00:00
|
|
|
# for each device endpoint ...
|
|
|
|
for dir in os.listdir(path):
|
|
|
|
if dir.startswith(device):
|
2010-09-13 03:53:46 +00:00
|
|
|
|
2018-03-30 04:45:11 +00:00
|
|
|
# find the endpoint that supports tty access
|
|
|
|
ttydir = os.path.join(USB_DEVICE_TREE, device, dir, 'tty')
|
|
|
|
if os.path.exists(ttydir):
|
2010-09-13 03:53:46 +00:00
|
|
|
|
2018-03-30 04:45:11 +00:00
|
|
|
# And emit each (the) interface name
|
|
|
|
for ttyname in os.listdir(ttydir):
|
|
|
|
yield os.path.join(DEV_TREE, ttyname)
|
2018-03-29 21:39:43 +00:00
|
|
|
|
2010-09-13 03:53:46 +00:00
|
|
|
|
|
|
|
def findPorts():
|
2018-03-30 04:45:11 +00:00
|
|
|
for device in os.listdir(DEV_TREE):
|
|
|
|
if not device.startswith('ttyACM'):
|
|
|
|
continue
|
|
|
|
yield os.path.join(DEV_TREE, device)
|
2018-03-29 21:39:43 +00:00
|
|
|
|
2010-09-13 03:53:46 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-03-30 04:45:11 +00:00
|
|
|
print("Looking for EiBotBoards")
|
|
|
|
for port in findEiBotBoards():
|
|
|
|
print(" ", port)
|
2010-09-13 03:53:46 +00:00
|
|
|
|
2018-03-30 04:45:11 +00:00
|
|
|
print("Looking for COM ports")
|
|
|
|
for port in findPorts():
|
|
|
|
print(" ", port)
|