简体   繁体   English

Windows 10:C#中的HID通信

[英]Windows 10: HID Communication in C#

I'm attempting to establish communication over USB HID with an Arduino Leonardo using C# on Windows 10. I have been able to enumerate and retrieve the HidDevice object but I am unable to receive any data. 我试图在Windows 10上使用C#通过Arduino Leonardo通过USB HID建立通信。我已经能够枚举和检索HidDevice对象,但是我无法接收任何数据。

Package.appxmanifest: Package.appxmanifest:

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

MainPage.xaml.cs 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);

This method of reading is what the MSDN articles used as well, but it's not providing me any results. 这种阅读方法也是MSDN文章使用的方法,但是没有提供任何结果。 If anyone has insight in to what I could do different or am doing wrong it would be much appreciated! 如果有人对我可以做些什么或做错了什么有深刻的了解,将不胜感激!

EDIT: Just adding a bit more information here, I have setup an event to trigger upon receiving the InputReport, and the event triggers at the set interval that I am sending messages from the Arduino which leads me to believe it is the correct packet/message/data. 编辑:只是在这里添加更多的信息,我已经设置了一个事件,在接收到InputReport后触发,并且该事件以我从Arduino发送消息的设置间隔触发,这使我相信这是正确的数据包/消息/数据。 The one issue is that this data is always empty, despite me having verified that an actual non-zero message is being sent. 一个问题是,尽管我已验证正在发送实际的非零消息,但该数据始终为空。

I managed to figure this out finally and there are couple of quirks. 我终于弄清楚了这一点,并且有一些怪癖。 The way I managed to get this to work was with the above, but my data wasn't where I initially expected. 我设法使它起作用的方法就是上面的方法,但是我的数据并没有达到我最初的预期。 After finding my device, I attached an InputReceived event that looks something like this: 找到我的设备后,我附加了一个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);
    }

This should get you a human readable string message from your device. 这应该使您从设备获得一条人类可读的字符串消息。

An additional bit as of 9/22/2015 is that upon deploying your Universal App to the store, your appxmanifest file overwrites the contents of which means your deployed application will NOT have access to your device. 截至2015年9月22日的另外一点是,将通用应用程序部署到商店后,您的appxmanifest文件将覆盖其内容,这意味着您部署的应用程序将无法访问您的设备。 I have reported the bug, it has been acknowledged by Microsoft and should be fixed with the next update of Visual Studio 2015. 我已经报告了该错误,Microsoft已确认该错误,并且应该在Visual Studio 2015的下一个更新中予以修复。

Edit: Good video going into lots of detail - https://channel9.msdn.com/Events/Build/2013/2-924b 编辑:优质视频深入细节-https: //channel9.msdn.com/Events/Build/2013/2-924b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM