【问题标题】:light up the mass storage's led点亮大容量存储器的 LED
【发布时间】:2011-06-02 15:15:46
【问题描述】:

我有带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 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 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

出现

我的代码有什么问题?

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

谢谢!

【问题讨论】:

  • cfg 是一个intdev.get_active_configuration() 正在返回一个整数
  • 但异常不是在cfg[0] 处引发,而是在index[0] 处引发。看我的回答。

标签: python io usb pyusb


【解决方案1】:

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

interface_number = cfg[0].bInterfaceNumber

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


来自tutorial

你也可以使用下标 操作员访问描述符 随机的,像这样:

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

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

【讨论】:

  • 官方文档建议迭代对象,如for intf in cfg:pyusb.sourceforge.net/docs/1.0/tutorial.html
  • @ulidtko,是的,我看到了,但我希望有更详细的类参考来证实我的猜测。教程是唯一可用的文档吗?
  • 恐怕是。我也找不到任何类参考。不过,正如您所指出的,本教程包含有关索引的说明:接口索引必须是一对整数。
猜你喜欢
  • 2018-06-16
  • 1970-01-01
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多