Last active
January 3, 2016 12:09
-
-
Save tcr/8460625 to your computer and use it in GitHub Desktop.
coolio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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