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

Windows下HID USB设备写入失败求助:Go代码报错解决方案

HID USB设备写入指令失败问题

我在操作HID USB设备时需要向其写入指令,使用SimpleHIDWrite工具可以成功完成,但自研Go应用调用设备接口时却返回“Invalid function”或“The parameter is incorrect”错误,更换多个USB/HID库后问题仍然存在。根据设备文档,指定的reportID为0x08,指令字节为0x02或0x00。

以下是我的Go测试代码:

package main

import (
    "fmt"
    "strings"

    "github.com/karalabe/usb"
)

func main() {
    // Enumerate all the HID devices in alphabetical path order
    hids, err := usb.EnumerateHid(0, 0)
    if err != nil {
        panic(err)
    }
    for i := 0; i < len(hids); i++ {
        for j := i + 1; j < len(hids); j++ {
            if hids[i].Path > hids[j].Path {
                hids[i], hids[j] = hids[j], hids[i]
            }
        }
    }

    var hidWanted usb.DeviceInfo

    for i, hid := range hids {
        fmt.Println(strings.Repeat("-", 128))
        fmt.Printf("HID #%d\n", i)
        fmt.Printf("  OS Path:      %s\n", hid.Path)
        fmt.Printf("  Vendor ID:    %#04x\n", hid.VendorID)
        fmt.Printf("  Product ID:   %#04x\n", hid.ProductID)
        fmt.Printf("  Release:      %d\n", hid.Release)
        fmt.Printf("  Serial:       %s\n", hid.Serial)
        fmt.Printf("  Manufacturer: %s\n", hid.Manufacturer)
        fmt.Printf("  Product:      %s\n", hid.Product)
        fmt.Printf("  Usage Page:   %d\n", hid.UsagePage)
        fmt.Printf("  Usage:        %d\n", hid.Usage)
        fmt.Printf("  Interface:    %d\n", hid.Interface)

        if hid.VendorID == 0x22b6 && hid.ProductID == 0x0100 {
            hidWanted = hid
        }
    }
    fmt.Println(strings.Repeat("=", 128))

    dev, err := hidWanted.Open()
    if err == nil {
        b := make([]byte, 37)
        b[0] = 0x08 // report id
        b[1] = 0x02 // cmd
        ln, err := dev.Write(b)
        fmt.Println("Write result", ln, err)
    } else {
        fmt.Println("Cannot write to the device: ", hidWanted)
    }
}

附HID设备描述信息:设备报告描述符显示,报告ID 0x08对应的输出报告长度为36字节(不含报告ID本身),设备VendorID为0x22b6,ProductID为0x0100。

内容的提问来源于stack exchange,提问作者Roman Kazmin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 23:27:26