You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows通过HidLibrary.dll能否向STM32 USB HID(键盘+自定义)设备发数据包?

问题:STM32F103C8T6复合HID设备自定义功能数据发送方式

我正在使用STM32F103C8T6开发一款USB复合设备,它可以被识别为键盘向Windows发送按键码,同时支持厂商自定义HID来收发自定义数据。目前仅键盘功能正常,自定义功能无法工作。

以下是设备相关描述符代码:

static __code const unsigned char _USB_HID_Custom_ReportDecriptor_Data[] = {
/*Usage Page (vendor define),                        */ 0x06, 0x00, 0xFF,
/*Usage (id 1),                                      */ 0x09, 1,
/*Collection (Application),                          */ 0xA1, 0x01,
/*  Usage Minimum (1),                               */ 0x19, 1,
/*  Usage Maximum (1),                               */ 0x29, 1,
/*  Logical Minimum (0),                             */ 0x15, 0,
/*  Logical Maximum (255),                           */ 0x25, 255,
/*  Report Size (8),                                 */ 0x75, 8,
/*  Report Count (8),                                */ 0x95, 8,
/*  Input (Data, Variable, Absolute), ;Modifier byte */ 0x81, 0x02,
/*  Usage Minimum (1),                               */ 0x19, 1,
/*  Usage Maximum (1),                               */ 0x29, 1,
/*  Output(Data, Variable, Absolute), ;Modifier byte */ 0x91, 0x02,
/*End Collection                                     */ 0xC0,
};

static __code const unsigned char _USB_HID_Keyboard_ReportDecriptor_Data[] = {
/*Usage Page (Generic Desktop),                      */ 0x05, 0x01,
/*Usage (Keyboard),                                  */ 0x09, 0x06,
/*Collection (Application),                          */ 0xA1, 0x01,
/*  Usage Page (Key Codes);                          */ 0x05, 0x07,
/*  Report ID (0x01)                                 */ 0x85, 0x01,
/*  Usage Minimum (224),                             */ 0x19, 0xE0,
/*  Usage Maximum (231),                             */ 0x29, 0xE7,
/*  Logical Minimum (0),                             */ 0x15, 0x00,
/*  Logical Maximum (1),                             */ 0x25, 0x01,
/*  Report Size (1),                                 */ 0x75, 0x01,
/*  Report Count (8),                                */ 0x95, 0x08,
/*  Input (Data, Variable, Absolute), ;Modifier byte */ 0x81, 0x02,
/*  Usage Page (Key Codes),                          */ 0x05, 0x07,
/*  Usage Minimum (0),                               */ 0x19, 0x00,
/*  Usage Maximum (101),                             */ 0x29, 0x65,
/*  Logical Minimum (0),                             */ 0x15, 0x00,
/*  Logical Maximum(101),                            */ 0x25, 0x65,
/*  Report Count (6),                                */ 0x95, 0x06,
/*  Report Size (8),                                 */ 0x75, 0x08,
/*  Input (Data, Array), ;Key arrays (6 bytes)       */ 0x81, 0x00,
/*End Collection                                     */ 0xC0,
};
static __code const unsigned char _USB_DeviceDescriptor_Data[] = {
/*bLength                  */ 0x12,             //(18 bytes)
/*bDescriptorType          */ 0x01,             //(Device Descriptor)
/*bcdUSB                   */ 0x00, 0x02,       //(USB Version 2.0) -> but device is Low-Speed only
/*bDeviceClass             */ 0x00,             //(defined by the interface descriptors)
/*bDeviceSubClass          */ 0x00,
/*bDeviceProtocol          */ 0x00,
/*bMaxPacketSize0          */ 0x40,              //(64 bytes)
/*idVendor                 */ 'M', 'S',
/*idProduct                */ 0, 'D',
/*bcdDevice                */ 0x01, 0x00,
/*iManufacturer            */ 0x01,             //(String Descriptor 1)
/*iProduct                 */ 0x02,             //(String Descriptor 2)
/*iSerialNumber            */ 0x00,             //(No String Descriptor)
/*bNumConfigurations       */ 0x01,             //(1 Configuration)
};
static __code const unsigned char _USB_ConfigurationDescription_Data[] = {
/*bLength                  */ 0x09,             //(9 bytes)
/*bDescriptorType          */ 0x02,             //(Configuration Descriptor)
/*wTotalLength             */ 9   + 9 + 9 + 7   + 9 + 9 + 7, 0x00,
/*bNumInterfaces           */ 0x02,             //(2 Interfaces)
/*bConfigurationValue      */ 0x01,             //(Configuration 1)
/*iConfiguration           */ 0x00,             //(No String Descriptor)
/*bmAttributes             */ 0x00,
/*MaxPower                 */ 0x32,             //(100 mA)
//-----------------------------------
//-----------------------------------
//-----------------------------------
/*bLength                  */ 0x09,             //(9 bytes)
/*bDescriptorType          */ 0x04,             //(Interface Descriptor)
/*bInterfaceNumber         */ 0x00,             //(Interface 0)
/*bAlternateSetting        */ 0x00,
/*bNumEndpoints            */ 0x01,             //(1 Endpoint)
/*bInterfaceClass          */ 0x03,             //(HID - Human Interface Device)
/*bInterfaceSubClass       */ 0x01,             //(Boot Interface)
/*bInterfaceProtocol       */ 0x01,             //(Keyboard)
/*iInterface               */ 0x00,             //(No String Descriptor)
//-----------------------------------
/*bLength                  */ 0x09,             //(9 bytes)
/*bDescriptorType          */ 0x21,             //(HID Descriptor)
/*bcdHID                   */ 0x11, 0x01,       //(HID Version 1.11)
/*bCountryCode             */ 0x00,             //(00 = not localized)
/*bNumDescriptors          */ 0x01,
/*bDescriptorType          */ 0x22,             //(Class=Report)
/*wDescriptorLength        */ sizeof(_USB_HID_Keyboard_ReportDecriptor_Data), 0x00,
//-----------------------------------
/*bLength                  */ 0x07,             //(7 bytes)
/*bDescriptorType          */ 0x05,             //(Endpoint Descriptor)
/*bEndpointAddress         */ 0x81,             //(Direction=IN EndpointID=1)
/*bmAttributes             */ 0x03,             //(TransferType=Interrupt)
/*wMaxPacketSize           */ 0x08, 0x00,
/*bInterval                */ 0x0A,             //(10 ms)
//-----------------------------------
//-----------------------------------
//-----------------------------------
/*bLength                  */ 0x09,             //(9 bytes)
/*bDescriptorType          */ 0x04,             //(Interface Descriptor)
/*bInterfaceNumber         */ 0x01,             //(Interface 1)
/*bAlternateSetting        */ 0x00,
/*bNumEndpoints            */ 0x01,             //(1 Endpoint)
/*bInterfaceClass          */ 0x03,             //(HID - Human Interface Device)
/*bInterfaceSubClass       */ 0x00,             //(None)
/*bInterfaceProtocol       */ 0x00,             //(None)
/*iInterface               */ 0x00,             //(No String Descriptor)
//-----------------------------------
/*bLength                  */ 0x09,             //(9 bytes)
/*bDescriptorType          */ 0x21,             //(HID Descriptor)
/*bcdHID                   */ 0x11, 0x01,       //(HID Version 1.11)
/*bCountryCode             */ 0x00,             //(00 = not localized)
/*bNumDescriptors          */ 0x01,
/*bDescriptorType          */ 0x22,             //(Class=Report)
/*wDescriptorLength        */ sizeof(_USB_HID_Custom_ReportDecriptor_Data), 0x00,
//-----------------------------------
/*bLength                  */ 0x07,             //(7 bytes)
/*bDescriptorType          */ 0x05,             //(Endpoint Descriptor)
/*bEndpointAddress         */ 0x01,             //(Direction=OUT EndpointID=1)
/*bmAttributes             */ 0x03,             //(TransferType=Interrupt)
/*wMaxPacketSize           */ 0x08, 0x00,
/*bInterval                */ 0x0A,             //(10 ms)
};

单独使用自定义HID时功能可正常运行。

请问电脑需要通过何种方式向该STM32设备发送自定义数据?


内容的提问来源于stack exchange,提问作者Thạch Duy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 00:14:51