点亮大容量存储的 LED

发布于 2024-11-13 08:04:39 字数 2614 浏览 3 评论 0原文

我有带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甩你一脸翔 2024-11-20 08:04:39

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

interface_number = cfg[0].bInterfaceNumber

现在,如果 cfg 是 int,则它不会有 __getitem__。问题是 cfg__getitem__ 似乎期望能够为它接收的 index 下标,如中间两个参数所示,索引[0],索引[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)]。

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 be cfg's __getitem__, because that's the only place a __getitem__ call would be made in the line

interface_number = cfg[0].bInterfaceNumber

Now if cfg were an int, it wouldn't have a __getitem__. The problem is that cfg's __getitem__ seems to expect to be able to subscript the index that it receives, as illustrated by the middle two arguments, index[0], index[1]. Since you passed cfg an integer, that's impossible.


From the 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)].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文