You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
6.6 KiB

using HidLibrary;
using System;
using System.Linq;
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;
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:
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. 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?.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(100);
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("\0", ""); // delete null chars
if(String.IsNullOrEmpty(print) == false)
{
Console.Write(print);
}
}
}
catch (TimeoutException) { }
}
}
}