【问题标题】:Why can't I connect to my USB Composite Device with HIDSharp?为什么我无法使用 HIDSharp 连接到我的 USB 复合设备?
【发布时间】:2019-05-24 16:00:09
【问题描述】:

我正在开展一个项目,该项目使用微处理器模拟由 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# .net dll hid


    【解决方案1】:

    所以我在 HIDSharp 论坛上询问了这个问题,我得到了一个answer from the dev

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

    【讨论】:

    • 啊,听起来很合理。有一个原始输入 API,可让您(例如)确定几个键盘中的哪一个正在生成特定事件,但我不知道您是否获得 USB HID 页面格式的数据。
    【解决方案2】:

    是的...搜索了几天...找到了您的帖子。我的老板,巫师,向我指出了一个相当优雅的(低级)解决方案。你必须注册 windows 钩子并捕捉击键。你必须有创意来确定它是否是你的输入,但至少你可以在没有焦点的情况下捕捉/吃击键......

    【讨论】:

      猜你喜欢
      • 2014-12-28
      • 1970-01-01
      • 2013-08-06
      • 2018-09-06
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      相关资源
      最近更新 更多