【问题标题】:What does dev[0][(0,0)][0] mean?dev[0][(0,0)][0] 是什么意思?
【发布时间】:2016-06-03 15:52:28
【问题描述】:

我正在实现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]

我从 [this] (http://www.usbmadesimple.co.uk/ums_3.htm) 网站了解了端点的含义,但仍然不明白 [0][(0,0)[0] 是什么。更改它也会产生错误。 pyusb 文档/教程也没有多大帮助

编辑

正如 Martin Evans 在下面的 cmets 中所建议的,我在 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,我猜因为我的代码中有 interface = 0,只有上面的行很重要。

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

【问题讨论】:

  • 尝试添加print dev,它将帮助您了解正在访问的内容。

标签: python pyusb


【解决方案1】:

the PyUSB tutorial:

你也可以使用下标操作符随机访问描述符,像这样:

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

如您所见,索引是从零开始的。可是等等!我访问接口的方式有些奇怪......是的,你是对的,配置中的下标运算符接受两个项目的序列,第一个是接口的索引,第二个是备用环境。因此,要访问第一个界面,但要访问它的第二个备用设置,我们编写 cfg[(0,1)]。

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

【讨论】:

    猜你喜欢
    • 2015-03-20
    • 2018-09-10
    • 2020-01-07
    • 2014-11-06
    • 2018-09-25
    • 2017-10-29
    • 2011-05-25
    • 2010-11-20
    • 1970-01-01
    相关资源
    最近更新 更多