【问题标题】:Get USB device address through python通过python获取USB设备地址
【发布时间】:2013-08-15 23:02:52
【问题描述】:

出于测试目的,我想连接一个 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 的查询?

感谢您的帮助。

【问题讨论】:

    标签: python pyusb


    【解决方案1】:

    这些属性(现在)很容易访问。至少它对我有用。 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)
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        设备对象中还有几个可用的字段(在您的代码中,这些字段被命名为dev)。

        一种快速而肮脏的方式来看待它们

        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
        

        并在“for dev in bus.devices”循环中调用它。看起来文件名可能对应于“设备地址”,尽管总线速度在 (dev.configurations[i].interfaces[j][k].interfaceProtocol) 中有点深,而且它只有一个整数。 usb.util 可能能够根据这些整数为您提供更多信息,但我没有该模块可供我使用。

        pyUSB 的文档似乎不是很广泛,但这个 SO question 指向它结束的 libusb docs

        【讨论】:

        • 感谢您的回复。文件名不会返回任何内容,但因为我主要关心的是获得速度,所以我可能会深入研究界面,看看我能找到什么。如果我能找到一个,我会在这里发布解决方案:-)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-18
        • 2010-12-17
        • 2016-02-23
        • 2018-06-09
        • 1970-01-01
        • 2015-12-26
        相关资源
        最近更新 更多