Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to query connected USB devices info in Python?

Tags:

python

usb

libusb

How can we query connected USB devices info in Python? I want to get UID Device Name (ex: SonyEricsson W660), path to device (ex: /dev/ttyACM0)

And also what would be the best Parameter out of above info to be used as identifying the device whenever it's connected again? (UID?)

I am working on Ubuntu 11.04.

ATM I have this code (using pyUSB)

busses = usb.busses() for bus in busses:   devices = bus.devices   for dev in devices:     print repr(dev)     print "Device:", dev.filename     print "  idVendor: %d (0x%04x)" % (dev.idVendor, dev.idVendor)     print "  idProduct: %d (0x%04x)" % (dev.idProduct, dev.idProduct)     print "Manufacturer:", dev.iManufacturer     print "Serial:", dev.iSerialNumber     print "Product:", dev.iProduct 

The problem is I don't get desired output, will paste one example:

<usb.legacy.Device object at 0x1653990> Device:    idVendor: 4046 (0x0fce)   idProduct: 53411 (0xd0a3) Manufacturer: 1 Serial: 3 Product: 2 

First I don't get filename, it's most important to me. I am assuming it is the /dev/ttyACM0 etc part. Second, I guess there was some UID of every USB device, or I should use both Vendor or Product id?

EDIT: Apparently I have some setup issues, I think I am using wrong USB Library. (using libusb0.1) ATM. That's why I get Device (dev.filename) string empty. If someone can please just tell that on what operating system he is using what USB Library and what version of PyUSB I think it will solve my problems.

like image 814
DivinesLight Avatar asked Nov 13 '11 07:11

DivinesLight


People also ask

How do you reveal detailed information about USB devices?

You can use the dmesg command to find out more information about the connected USB devices. The last connected USB device is the easiest to find with dmesg command. It is more widely used for debugging purpose.

How do I check USB connection history?

If you don't know the name of your computer, go to Settings > System > About. Your computer name is shown at the top. Click the Start button to see the USB history. You can then expand the results to see details such as the time and date it was last used.


1 Answers

I can think of a quick code like this.

Since all USB ports can be accessed via /dev/bus/usb/< bus >/< device >

For the ID generated, even if you unplug the device and reattach it [ could be some other port ]. It will be the same.

import re import subprocess device_re = re.compile("Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I) df = subprocess.check_output("lsusb") devices = [] for i in df.split('\n'):     if i:         info = device_re.match(i)         if info:             dinfo = info.groupdict()             dinfo['device'] = '/dev/bus/usb/%s/%s' % (dinfo.pop('bus'), dinfo.pop('device'))             devices.append(dinfo) print devices 

Sample output here will be:

[ {'device': '/dev/bus/usb/001/009', 'tag': 'Apple, Inc. Optical USB Mouse [Mitsumi]', 'id': '05ac:0304'}, {'device': '/dev/bus/usb/001/001', 'tag': 'Linux Foundation 2.0 root hub', 'id': '1d6b:0002'}, {'device': '/dev/bus/usb/001/002', 'tag': 'Intel Corp. Integrated Rate Matching Hub', 'id': '8087:0020'}, {'device': '/dev/bus/usb/001/004', 'tag': 'Microdia ', 'id': '0c45:641d'} ] 

Code Updated for Python 3

import re import subprocess device_re = re.compile(b"Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I) df = subprocess.check_output("lsusb") devices = [] for i in df.split(b'\n'):     if i:         info = device_re.match(i)         if info:             dinfo = info.groupdict()             dinfo['device'] = '/dev/bus/usb/%s/%s' % (dinfo.pop('bus'), dinfo.pop('device'))             devices.append(dinfo)              print(devices) 
like image 113
meson10 Avatar answered Sep 23 '22 09:09

meson10