113 lines
3.6 KiB
C#
113 lines
3.6 KiB
C#
using EonaCat.HID;
|
|
using EonaCat.HID.Models;
|
|
using System.Globalization;
|
|
|
|
namespace EonaCat.HID.Example
|
|
{
|
|
public class Program
|
|
{
|
|
static IHidManager _deviceManager;
|
|
static IHid _device;
|
|
static IEnumerable<IHid> _deviceList;
|
|
|
|
static async Task Main(string[] args)
|
|
{
|
|
try
|
|
{
|
|
_deviceManager = HidFactory.CreateDeviceManager();
|
|
if (_deviceManager == null)
|
|
{
|
|
Console.WriteLine("Failed to create HID manager.");
|
|
return;
|
|
}
|
|
|
|
_deviceManager.OnDeviceInserted += (s, e) =>
|
|
{
|
|
Console.WriteLine($"Inserted Device --> VID: {e.Device.VendorId:X4}, PID: {e.Device.ProductId:X4}");
|
|
};
|
|
|
|
_deviceManager.OnDeviceRemoved += (s, e) =>
|
|
{
|
|
Console.WriteLine($"Removed Device --> VID: {e.Device.VendorId:X4}, PID: {e.Device.ProductId:X4}");
|
|
};
|
|
|
|
RefreshDevices();
|
|
|
|
if (!_deviceList.Any())
|
|
{
|
|
Console.WriteLine("No HID found.");
|
|
return;
|
|
}
|
|
|
|
DisplayDevices();
|
|
|
|
Console.Write("Select a device index to connect: ");
|
|
int index = int.Parse(Console.ReadLine());
|
|
_device = _deviceList.ElementAt(index);
|
|
|
|
_device.OnDataReceived += (s, e) =>
|
|
{
|
|
Console.WriteLine($"Rx Data: {BitConverter.ToString(e.Report.Data)}");
|
|
};
|
|
|
|
_device.OnError += (s, e) =>
|
|
{
|
|
Console.WriteLine($"Error: {e.Exception.Message}");
|
|
};
|
|
|
|
_device.Open();
|
|
Console.WriteLine($"Connected to {_device.ProductName}");
|
|
|
|
await _device.StartListeningAsync(default);
|
|
|
|
Console.WriteLine("Listening... Press [Enter] to send test output report.");
|
|
Console.ReadLine();
|
|
|
|
// Example: Send output report using HidReport
|
|
var data = ByteHelper.HexStringToByteArray("01-02-03");
|
|
var reportId = (byte)0x00; // Report ID
|
|
var outputReport = new HidReport(reportId, data);
|
|
|
|
await _device.WriteOutputReportAsync(outputReport);
|
|
Console.WriteLine($"Sent output report: Report ID: {reportId}, Data: {BitConverter.ToString(data)}");
|
|
|
|
Console.WriteLine("Press [Enter] to exit...");
|
|
Console.ReadLine();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[EXCEPTION] {ex.Message}");
|
|
}
|
|
}
|
|
|
|
static void RefreshDevices(ushort? vendorId = null, ushort? productId = null)
|
|
{
|
|
_deviceList = _deviceManager.Enumerate(vendorId, productId);
|
|
}
|
|
|
|
static void DisplayDevices()
|
|
{
|
|
int i = 0;
|
|
foreach (var dev in _deviceList)
|
|
{
|
|
Console.WriteLine($"[{i++}] {dev.ProductName} | VID:PID = {dev.VendorId:X4}:{dev.ProductId:X4}");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class ByteHelper
|
|
{
|
|
public static byte[] HexStringToByteArray(string hex)
|
|
{
|
|
return hex
|
|
.Replace("-", "")
|
|
.Replace(" ", "")
|
|
.ToUpper()
|
|
.Where(c => Uri.IsHexDigit(c))
|
|
.Select((c, i) => new { c, i })
|
|
.GroupBy(x => x.i / 2)
|
|
.Select(g => byte.Parse($"{g.First().c}{g.Last().c}", NumberStyles.HexNumber))
|
|
.ToArray();
|
|
}
|
|
}
|
|
} |