using HidLibrary; using System.Text; namespace UsbHidTestApp; internal class Program { private static HidDevice _device; private static bool _continue; private static Thread readThread; private static List lastDevices; public static bool applicationQuitRequested { get; private set; } static void Main(string[] args) { while (applicationQuitRequested == false) { Console.WriteLine($"----- USB HID PROTOCOL TEST APP -----"); Console.WriteLine($"Command list:"); Console.WriteLine($"l = list devices"); Console.WriteLine($"c = connect"); Console.WriteLine($"q = quit"); var key = Console.ReadKey(); switch (key.Key) { // list devices case ConsoleKey.L: List hidDevices = HidDevices.Enumerate().ToList(); Console.WriteLine($"Following HID devices were found: {Environment.NewLine}"); foreach (var device in hidDevices) { bool newDevice = false; if(lastDevices?.Where(x => x.DevicePath == device.DevicePath).Any() == false) { Console.ForegroundColor = ConsoleColor.Green; newDevice = true; } Console.WriteLine($"-- {device}"); Console.WriteLine($" {device.Description}"); if (newDevice) { Console.ForegroundColor = ConsoleColor.White; } } lastDevices = hidDevices; break; // connect case ConsoleKey.C: Console.Write($"Write device vendor id as hex: "); string? vendorIdString = Console.ReadLine(); int vendorId; if (vendorIdString == null || vendorIdString == "" || Int32.TryParse(vendorIdString, System.Globalization.NumberStyles.HexNumber, null, out vendorId) == false) { Console.WriteLine($"Invalid vendor ID entered."); break; } Console.Write($"Write product vendor id as hex: "); string? productIdString = Console.ReadLine(); int productId; if (productIdString == null || productIdString == "" || Int32.TryParse(productIdString, System.Globalization.NumberStyles.HexNumber, null, out productId) == false) { Console.WriteLine($"Invalid product ID entered."); break; } var devices = HidDevices.Enumerate(vendorId, productId); _device = devices.FirstOrDefault();// Where(x => x.DevicePath.Contains("&mi_00")).FirstOrDefault(); if (_device == null) { Console.WriteLine("Could not find device."); break; } _device.OpenDevice(); _device.Inserted += DeviceAttachedHandler; _device.Removed += DeviceRemovedHandler; _continue = true; readThread = new Thread(Read); readThread.Start(); // not supported apparently: //_device.MonitorDeviceEvents = true; //_device.ReadReport(OnReport); Console.WriteLine("Connected to device. Press End key to end communication."); while (Console.ReadKey().Key != ConsoleKey.End) { } _continue = false; readThread.Join(); readThread = null; _device?.CloseDevice(); Console.WriteLine("Closed connection to device."); break; // quit case ConsoleKey.Q: applicationQuitRequested = true; break; default: Console.WriteLine($"{Environment.NewLine}Did not recognize key.. {Environment.NewLine}"); break; } } } private static void DeviceRemovedHandler() { Console.WriteLine("Device removed."); } private static void DeviceAttachedHandler() { Console.WriteLine("Device attached."); _device.ReadReport(OnReport); } private static void OnReport(HidReport report) { if (!_device.IsConnected) { return; } var data = report.Data; Console.WriteLine(Encoding.ASCII.GetString(data)); _device.ReadReport(OnReport); } public static void Read() { while (_continue) { try { var readData = _device.Read(); if (readData != null) { string print = Encoding.UTF8.GetString(readData.Data); //print = print.Replace("\r", "").Replace("\n", ""); // Newline Mode: none //print = print.Replace("\r", ""); // Newline Mode: only print \n //print = print.Replace("\n", ""); // Newline Mode: only print \r Console.Write(print); } } catch (TimeoutException) { } } } }