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 0开发者_运维知识库5h 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!
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)].
精彩评论