python - 点亮大容量存储器的 LED

标签 python io usb pyusb

我有带 LED 的 USB 大容量存储

我正在尝试打开和关闭 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 输入,批量,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

出现

我的代码有什么问题吗?

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

谢谢!

最佳答案

我对 pyusb 一无所知,但我对错误消息的解释是,与其他人的观点相反,cfg不是整数,但它需要非整数索引。我这样说是因为异常是在 __getitem__ 中抛出的。函数,只能是 cfg__getitem__ ,因为这是唯一一个 __getitem__ 的地方将在该行中进行调用

interface_number = cfg[0].bInterfaceNumber

现在如果cfg是一个 int,它不会有 __getitem__ 。问题是cfg__getitem__似乎期望能够下标 index正如中间两个参数所示,它接收 index[0], index[1] 。自从你通过cfg一个整数,这是不可能的。

<小时/>

来自 tutorial :

You can also use the subscript operator to access the descriptors randomly, like that:

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

As you can see, the index is zero based. But wait! There is something weird in the way I access an interface... Yes, you are right, the subscript operator in the Configuration accepts a sequence of two items, with the first one being the index of the Interface and the second one, the alternate setting. So, to access the first interface, but its second alternate setting, we write cfg[(0,1)].

关于python - 点亮大容量存储器的 LED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6216505/

相关文章:

python - 如何在不改变其形状的情况下取消嵌套(unlevel)不必要的嵌套列表? (Python)

java - 解析 Java 程序的 Python 配置文件

kill 命令后关闭文件

c - 在 C 中,解析由多个空格分隔的整数组成的字符串

ubuntu - vmware ubuntu : error usb 4-1: stat urb: status -32 when connecting external drive

Python:sys.exc_info() 缺少局部变量

python - 求平截头体的总表面积,公式有问题

java - 缓冲读取器 : read and save certain parts of a line with HashMap (in java? )

linux - 使用Linux可启动USB闪存驱动器可以用来规避勒索软件吗?

c++ - 从USB存储器中检索序列号(Windows环境c++)