dev[0][(0,0)][0] 是什么意思?

What does dev[0][(0,0)][0] mean?

我正在执行 this 程序来从 usb 鼠标读取 usb 数据并显示它。该程序产生正确的输出。它也适用于其他 USB 设备。代码如下

import sys
import usb.core
import usb.util

dev=usb.core.find(idVendor=0x1c4f, idProduct=0x0032)
interface=0
endpoint = dev[0][(0,0)][0]
if dev.is_kernel_driver_active(interface) is True:
    dev.detach_kernel_driver(interface)
    usb.util.claim_interface(dev,interface)

collected = 0
attempts = 50
while collected < attempts:
    try:
        data = dev.read(endpoint.bEndpointAddress,endpoint.wMaxPacketSize)
        collected += 1
        print data
    except usb.core.USBError as e :
        data = None
        if e.args == ('Operation timed out',):
            continue
usb.util.release_interface(dev,interface)
dev.attach_kernel_driver(interface)

我无法理解以下几行

interface=0

为什么它需要等于零?更改它会产生错误。

下面这行是做什么的?

endpoint = dev[0][(0,0)][0]

我已经从[这个] (http://www.usbmadesimple.co.uk/ums_3.htm) 网站了解了端点的含义,但它仍然不明白 [0][(0,0)[0] 是什么。改变这个也会报错。 pyusb docs/tutorials 也没什么用

编辑

正如 Martin Evans 在下面的评论中所建议的,我在 dev = [0][(0,0)][0] 之后添加了 print dev 并且我得到了以下输出。

DEVICE ID 1c4f:0002 on Bus 002 Address 003 =================
 bLength                :   0x12 (18 bytes)
 bDescriptorType        :    0x1 Device
 bcdUSB                 :  0x110 USB 1.1
 bDeviceClass           :    0x0 Specified at interface
 bDeviceSubClass        :    0x0
 bDeviceProtocol        :    0x0
 bMaxPacketSize0        :    0x8 (8 bytes)
 idVendor               : 0x1c4f
 idProduct              : 0x0002
 bcdDevice              :  0x110 Device 1.1
 iManufacturer          :    0x1 SIGMACHIP
 iProduct               :    0x2 USB Keyboard
 iSerialNumber          :    0x0 
 bNumConfigurations     :    0x1
  CONFIGURATION 1: 98 mA ===================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x3b (59 bytes)
   bNumInterfaces       :    0x2
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0 
   bmAttributes         :   0xa0 Bus Powered, Remote Wakeup
   bMaxPower            :   0x31 (98 mA)
    INTERFACE 0: Human Interface Device ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x1
     bInterfaceClass    :    0x3 Human Interface Device
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x1
     iInterface         :    0x0 
      ENDPOINT 0x81: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x81 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :    0x8 (8 bytes)
       bInterval        :    0xa

我还有一些行,但我没有发布它们,因为它们属于接口 1,我猜是因为我的代码中有接口 = 0,所以只有上面的行很重要。

所以我认为第一个 [0] 对应于 bEndpointAddress , ([0,0]) 对应于 (bmAttributes, wMaxPacketSize) 最后一个 [0] 对应于 bInterval ?还是我错了?

根据 the PyUSB tutorial:

You can also use the subscript operator to access the descriptors randomly, like this:

>>> # access the second configuration
>>> cfg = dev[1]
>>> # access the first interface
>>> intf = cfg[(0,0)]
>>> # third endpoint
>>> ep = intf[2]

As you can see, the index is zero-based. But wait! There is something weird in the way I access an interface... Yes, you are right, the subscript operator in the Configuration accepts a sequence of two items, with the first one being the index of the Interface and the second one, the alternate setting. So, to access the first interface, but its second alternate setting, we write cfg[(0,1)].

因此,第一个下标[0]访问第一个配置,第二个下标[0,0]选择第一个接口(使用第一个备用设置),第三个下标[0]选择第一个端点。