首页 > 解决方案 > Python - 将数据帧发送到 HID 设备

问题描述

我正在尝试通过 Python 向我的 HID 设备发送帧。我可以连接到我的设备,但不能向它发送帧。下面是我要发送的帧示例(来自 Wireshark 的屏幕): 在此处输入图像描述

我在 Python 中的框架:

data = bytearray(64)
data[0] = 0x02
data[5] = 0x82
first_data = bytes(data)

它看起来像来自 Wireshark 的数据包。但它不起作用:

h.write(first_data)

我在 Wireshark 中看不到通讯。所以我发现了这样的东西:

def send_frame(data, device):
    data = bytearray(data)
    data_len = len(data)
    seq = 0
    idx = 0
    write = []
    while idx < data_len:
        if idx == 0:
            # INIT frame
            write = data[idx: idx + min(data_len, usb_report_size - 7)]
            device.write(b'\0' + struct.pack(">IBH", HWW_CID, HWW_CMD, data_len & 0xFFFF) + write + b'\xEE' * (usb_report_size - 7 - len(write)))
        else:
            # CONT frame
            write = data[idx: idx + min(data_len, usb_report_size - 5)]
            device.write(b'\0' + struct.pack(">IB", HWW_CID, seq) + write + b'\xEE' * (usb_report_size - 5 - len(write)))
            seq += 1
        idx += len(write)

我认为这可能对我有帮助,但是... HWW_CMD 是 USB 功能吗?在本例中:0x0009?什么是 HWW_CID 和 usb_report_side?或者也许有更好的选择将帧发送到设备?

标签: pythonframedevicesendhid

解决方案


推荐阅读