用于窗口的 HID 终端

HID terminal for windows

本文关键字:终端 HID 窗口 用于      更新时间:2023-10-16

我已经为我的STM32设备开发了固件。在此设备中,我通过此接口为发送/接收字符串定义了一个自定义HID接口。我的USB接口描述向量:

/**** Descriptor of CUSTOM HID interface ****/
0x09, /*bLength: Interface Descriptor size*/
0x04, /*bDescriptorType: Interface descriptor type*/
0x00, /*bInterfaceNumber: Number of Interface*/
0x00, /*bAlternateSetting: Alternate setting*/
0x02, /*bNumEndpoints*/
0x03, /*bInterfaceClass: CUSTOM_HID*/
0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
0x00, /*iInterface: Index of string descriptor*/
/**** Descriptor of CUSTOM_HID ****/
0x09, /*bLength: CUSTOM_HID Descriptor size*/
0x21, /*bDescriptorType: CUSTOM_HID*/
0x11, /*bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number*/
0x01,
0x00, /*bCountryCode: Hardware target country*/
0x01, /*bNumDescriptors: Number of CUSTOM_HID class descriptors to follow*/
0x22, /*bDescriptorType*/
0x33,/*wItemLength: Total length of Report descriptor*/
0x00,
/**** Descriptor of Custom HID endpoints ****/
0x07, /* bLength: Endpoint Descriptor size */
0x05, /* bDescriptorType: */
0x04, /*bEndpointAddress: Endpoint Address (OUT)*/
0x03, /* bmAttributes: Interrupt endpoint */
0x40, /* wMaxPacketSize (64 bytes) */
0x00,
0x0A, /* bInterval: Polling Interval (10 ms) */
0x07, /*bLength: Endpoint Descriptor size*/
0x05, /*bDescriptorType:*/
0x81, /*bEndpointAddress: Endpoint Address (IN)*/
0x03, /*bmAttributes: Interrupt endpoint*/
0x40, /*wMaxPacketSize (64 bytes) */
0x00,
0x0A, /*bInterval: Polling Interval (10 ms)*/

我已经在 Linux 上测试了我的设备发送/接收字符串,它工作得很好(一个简单的回显字符串到 hidraw 设备(。 现在我正在尝试在Windows中开发一个简单的程序(尝试使用v7x64和v10x64(。我已经测试了很多库:

  • (Python( pywinusb
  • (C#( HIDSharp
  • (C++(日田比项目
  • (实用(逸

在我的测试中,我发送了这样的消息:

0x00 (Report ID)
0x41 (Report payload) (A)
...

但是在我的所有测试中,我发现并非所有消息都发送到设备。例如,在设备收到消息之前,我需要发送 10 次或更多次相同的字符串。 如何改善 Windows 和设备之间的通信?

经过大量调试,我发现了问题。

在STM32库上,可以定义HID缓冲区大小

#define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE ...

Unix系统省略了对这个缓冲区大小的控制,但Windows没有! 就我而言,我定义了 1024 字节的缓冲区(在 USB FS 版本中(。当我尝试发送消息时,Windows 发送 64 字节的块,并且在缓冲区未满之前无法从 USB 接收任何字节。

使用正确的值 64 字节更改缓冲区大小后,通信在 Windows 上也能很好地工作。