点亮大容量存储的LED

1 投票
1 回答
1549 浏览
提问于 2025-04-16 18:50

我有一个带LED灯的USB大容量存储设备。

我想控制LED灯的开和关。

我使用了一个叫USBlyzer的工具来抓取USB数据包,

可以获取到原始数据。

这些数据是: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。

这些数据的请求信息是“批量传输”或“中断传输”,输入输出是“输出”。

在USB属性部分,

我可以获取到一些信息,比如:

端点描述符 81 1 输入,批量,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写了一个代码。

完整的源代码在这里:

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 个回答

2

我对pyusb不太了解,但我理解这个错误信息的意思是,和其他人的看法不同,cfg 其实不是一个整数,而是需要一个非整数的索引。我这么说是因为这个异常是在一个叫 __getitem__ 的函数里抛出的,而这个函数很可能是 cfg 自己的 __getitem__,因为在那行代码里,只有在这个地方才会调用 __getitem__

interface_number = cfg[0].bInterfaceNumber

如果 cfg 是一个整数的话,它就不会有 __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)]

撰写回答