removed old implementation of usb hid test app,

replaced with libusb implementation,
added skipping first two bytes
master
Jonas Arnold 3 years ago
parent 6ce31a7bb7
commit 122d083b34
  1. 16
      ProtocolTests.sln
  2. 118
      UsbHidTestApp/Program.cs
  3. 11
      UsbHidTestApp/UsbHidTestApp.csproj
  4. 0
      UsbHidTestApp/hidapi.dll
  5. 0
      UsbHidTestApp/hidapi.lib
  6. 178
      UsbHidTestApp_libusb/Program.cs
  7. 26
      UsbHidTestApp_libusb/UsbHidTestApp_libusb.csproj

@ -5,13 +5,11 @@ VisualStudioVersion = 17.5.33424.131
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerialTestApp", "SerialTestApp\SerialTestApp.csproj", "{25FE0910-9C8A-44CA-BE72-5FED8CD92E2E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UsbHidTestApp", "UsbHidTestApp\UsbHidTestApp.csproj", "{39995F3A-1305-4053-9A9C-3432C1FF25B6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UdpTestApp", "UdpTestApp\UdpTestApp.csproj", "{97751007-78CC-4356-9F17-F118BA9297FB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TcpTestApp", "TcpTestApp\TcpTestApp.csproj", "{AFDCE6F1-D0E8-42DA-B0D9-201958AC0BAD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UsbHidTestApp_libusb", "UsbHidTestApp_libusb\UsbHidTestApp_libusb.csproj", "{7610FE08-8496-46D7-9044-29BFEAAC4C81}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UsbHidTestApp", "UsbHidTestApp\UsbHidTestApp.csproj", "{8B615DA8-B875-42F6-BE3B-B8569BF18453}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -23,10 +21,6 @@ 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
{97751007-78CC-4356-9F17-F118BA9297FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{97751007-78CC-4356-9F17-F118BA9297FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97751007-78CC-4356-9F17-F118BA9297FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -35,10 +29,10 @@ Global
{AFDCE6F1-D0E8-42DA-B0D9-201958AC0BAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFDCE6F1-D0E8-42DA-B0D9-201958AC0BAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFDCE6F1-D0E8-42DA-B0D9-201958AC0BAD}.Release|Any CPU.Build.0 = Release|Any CPU
{7610FE08-8496-46D7-9044-29BFEAAC4C81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7610FE08-8496-46D7-9044-29BFEAAC4C81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7610FE08-8496-46D7-9044-29BFEAAC4C81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7610FE08-8496-46D7-9044-29BFEAAC4C81}.Release|Any CPU.Build.0 = Release|Any CPU
{8B615DA8-B875-42F6-BE3B-B8569BF18453}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8B615DA8-B875-42F6-BE3B-B8569BF18453}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B615DA8-B875-42F6-BE3B-B8569BF18453}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B615DA8-B875-42F6-BE3B-B8569BF18453}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -1,21 +1,19 @@
using HidLibrary;
using System;
using System.Linq;
using HidApi;
using System.Text;
namespace UsbHidTestApp;
namespace UsbHidTestApp_libusb;
internal class Program
{
private static HidDevice? _device;
private static HidApi.Device? _device;
private static bool _continue;
private static Thread? readThread;
private static List<HidDevice>? lastDevices;
private static IEnumerable<HidApi.DeviceInfo>? lastDevices;
private static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
public static bool applicationQuitRequested { get; private set; }
static void Main(string[] args)
{
while (applicationQuitRequested == false)
@ -32,23 +30,30 @@ internal class Program
{
// list devices
case ConsoleKey.L:
List<HidDevice> hidDevices = HidDevices.Enumerate().ToList();
var hidDevices = Hid.Enumerate();
List<DeviceInfo> newDevicesDeviceInfo = new();
Console.WriteLine($"Following HID devices were found: {Environment.NewLine}");
foreach (var device in hidDevices)
foreach (var deviceInfo in hidDevices)
{
bool newDevice = false;
if(lastDevices?.Where(x => x.DevicePath == device.DevicePath).Any() == false)
var device = deviceInfo.ConnectToDevice();
if (lastDevices?.Where(x => x.Path == device.GetDeviceInfo().Path).Any() == false)
{
Console.ForegroundColor = ConsoleColor.Green;
newDevice = true;
// add and list later
newDevicesDeviceInfo.Add(deviceInfo);
}
Console.WriteLine($"-- {device}");
Console.WriteLine($" {device.Description}");
if (newDevice)
{
Console.ForegroundColor = ConsoleColor.White;
PrintDeviceInfo(deviceInfo);
}
// print new devices in green
Console.ForegroundColor = ConsoleColor.Green;
foreach (var deviceInfo in newDevicesDeviceInfo)
{
PrintDeviceInfo(deviceInfo);
}
Console.ForegroundColor = ConsoleColor.White;
newDevicesDeviceInfo.Clear(); // clear list again
lastDevices = hidDevices;
break;
@ -57,11 +62,11 @@ internal class Program
case ConsoleKey.C:
Console.Write($"Write device vendor id as hex: ");
string? vendorIdString = Console.ReadLine();
int vendorId;
ushort vendorId;
if (vendorIdString == null ||
vendorIdString == "" ||
Int32.TryParse(vendorIdString, System.Globalization.NumberStyles.HexNumber, null, out vendorId) == false)
ushort.TryParse(vendorIdString, System.Globalization.NumberStyles.HexNumber, null, out vendorId) == false)
{
Console.WriteLine($"Invalid vendor ID entered.");
break;
@ -69,39 +74,30 @@ internal class Program
Console.Write($"Write product vendor id as hex: ");
string? productIdString = Console.ReadLine();
int productId;
ushort productId;
if (productIdString == null ||
productIdString == "" ||
Int32.TryParse(productIdString, System.Globalization.NumberStyles.HexNumber, null, out productId) == false)
ushort.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();
_device = new Device(vendorId, productId);
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. Write 'quit' to end communication.");
var devInfo = _device.GetDeviceInfo();
Console.WriteLine($"Connected to device VendorId: 0x{devInfo.VendorId.ToString("X4")}, ProductId: 0x{devInfo.ProductId.ToString("X4")}, Manufacturer: {devInfo.ManufacturerString}.");
Console.WriteLine("Write 'quit' to end communication.");
while (_continue)
{
@ -124,7 +120,7 @@ internal class Program
readThread.Join();
readThread = null;
_device?.CloseDevice();
_device.Dispose(); // "close"
Console.WriteLine("Closed connection to device.");
@ -142,28 +138,15 @@ internal class Program
}
}
private static void DeviceRemovedHandler()
{
Console.WriteLine("Device removed.");
}
Hid.Exit(); //Call at the end of your program
private static void DeviceAttachedHandler()
{
Console.WriteLine("Device attached.");
_device?.ReadReport(OnReport);
}
private static void OnReport(HidReport report)
private static void PrintDeviceInfo(DeviceInfo deviceInfo)
{
if (!_device.IsConnected) { return; }
var data = report.Data;
Console.WriteLine(Encoding.ASCII.GetString(data));
_device.ReadReport(OnReport);
Console.WriteLine($"-- VendorId: 0x{deviceInfo.VendorId.ToString("X4")}, ProductId: 0x{deviceInfo.ProductId.ToString("X4")}, Manufacturer: {deviceInfo.ManufacturerString}, SerialNumber: {deviceInfo.SerialNumber}, Path:");
Console.WriteLine($" {deviceInfo.Path}");
}
public static void Read()
@ -172,23 +155,34 @@ internal class Program
{
try
{
var readData = _device?.Read(100);
ReadOnlySpan<byte> readData = null;
if(_device != null)
{
readData = _device.Read(1000);
}
else
{
Thread.Sleep(20);
}
if (readData != null)
if (readData != null && readData.IsEmpty == false)
{
string print = Encoding.UTF8.GetString(readData.Data);
string print = Encoding.UTF8.GetString(readData).Substring(2);
//print = print.Replace("\r", "").Replace("\n", ""); // Newline Mode: none
//print = print.Replace("\r", ""); // Newline Mode: only print \n
print = print.Replace("\0", ""); // delete null chars
if(String.IsNullOrEmpty(print) == false)
//print = print.Replace("\0", ""); // delete null chars
if (String.IsNullOrEmpty(print) == false)
{
Console.Write(print);
Console.WriteLine(print);
}
}
}
catch (HidException hidex)
{
Console.WriteLine($"Got HidException, device likely disconnected: {hidex}");
Console.WriteLine($"Write 'quit' to end");
_continue = false;
}
catch (TimeoutException) { }
}
}
}

@ -8,7 +8,16 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="hidlibrary" Version="3.3.40" />
<EmbeddedResource Include="hidapi.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="hidapi.lib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="HidApi.Net" Version="0.3.0" />
</ItemGroup>
</Project>

@ -1,178 +0,0 @@
using HidApi;
using System.Text;
namespace UsbHidTestApp_libusb;
internal class Program
{
private static HidApi.Device? _device;
private static bool _continue;
private static Thread? readThread;
private static IEnumerable<HidApi.DeviceInfo>? lastDevices;
private static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
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:
var hidDevices = Hid.Enumerate();
Console.WriteLine($"Following HID devices were found: {Environment.NewLine}");
foreach (var deviceInfo in hidDevices)
{
bool newDevice = false;
var device = deviceInfo.ConnectToDevice();
if (lastDevices?.Where(x => x.Path == device.GetDeviceInfo().Path).Any() == false)
{
Console.ForegroundColor = ConsoleColor.Green;
newDevice = true;
}
Console.WriteLine($"-- VendorId: 0x{deviceInfo.VendorId.ToString("X4")}, ProductId: 0x{deviceInfo.ProductId.ToString("X4")}, Manufacturer: {deviceInfo.ManufacturerString}, Path:");
Console.WriteLine($" {deviceInfo.Path}");
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();
ushort vendorId;
if (vendorIdString == null ||
vendorIdString == "" ||
ushort.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();
ushort productId;
if (productIdString == null ||
productIdString == "" ||
ushort.TryParse(productIdString, System.Globalization.NumberStyles.HexNumber, null, out productId) == false)
{
Console.WriteLine($"Invalid product ID entered.");
break;
}
_device = new Device(vendorId, productId);
if (_device == null)
{
Console.WriteLine("Could not find device.");
break;
}
_continue = true;
readThread = new Thread(Read);
readThread.Start();
var devInfo = _device.GetDeviceInfo();
Console.WriteLine($"Connected to device VendorId: 0x{devInfo.VendorId.ToString("X4")}, ProductId: 0x{devInfo.ProductId.ToString("X4")}, Manufacturer: {devInfo.ManufacturerString}.");
Console.WriteLine("Write 'quit' to end communication.");
while (_continue)
{
var message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
{
_continue = false;
}
else
{
byte[] msgBytes = Encoding.ASCII.GetBytes(message!);
List<byte> listBytes = new List<byte>();
listBytes.Add((byte)' '); // temp fix for first sign wrong
listBytes.AddRange(msgBytes);
listBytes.Add((byte)'\n');
_device.Write(listBytes.ToArray());
}
}
readThread.Join();
readThread = null;
_device.Dispose(); // "close"
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;
}
}
Hid.Exit(); //Call at the end of your program
}
public static void Read()
{
while (_continue)
{
try
{
ReadOnlySpan<byte> readData = null;
if(_device != null)
{
readData = _device.Read(1000);
}
else
{
Thread.Sleep(20);
}
if (readData != null && readData.IsEmpty == false)
{
string print = Encoding.UTF8.GetString(readData);
//print = print.Replace("\r", "").Replace("\n", ""); // Newline Mode: none
//print = print.Replace("\r", ""); // Newline Mode: only print \n
//print = print.Replace("\0", ""); // delete null chars
if (String.IsNullOrEmpty(print) == false)
{
Console.WriteLine(print);
}
}
}
catch (HidException hidex)
{
Console.WriteLine($"Got HidException, device likely disconnected: {hidex}");
Console.WriteLine($"Write 'quit' to end");
_continue = false;
}
}
}
}

@ -1,26 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="hidapi.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="hidapi.lib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="hidapi.pdb">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="HidApi.Net" Version="0.3.0" />
</ItemGroup>
</Project>
Loading…
Cancel
Save