Created
September 28, 2014 18:10
-
-
Save DarranShepherd/f11efef7de934ae985dc to your computer and use it in GitHub Desktop.
C# USB HID example for mbed using hidlibrary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="hidlibrary" version="3.2.29.0" targetFramework="net45" /> | |
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace UsbHid | |
{ | |
using System; | |
using System.Linq; | |
using System.Threading; | |
using HidLibrary; | |
public class Program | |
{ | |
private const int VendorId = 0x1234; | |
private const int ProductId = 0x0006; | |
private const int ReportLength = 8; | |
private static readonly Random Random = new Random(); | |
private static HidDevice device; | |
public static void Main() | |
{ | |
device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault(); | |
if (device == null) | |
{ | |
Console.WriteLine("No device connected"); | |
return; | |
} | |
Console.WriteLine("mbed found"); | |
device.ReadReport(ReadReportCallback); | |
while (true) | |
{ | |
var data = new byte[ReportLength]; | |
Random.NextBytes(data); | |
device.WriteReport(new HidReport(ReportLength, new HidDeviceData(data, HidDeviceData.ReadStatus.Success))); | |
Thread.Sleep(TimeSpan.FromSeconds(0.2)); | |
} | |
} | |
private static void ReadReportCallback(HidReport report) | |
{ | |
Console.WriteLine("recv: {0}", string.Join(", ", report.Data.Select(b => b.ToString("X2")))); | |
device.ReadReport(ReadReportCallback); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://mbed.org/cookbook/USBHID-bindings- for the mbed side of things.