简体   繁体   中英

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.

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);

This method of reading is what the MSDN articles used as well, but it's not providing me any results. 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. 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:

    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. I have reported the bug, it has been acknowledged by Microsoft and should be fixed with the next update of Visual Studio 2015.

Edit: Good video going into lots of detail - https://channel9.msdn.com/Events/Build/2013/2-924b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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