2011-06-02 78 views
1

我有帶LED光了海量存儲的領導

我想點亮和關閉使用USB數據包嗅探工具USBlyzer的領導

USB大容量stroage,

我可以得到原數據

其請求信息是批量或中斷反FER和I/O停止

,並在USB屬性部分

我可以得到信息,如

端點描述符81 1在,本體512個字節

bDescriptorType 05h Endpoint 

bEndpointAddress 81h 1 In 

端點描述符02 2 In,批量,512字節

bDescriptorType 05h Endpoint 

bEndpointAddress 02h 2 Out 

我用pytho製作了一個python代碼ñ2.7的libusb-win32的彬1.2.4.0,pyusb-1.0.0-A1

完整的源代碼是在這裏

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 

出現

我的代碼有什麼問題?

如果有任何錯誤的概念是有的,請讓我知道

謝謝!

+0

cfg是一個'int'。 'dev.get_active_configuration()'返回一個整數 – tMC 2011-06-02 15:31:30

+1

但是''cfg [0]'不會拋出異常,它會拋出'index [0]'。看到我的答案。 – senderle 2011-06-02 15:43:01

回答

2

我對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] 

正如你所看到的該指數是基於零的。可是等等!有 東西在我訪問 的接口方式怪異......是的,你是對的, 在 配置下標操作者接受的 兩個項目的序列,第一個是 接口的索引, 第二個,備用設置。所以, 要訪問第一個接口,但其0​​第二個替代設置,我們寫入 cfg [(0,1)]。

+0

官方文檔建議迭代對象,例如'for cff:'中的intf。 http://pyusb.sourceforge.net/docs/1.0/tutorial.html – ulidtko 2011-06-02 15:46:35

+0

@ulidtko,是的,我看到了,但我希望能有更詳細的類參考來確認我的猜測。該教程是唯一可用的文檔嗎? – senderle 2011-06-02 15:59:53

+0

恐怕是這樣。我也找不到任何類別參考。儘管如此,正如您所指出的那樣,本教程包含有關索引的解釋:接口索引必須是一對整數。 – ulidtko 2011-06-02 16:07:42