1

我有带led的usb mass stroage

我正在尝试打开和关闭 LED

使用 USB 数据包嗅探工具 USBlyzer,

我可以得到原始数据

55 53 42 43 58 66 93 88 00 00 00 00 00 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

其请求信息为批量或中断传输且 I/O 已输出

在 USB 属性部分

我可以获得诸如

端点描述符 81 1 In,批量,512 字节

bDescriptorType 05h Endpoint

bEndpointAddress 81h 1 In

端点描述符 02 2 输入,批量,512 字节

bDescriptorType 05h Endpoint

bEndpointAddress 02h 2 Out

我用 python 2.7、libusb-win32-bin-1.2.4.0、pyusb-1.0.0-a1 做了一个 python 代码

完整的来源在这里

import usb.core
import usb.util

# find our device
dev = usb.core.find(idVendor=0x1516, idProduct=0x8628)

# was it found?
if dev is None:
    raise ValueError('Device not found')

dev.set_configuration()
# get an endpoint instance
cfg = dev.get_active_configuration()
interface_number = cfg[0].bInterfaceNumber
alternate_setting = usb.control.get_interface(interface_number)
intf = usb.util.find_descriptor(cfg, bInterfaceNumber = \
                                ineterface_number, bAlternateSetting = alternate_setting)
ep = usb.util.find_descriptor(intf,custom_match = \
                                  lambda e: \
                                      usb.util.endpoint_direction(e.bEndpointAddress) == \
                                      usb.util.ENDPOINT_OUT)
# set the active configuration. With no arguments, the first
# configuration will be the active one


assert ep is not None

ep.write(0x2,0x55)
ep.write(0x2,0x53)
ep.write(0x2,0x42)
ep.write(0x2,0x43)
ep.write(0x2,0x58)
ep.write(0x2,0x66)
ep.write(0x2,0x93)
ep.write(0x2,0x88)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x06)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)
ep.write(0x2,0x00)

但是当我尝试执行它时,

Traceback (most recent call last):
  File "C:\Documents and Settings\kty1104\Desktop\usb2.py", line 14, in <module>
    interface_number = cfg[0].bInterfaceNumber
  File "C:\Python27\lib\site-packages\usb\core.py", line 447, in __getitem__
    return Interface(self.device, index[0], index[1], self.index)
TypeError: 'int' object is not subscriptable

出现

我的代码有什么问题?

如果有任何错误的概念,请告诉我

谢谢!

4

1 回答 1

2

我对pyusb一无所知,但我对错误消息的解释是,与其他人的意见相反,cfg它不是整数,而是它需要一个非整数索引。我这样说是因为异常是在一个__getitem__函数中引发的,该函数只能是cfg's __getitem__,因为这__getitem__是在该行中进行调用的唯一位置

interface_number = cfg[0].bInterfaceNumber

现在,如果cfg是一个 int,它就没有__getitem__. 问题是cfg's__getitem__似乎期望能够下标index它收到的 ,如中间两个参数所示,index[0], index[1]. 既然你传递cfg了一个整数,那是不可能的。


教程

您还可以使用下标运算符随机访问描述符,如下所示:

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

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

于 2011-06-02T15:42:01.097 回答