Initial version
This commit is contained in:
14
EonaCat.HID.Console/EonaCat.HID.Console.csproj
Normal file
14
EonaCat.HID.Console/EonaCat.HID.Console.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\EonaCat.HID\EonaCat.HID.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
116
EonaCat.HID.Console/Program.cs
Normal file
116
EonaCat.HID.Console/Program.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using EonaCat.HID;
|
||||
using EonaCat.HID.Models;
|
||||
using System.Globalization;
|
||||
|
||||
namespace EonaCat.HID.Example
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user