Simple way to query connected USB devices info in Python?

157,301

Solution 1

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)

Solution 2

If you are working on windows, you can use pywin32 (old link: see update below).

I found an example here:

import win32com.client

wmi = win32com.client.GetObject ("winmgmts:")
for usb in wmi.InstancesOf ("Win32_USBHub"):
    print usb.DeviceID

Update Apr 2020:

'pywin32' release versions from 218 and up can be found here at github. Current version 227.

Solution 3

For a system with legacy usb coming back and libusb-1.0, this approach will work to retrieve the various actual strings. I show the vendor and product as examples. It can cause some I/O, because it actually reads the info from the device (at least the first time, anyway.) Some devices don't provide this information, so the presumption that they do will throw an exception in that case; that's ok, so we pass.

import usb.core
import usb.backend.libusb1

busses = usb.busses()
for bus in busses:
    devices = bus.devices
    for dev in devices:
        if dev != None:
            try:
                xdev = usb.core.find(idVendor=dev.idVendor, idProduct=dev.idProduct)
                if xdev._manufacturer is None:
                    xdev._manufacturer = usb.util.get_string(xdev, xdev.iManufacturer)
                if xdev._product is None:
                    xdev._product = usb.util.get_string(xdev, xdev.iProduct)
                stx = '%6d %6d: '+str(xdev._manufacturer).strip()+' = '+str(xdev._product).strip()
                print stx % (dev.idVendor,dev.idProduct)
            except:
                pass

Solution 4

For linux, I wrote a script called find_port.py which you can find here: https://github.com/dhylands/usb-ser-mon/blob/master/usb_ser_mon/find_port.py

It uses pyudev to enumerate all tty devices, and can match on various attributes.

Use the --list option to show all of the know USB serial ports and their attributes. You can filter by VID, PID, serial number, or vendor name. Use --help to see the filtering options.

find_port.py prints the /dev/ttyXXX name rather than the /dev/usb/... name.

Share:
157,301
DivinesLight
Author by

DivinesLight

Traversing digital plane as real life is full of deceit.

Updated on July 09, 2022

Comments

  • DivinesLight
    DivinesLight almost 2 years

    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.

  • DivinesLight
    DivinesLight over 12 years
    I'll try it in a while and get back
  • DivinesLight
    DivinesLight over 12 years
    Qasim simple lsubs gives me such info: Bus 002 Device 002: ID 413c:1003 Dell Computer Corp. Keyboard Hub Bus 002 Device 003: ID 413c:2010 Dell Computer Corp. Keyboard Which is missing one of most important things I need Actual file name onto which it's mapped (ex: /dev/ttyACM0)
  • errordeveloper
    errordeveloper over 12 years
    you can write a udev rule and make a symlink /dev/ttyACM<x> -> /dev/mydevice, if you are to package this - no problem, just put the rule into the package (make sure the filename will not clash or echo the rule into an exiting file in post-install script). Otherwise you can query it using usbfs (usually /proc/bus/usb/) or a udev command (not sure what it was called).
  • DivinesLight
    DivinesLight over 12 years
    Awesome code, thanks. Works without depending on any external lib and gives me what I want. Exactly. Thanks.
  • DivinesLight
    DivinesLight over 12 years
    Mate can you please clarify one thing? I get following info: ` /dev/bus/usb/004/002 22b8:4902 Motorola PCS Triplet GSM Phone (AT)` Which is of the device I want to connect, but When I do with gammu I get this: gammu.ERR_DEVICEOPENERROR: {'Text': u'Error opening device. Unknown, busy or no permissions.', 'Code': 2, 'Where': 'Init'} Any idea (I have tried running script with sudo)?
  • meson10
    meson10 over 12 years
    Hi, I have very little experience with Gammu, but can you pastebin your configuration file ? Let me have a look.
  • DivinesLight
    DivinesLight over 12 years
    Gammu is not so relevant here, I just want to know that when i plug my GSM Modem dmesg shows it gets registered as an ACM device and is attached to /dev/ttyACM0 port, now from that port the connection to Modem is flawless. however what lsubs returns is "/dev/bus...../002" and I can't connect to that device. Are you sure that using that device with longer path should work like using it from /dev/ttyACM0 path?
  • mjp
    mjp about 8 years
    This works great. Is there a way to get the actual device user-given name (e.g. 'Ben's USB') and not just the hardwired name (e.g. Apple, Inc. Optical USB Mouse [Mitsumi])?
  • Francesco Mantovani
    Francesco Mantovani over 7 years
    great code @meson10, BTW it seems not considering "\n" and it prints all in the same line. How to fix this?
  • meson10
    meson10 over 7 years
    @FrancescoMantovani Hi, I had done this some 5 years ago and things might have changed since then. I can re-visit it, if you would want.
  • Francesco Mantovani
    Francesco Mantovani over 7 years
    @meson10 yes, that would be great. I've tried to do that by myself but it didn't worked :( . I'm curious to see where I was wrong
  • meson10
    meson10 over 7 years
    @FrancescoMantovani Can you share your code as a Gist or something? I will take it up from there.
  • priya
    priya almost 5 years
    in windows ,I have tried with your code but lsusb is not supported. I am getting the issue and can u help me with solution of lsusb?
  • JoeyC
    JoeyC over 4 years
    @priya does this answer help? stackoverflow.com/a/8110666/1335473