照亮大容量存储器

时间:2011-06-02 15:15:47

标签: python io usb pyusb

我使用led

进行了usb 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

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

并在USB属性部分

我可以获得诸如

之类的信息

端点描述符81 1 In,bulk,512 bytes

bDescriptorType 05h Endpoint

bEndpointAddress 81h 1 In

端点描述符02 2 In,bulk,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

出现

我的代码出了什么问题?

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

谢谢!

1 个答案:

答案 0 :(得分:2)

我对pyusb一无所知,但我对错误信息的解释是,与其他人的意见相反,cfg不是整数,而是需要非整数索引。我这样说是因为__getitem__函数抛出了异常,该函数只能是cfg的{​​{1}},因为这是__getitem__调用的唯一位置这条线

__getitem__

现在,如果interface_number = cfg[0].bInterfaceNumber 是一个int,则它不会有cfg。问题是__getitem__的{​​{1}}似乎希望能够下标它收到的cfg,如中间的两个参数__getitem__所示。由于你传递了index一个整数,这是不可能的。


来自tutorial

  

您也可以使用下标   运算符来访问描述符   随机,像那样:

index[0], index[1]
  

如您所见,索引为零。可是等等!那里   我访问的方式很奇怪   界面......是的,你是对的,   中的下标运算符   配置接受一系列   两个项目,第一个是   接口的索引和   第二个,备用设置。所以,   访问第一个界面,但它   第二个备用设置,我们写   CFG [(0,1)]。