【发布时间】: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,它将帮助您了解正在访问的内容。