Windows10:C# 中的 HID 通信

Windows 10: HID Communication in C#

我正在尝试在 Windows10 上使用 C# 通过 USB HID 与 Arduino Leonardo 建立通信。我已经能够枚举和检索 HidDevice 对象,但我无法接收任何数据。

Package.appxmanifest:

<DeviceCapability Name="humaninterfacedevice">
    <Device Id="vidpid:16C0 0486">
      <Function Type="usage:FFAB 0200"/>
    </Device>
</DeviceCapability>

MainPage.xaml.cs

HidInputReport testReport = await device.GetInputReportAsync();

DataReader dataReader = DataReader.FromBuffer(testReport.Data);
byte[] fileContent = new byte[dataReader.UnconsumedBufferLength];
dataReader.ReadBytes(fileContent);

textBlock.Text += System.Text.Encoding.UTF8.GetString(fileContent);

MSDN的文章也是用这种阅读方法,但是没有给我任何结果。如果有人了解我可以做些什么或做错了什么,将不胜感激!

编辑: 只需在此处添加更多信息,我设置了一个事件以在接收到 InputReport 时触发,并且该事件在我发送消息的设定时间间隔内触发来自 Arduino,这让我相信它是正确的 packet/message/data。一个问题是这个数据总是空的,尽管我已经验证了一个实际的非零消息正在发送。

我终于弄明白了,但有几个问题。我设法让它工作的方式是上面的,但我的数据不是我最初预期的。找到我的设备后,我附加了一个看起来像这样的 InputReceived 事件:

    private void ControlDevice_InputReportReceived(HidDevice sender, HidInputReportReceivedEventArgs args)
    {
        HidInputReport inputReport = args.Report;
        IBuffer buffer = inputReport.Data;
        DataReader dr = DataReader.FromBuffer(buffer);
        byte[] bytes = new byte[inputReport.Data.Length];
        dr.ReadBytes(bytes);

        String receivedMessage = System.Text.Encoding.ASCII.GetString(bytes);
        handleRead_HID(receivedMessage);
    }

这应该会从您的设备中获取一条人类可读的字符串消息。

截至 2015 年 9 月 22 日的另外一点是,在将您的通用应用程序部署到商店时,您的 appxmanifest 文件会覆盖其内容,这意味着您部署的应用程序将无法访问您的设备。我已经报告了该错误,它已被 Microsoft 确认,应该会在 Visual Studio 2015 的下一次更新中修复。

编辑:非常详细的视频 - https://channel9.msdn.com/Events/Build/2013/2-924b