diff --git a/ProtocolTests.sln b/ProtocolTests.sln index c0b6b7a..3c09c0a 100644 --- a/ProtocolTests.sln +++ b/ProtocolTests.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.5.33424.131 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerialTestApp", "SerialTestApp\SerialTestApp.csproj", "{25FE0910-9C8A-44CA-BE72-5FED8CD92E2E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UsbHidTestApp", "UsbHidTestApp\UsbHidTestApp.csproj", "{39995F3A-1305-4053-9A9C-3432C1FF25B6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {25FE0910-9C8A-44CA-BE72-5FED8CD92E2E}.Debug|Any CPU.Build.0 = Debug|Any CPU {25FE0910-9C8A-44CA-BE72-5FED8CD92E2E}.Release|Any CPU.ActiveCfg = Release|Any CPU {25FE0910-9C8A-44CA-BE72-5FED8CD92E2E}.Release|Any CPU.Build.0 = Release|Any CPU + {39995F3A-1305-4053-9A9C-3432C1FF25B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {39995F3A-1305-4053-9A9C-3432C1FF25B6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {39995F3A-1305-4053-9A9C-3432C1FF25B6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {39995F3A-1305-4053-9A9C-3432C1FF25B6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/UsbHidTestApp/Program.cs b/UsbHidTestApp/Program.cs new file mode 100644 index 0000000..9ef29df --- /dev/null +++ b/UsbHidTestApp/Program.cs @@ -0,0 +1,177 @@ + + +using HidLibrary; +using System.Security.Cryptography.X509Certificates; +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) { } + } + } + + +} \ No newline at end of file diff --git a/UsbHidTestApp/UsbHidTestApp.csproj b/UsbHidTestApp/UsbHidTestApp.csproj new file mode 100644 index 0000000..9092a10 --- /dev/null +++ b/UsbHidTestApp/UsbHidTestApp.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + +