在pywinusb中获取输入报告

时间:2017-01-09 21:52:41

标签: pywinusb

我正在使用pywinusb从Custom HID设备发送/获取数据。我可以成功发送数据,但无法得到它。有什么建议?我尝试使用Microsoft Message Analyzer进行调试,可以在那里查看数据,但不能在脚本中查看。产品/供应商ID,报告ID等是正确的。

这里是代码,

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()

1 个答案:

答案 0 :(得分: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

我希望这可以有所帮助。