2017-01-09 126 views
0

我正在使用pywinusb從自定義HID設備發送/獲取數據。我可以成功發送數據,但不能得到它。有什麼建議麼?我試圖使用Microsoft Message Analyzer進行調試,並可以在那裏看到數據,但不能在腳本中看到。產品/供應商ID,報告ID等都是正確的。在pywinusb中獲取輸入報告

這裏的代碼,

from pywinusb import hid 
from time import sleep 

devicefilter = hid.HidDeviceFilter(vendor_id=0x0483, product_id=0x572A) 

devices = devicefilter.get_devices() 
print ("devices:", devices) 

hid_device = devices[0] 
print ("hid_device:", hid_device) 

hid_device.open() 
out_report = hid_device.find_output_reports() 
in_report = hid_device.find_input_reports() 
print("out_report:", out_report) 
print("out_report[0]:",out_report[0]) 
print("in_report:", in_report) 
print("in_report[0]:",in_report[0]) 

txBuffer = [0x55] * 64 
txBuffer[0] = 0x01 # Report ID 
rxBuffer = [0x00] * 64 
rxBuffer[0] = 0x02 # Report ID 
print(txBuffer) 
print(rxBuffer) 
out_report[0].set_raw_data(txBuffer) 
in_report[0].set_raw_data(rxBuffer) 
while 1: 
    out_report[0].send() 
    rxBuffer = in_report[0].get() 
    print("rxBuffer:", rxBuffer) 
    sleep(1) 
hid_device.close() 

回答

0

我對這個我自己不是專家,但我有一個類似的探測應用這是我做的。我沒有明確創建輸入報告,而是將輸入報告處理程序附加到USB接收緩衝區。

import pywinusb.hid as hid 

# handler called when a report is received 
def rx_handler(data): 
    print 'recv: ', data 

def scan_hiddevice(): 
    """ Scans for and returns the HID device. """ 
    devices = hid.HidDeviceFilter(vendor_id = vendor_id).get_devices() 
    if not devices: 
     print "scan_hiddevice: No device connected." 
     return None 
    else: 
     device = devices[0] 
     #print("scan_hiddevice: found %s", device) 
     return device  
    return None 

def setup_hiddevice(): 
    """Creates a new HID device, opens it and attaches a receive data handler""" 
    hid_device = scan_hiddevice() 
    hid_device.open() 
    hid_device.set_raw_data_handler(rx_handler) 
    return hid_device 

def main(verbose=True): 
    hid_device = setup_hiddevice() 

    while (True): 
     #wait for data 

我希望這可以有一些用處。