首页 > 解决方案 > 为什么我无法使用 HIDSharp 连接到我的 USB 复合设备?

问题描述

我正在开发一个项目,该项目使用微处理器模拟由 HID 键盘和 HID 鼠标组成的 USB 复合设备。我的设备可以正确枚举并与我的 Windows 7 x64 和 Raspbian 主机一起运行,一切看起来都不错,但我遇到问题的地方是让我的 winforms 应用程序(使用 HidSharp)打开连接的复合设备,这样我就可以获取键盘端点中的原始数据。

问题似乎与 TryOpen() 函数有关,因为我可以通过匹配 VID 和 PID 找到连接的设备,我分配设备信息和报告描述符,但是当我尝试通过 TryOpen() 打开数据流时失败,我不知道为什么。不幸的是,该函数只返回一个布尔值,所以我不知道它为什么失败,只是它无法打开数据流。我想知道打开一个我不知道的复合设备是否有什么好笑的?我查找设备和打开数据流的代码如下:

/*These vars are part of the class*/
byte[] keyboardBuffer;  //EP1
HidSharp.Reports.Input.HidDeviceInputReceiver InputReceiver;
HidSharp.Reports.ReportDescriptor KeyboardRptDescriptor;
HidStream KeyboardStream;
HidDevice KeyboardDevice;

private void FindDevice()
{
    var list = DeviceList.Local;
    var stopwatch = Stopwatch.StartNew();
    var hidDeviceList = list.GetHidDevices().ToArray();

    foreach (HidDevice d in hidDeviceList)
    {

        if (d.VendorID == 0x0000 && d.ProductID == 0xA0A0)
        {
            /*Proper VID and PID Found*/
            if (d.GetProductName() == "Keyboard")
            {
                KeyboardDevice = d;
                KeyboardRptDescriptor = KeyboardDevice.GetReportDescriptor();

            }

        }

    }

    if (KeyboardDevice != null)
    {
        /*Device Found, open the datastream*/
        if (KeyboardDevice.TryOpen(out KeyboardStream))    //PROBLEM LINE - Always False?
        {
            KeyboardReport = KeyboardRptDescriptor.InputReports.FirstOrDefault();
            keyboardBuffer = new byte[KeyboardDevice.GetMaxInputReportLength()];
            InputParser = KeyboardReport.DeviceItem.CreateDeviceItemInputParser();
            InputReceiver = KeyboardRptDescriptor.CreateHidDeviceInputReceiver();
            InputReceiver.Received -= new EventHandler(HidInputReceived);
            InputReceiver.Received += new EventHandler(HidInputReceived);
            InputReceiver.Start(KeyboardStream);
        } else {
            rtb_hidLog.AppendText("Unable to connect to device\r\n");
        }


    }
    else
    {
        rtb_hidLog.AppendText("No Device Found\r\n");
    }

}

现在我只尝试从 HID 键盘读取,一旦我整理键盘就会添加鼠标。找到设备似乎没有问题,但为什么打开它会给我这样的问题?我的 HIDSharp 库似乎是 v2.0.2.0(根据文件属性)。

在此先感谢您的任何建议!

标签: c#.netdllhid

解决方案


所以我在 HIDSharp 论坛上询问了这个问题,我得到了开发人员的回答

原来 Windows 不允许您将 HID 键盘设备作为安全“功能”打开,因此 HIDSharp 将始终无法打开 HID 键盘设备的数据流。


推荐阅读