Implemented USB HID Test app.

Implemented listing devices, connecting to devices.
Newly connected devices get marked green in the listing.
master
Jonas Arnold 3 years ago
parent a030ff909e
commit 10a0b1612e
  1. 6
      ProtocolTests.sln
  2. 177
      UsbHidTestApp/Program.cs
  3. 14
      UsbHidTestApp/UsbHidTestApp.csproj

@ -5,6 +5,8 @@ VisualStudioVersion = 17.5.33424.131
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerialTestApp", "SerialTestApp\SerialTestApp.csproj", "{25FE0910-9C8A-44CA-BE72-5FED8CD92E2E}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerialTestApp", "SerialTestApp\SerialTestApp.csproj", "{25FE0910-9C8A-44CA-BE72-5FED8CD92E2E}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UsbHidTestApp", "UsbHidTestApp\UsbHidTestApp.csproj", "{39995F3A-1305-4053-9A9C-3432C1FF25B6}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{25FE0910-9C8A-44CA-BE72-5FED8CD92E2E}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -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<HidDevice> 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<HidDevice> 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) { }
}
}
}

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="hidlibrary" Version="3.3.40" />
</ItemGroup>
</Project>
Loading…
Cancel
Save