Get USB device address through python

纵然是瞬间 提交于 2021-01-28 09:01:04

问题


For test purposes, I want to connect a USB device and want to check what is the speed (HS/FS/LS). I am able to access to Device Descriptor, Endpoint descriptor, interface descriptor but I would like to know the device address which has been allocated by the OS (windows 7)

My code so far :

import usb
busses = usb.busses()
for bus in busses:
    for dev in bus.devices:
        if dev.idVendor == vendor_id and dev.idProduct == product_id:
            print ("Test vehicle %s device FOUND!" %protocol)
            print ("iManufacturer   : %s" %usb.util.get_string(dev.dev, 256, 1))
            print ("iProduct            : %s" %usb.util.get_string(dev.dev, 256, 2))
            print ("iSerialNumber   : %s" %usb.util.get_string(dev.dev, 256, 3))

            return dev

print ("Test vehicle %s device NOT FOUND!" %protocol)

Returns :

C:\Python27\Lib\site-packages>python example.py

Test vehicle HS device FOUND!
iManufacturer   : Kingston
iProduct        : DataTraveler 2.0
iSerialNumber   : 5B720A82364A

In the very useful USBview software, there is a section :

ConnectionStatus: DeviceConnected
Current Config Value: 0x01
Device Bus Speed:     High
Device Address:       0x09
Open Pipes:              2

How do I get these informations ? is it a query to the USB device using pyUSB ? or is it a query to sys ?

Thanks for any help.


回答1:


There are several more fields available in the device objects (in your code these are named dev).

A quick and dirty way to look at them

def print_internals(dev):
    for attrib in dir(dev):
        if not attrib.startswith('_') and not attrib == 'configurations':
            x=getattr(dev, attrib)
            print "  ", attrib, x
    for config in dev.configurations:
        for attrib in dir(config):
            if not attrib.startswith('_'):
                x=getattr(config, attrib)
                print "    ", attrib, x

And call it within your "for dev in bus.devices" loop. It looks like the filename might correspond to 'device address', though bus speed is a bit deeper in (dev.configurations[i].interfaces[j][k].interfaceProtocol), and this only has an integer. usb.util might be able to provide you more information based on those integers, but I don't have that module available to me.

Documentation for pyUSB doesn't seem to be very extensive, but this SO question points at the libusb docs which it wraps up.




回答2:


You can get usb device speed information by pyUSB with this patch https://github.com/DeliangFan/pyusb/commit/a882829859cd6ef3c91ca11870937dfff93fea9d.

Because libusb1.0 has already support to get usb speed information.




回答3:


These attributes are (nowadays) easily accessible. At least it works for me. https://github.com/pyusb/pyusb/blob/master/usb/core.py

import usb.core

devices = usb.core.find(find_all=True)

dev = next(devices)

print("device bus:", dev.bus)
print("device address:", dev.address)
print("device port:", dev.port_number)
print("device speed:", dev.speed)


来源:https://stackoverflow.com/questions/18224189/get-usb-device-address-through-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!