我有带led 的usb大容量存储
我正在尝试打开和关闭led
使用usb数据包嗅探工具USBlyzer、
我可以得到原始数据
55 53 42 43 58 66 93 88 00 00 00 00 06 00 00 00 000 00 00 00 0 00 00 00 01 00 00 00
其请求信息为批量或中断传输,并且I/O在之外
以及在USB属性部分中
我可以获得等信息
端点描述符81 1 In,批量,512字节
bDescriptorType 05h Endpoint
bEndpointAddress 81h 1 In
端点描述符02 2 In,批量,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 Settingskty1104Desktopusb2.py", line 14, in <module>
interface_number = cfg[0].bInterfaceNumber
File "C:Python27libsite-packagesusbcore.py", line 447, in __getitem__
return Interface(self.device, index[0], index[1], self.index)
TypeError: 'int' object is not subscriptable
出现
我的代码出了什么问题?
如果有任何错误的概念,请告诉我
谢谢!
我对pyusb一无所知,但我对错误消息的解释是,与其他人的意见相反,cfg
不是整数,但它需要一个非整数索引。我这么说是因为异常是在__getitem__
函数中抛出的,它只能是cfg
的__getitem__
,因为这是在行中进行__getitem__
调用的唯一位置
interface_number = cfg[0].bInterfaceNumber
现在,如果cfg
是一个int,它就不会有__getitem__
。问题是cfg
的__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)]。