点亮大容量存储的 LED
我有带有 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 In,批量,512 字节
bDescriptorType 05h Endpoint
bEndpointAddress 81h 1 In
端点描述 符之类的信息02 2 In,bulk,512 bytes
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
出现了
问题用我的代码?
如果有任何错误的概念,请告诉我,
谢谢!
I have usb mass stroage with led
I am trying to light on and off the led
using usb packet sniffing tool USBlyzer,
I can get the raw data
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
whose Request info is Bulk or Interrupt Transfer and I/O is out
and in the USB properties section
I can get the info such as
Endpoint Descriptor 81 1 In, bulk, 512 bytes
bDescriptorType 05h Endpoint
bEndpointAddress 81h 1 In
Endpoint Descriptor 02 2 In, bulk, 512 bytes
bDescriptorType 05h Endpoint
bEndpointAddress 02h 2 Out
I made a python code with python 2.7, libusb-win32-bin-1.2.4.0, pyusb-1.0.0-a1
the full source is here
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)
but when I try to execute it,
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
arise
what's wrong with my code?
any if anything wrong concept is there, please let me know
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 pyusb 一无所知,但我对错误消息的解释是,与其他人的观点相反, cfg 不是整数,但它需要非整数索引。我这样说是因为异常是在
__getitem__
函数中抛出的,该函数只能是cfg
的__getitem__
,因为这是唯一一个__getitem__
的地方。 code>__getitem__ 调用将在该行中进行。现在,如果
cfg
是 int,则它不会有__getitem__
。问题是cfg
的__getitem__
似乎期望能够为它接收的index
下标,如中间两个参数所示,索引[0],索引[1]
。由于您向 cfg 传递了一个整数,所以这是不可能的。来自教程:
I don't know anything about pyusb, but my interpretation of the error message is that, contra others' opinions,
cfg
is not an integer, but that it requires a non-integral index. I say this because the exception is thrown in a__getitem__
function, which could only becfg
's__getitem__
, because that's the only place a__getitem__
call would be made in the lineNow if
cfg
were an int, it wouldn't have a__getitem__
. The problem is thatcfg
's__getitem__
seems to expect to be able to subscript theindex
that it receives, as illustrated by the middle two arguments,index[0], index[1]
. Since you passedcfg
an integer, that's impossible.From the tutorial: