0

我正在使用 pywinusb 从自定义 HID 设备发送/获取数据。我可以成功发送数据但无法获取。有什么建议么?我尝试使用 Microsoft 消息分析器进行调试,可以看到那里的数据,但看不到脚本中的数据。产品/供应商 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()
4

1 回答 1

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

我希望这可以有一些用处。

于 2017-02-08T00:42:34.247 回答