Skip to content

Instantly share code, notes, and snippets.

@tcr
Last active January 3, 2016 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcr/8460625 to your computer and use it in GitHub Desktop.
Save tcr/8460625 to your computer and use it in GitHub Desktop.
coolio
import os
from os.path import join
"""find_tty_usb('067b', '2302') -> '/dev/ttyUSB0'"""
# Note: if searching for a lot of pairs, it would be much faster to search
# for the enitre lot at once instead of going over all the usb devices
# each time.
for dnbase in os.listdir('/sys/bus/usb/devices'):
dn = join('/sys/bus/usb/devices', dnbase)
if not os.path.exists(join(dn, 'idVendor')):
continue
idv = open(join(dn, 'idVendor')).read().strip()
# if idv != idVendor:
# continue
idp = open(join(dn, 'idProduct')).read().strip()
#if idp != idProduct:
# continue
print(idv, idp)
for subdir in os.listdir(dn):
if subdir.startswith(dnbase+':'):
for subsubdir in os.listdir(join(dn, subdir)):
# print('--->', subsubdir)
if subsubdir.startswith('ttyUSB'):
print('-->', subsubdir);
if subsubdir.startswith('tty'):
print('-->', subsubdir);
# return join('/dev', subsubdir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment