1

出于测试目的,我想连接一个 USB 设备并想检查速度是多少(HS/FS/LS)。我可以访问设备描述符、端点描述符、接口描述符,但我想知道操作系统分配的设备地址(windows 7)

到目前为止我的代码:

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)

返回:

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

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

在非常有用的 USBview 软件中,有一段:

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

我如何获得这些信息?是否使用 pyUSB 对 USB 设备进行查询?还是对 sys 的查询?

谢谢你的帮助。

4

3 回答 3

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.

于 2013-08-14T07:52:19.303 回答
0

您可以使用此补丁https://github.com/DeliangFan/pyusb/commit/a882829859cd6ef3c91ca11870937dfff93fea9d通过 pyUSB 获取 USB 设备速度信息。

因为libusb1.0已经支持获取usb速度信息。

于 2015-01-09T03:09:13.947 回答
0

这些属性(现在)很容易访问。至少它对我有用。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)
于 2018-10-11T13:42:42.787 回答