Updated README.md
This commit is contained in:
parent
1b20217def
commit
effcea8b4d
|
@ -5,6 +5,9 @@ using System.Windows.Forms;
|
||||||
|
|
||||||
namespace EonaCat.HID.Analyzer
|
namespace EonaCat.HID.Analyzer
|
||||||
{
|
{
|
||||||
|
// 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.
|
||||||
|
|
||||||
internal partial class AboutBox : Form
|
internal partial class AboutBox : Form
|
||||||
{
|
{
|
||||||
public AboutBox()
|
public AboutBox()
|
||||||
|
|
|
@ -13,6 +13,9 @@ using System.Windows.Forms;
|
||||||
|
|
||||||
namespace EonaCat.HID.Analyzer
|
namespace EonaCat.HID.Analyzer
|
||||||
{
|
{
|
||||||
|
// 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 partial class MainForm : Form
|
public partial class MainForm : Form
|
||||||
{
|
{
|
||||||
IHidManager _deviceManager;
|
IHidManager _deviceManager;
|
||||||
|
|
|
@ -3,6 +3,8 @@ using System.Windows.Forms;
|
||||||
|
|
||||||
namespace EonaCat.HID.Analyzer
|
namespace EonaCat.HID.Analyzer
|
||||||
{
|
{
|
||||||
|
// 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.
|
||||||
internal static class Program
|
internal static class Program
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -4,6 +4,9 @@ using System.Globalization;
|
||||||
|
|
||||||
namespace EonaCat.HID.Example
|
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
|
public class Program
|
||||||
{
|
{
|
||||||
static IHidManager _deviceManager;
|
static IHidManager _deviceManager;
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
<Copyright>Copyright 2024 EonaCat (Jeroen Saey)</Copyright>
|
<Copyright>Copyright 2024 EonaCat (Jeroen Saey)</Copyright>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
<PackageId>EonaCat.HID</PackageId>
|
<PackageId>EonaCat.HID</PackageId>
|
||||||
<Version>1.0.3</Version>
|
<Version>1.0.4</Version>
|
||||||
<Title>EonaCat.HID</Title>
|
<Title>EonaCat.HID</Title>
|
||||||
<Authors>EonaCat (Jeroen Saey)</Authors>
|
<Authors>EonaCat (Jeroen Saey)</Authors>
|
||||||
<Description>HID Devices</Description>
|
<Description>HID Devices</Description>
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace EonaCat.HID.Models
|
namespace EonaCat.HID.Models
|
||||||
{
|
{
|
||||||
|
// 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 HidReport
|
public class HidReport
|
||||||
{
|
{
|
||||||
public byte ReportId { get; }
|
public byte ReportId { get; }
|
||||||
|
|
186
README.md
186
README.md
|
@ -6,114 +6,120 @@ Works on Windows, Linux and macOS.
|
||||||
Example Code:
|
Example Code:
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using EonaCat.HID.EventArguments;
|
using EonaCat.HID;
|
||||||
using EonaCat.HID.Helpers;
|
using EonaCat.HID.Models;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
class Program
|
namespace EonaCat.HID.Example
|
||||||
{
|
{
|
||||||
static IHidManager _deviceManager;
|
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||||
static IHid _device;
|
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||||
static IEnumerable<IHid> _deviceList;
|
|
||||||
|
|
||||||
static async Task Main(string[] args)
|
public class Program
|
||||||
{
|
{
|
||||||
try
|
static IHidManager _deviceManager;
|
||||||
|
static IHid _device;
|
||||||
|
static IEnumerable<IHid> _deviceList;
|
||||||
|
|
||||||
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
_deviceManager = HidFactory.CreateDeviceManager();
|
try
|
||||||
if (_deviceManager == null)
|
|
||||||
{
|
{
|
||||||
Console.WriteLine("Failed to create HID manager.");
|
_deviceManager = HidFactory.CreateDeviceManager();
|
||||||
return;
|
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)
|
||||||
_deviceManager.OnDeviceInserted += (s, e) =>
|
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Inserted Device --> VID: {e.Device.VendorId:X4}, PID: {e.Device.ProductId:X4}");
|
Console.WriteLine($"[EXCEPTION] {ex.Message}");
|
||||||
};
|
|
||||||
|
|
||||||
_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.Data)}");
|
|
||||||
};
|
|
||||||
|
|
||||||
_device.OnError += (s, e) =>
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Error: {e.Exception.Message}");
|
|
||||||
};
|
|
||||||
|
|
||||||
_device.Open();
|
|
||||||
Console.WriteLine($"Connected to {_device.ProductName}");
|
|
||||||
|
|
||||||
await _device.StartListeningAsync();
|
|
||||||
|
|
||||||
Console.WriteLine("Listening... Press [Enter] to send test output report.");
|
|
||||||
Console.ReadLine();
|
|
||||||
|
|
||||||
// Example: Send output report
|
|
||||||
var data = ByteHelper.HexStringToByteArray("01-02-03");
|
|
||||||
byte[] outputReport = new byte[data.Length + 1];
|
|
||||||
outputReport[0] = 0x00; // Report ID
|
|
||||||
Array.Copy(data, 0, outputReport, 1, data.Length);
|
|
||||||
|
|
||||||
await _device.WriteOutputReportAsync(outputReport);
|
|
||||||
Console.WriteLine($"Sent output report: {BitConverter.ToString(outputReport)}");
|
|
||||||
|
|
||||||
Console.WriteLine("Press [Enter] to exit...");
|
|
||||||
Console.ReadLine();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
|
static void RefreshDevices(ushort? vendorId = null, ushort? productId = null)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"[EXCEPTION] {ex.Message}");
|
_deviceList = _deviceManager.Enumerate(vendorId, productId);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static void RefreshDevices(ushort? vendorId = null, ushort? productId = null)
|
static void DisplayDevices()
|
||||||
{
|
|
||||||
_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}");
|
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 class ByteHelper
|
||||||
{
|
|
||||||
public static byte[] HexStringToByteArray(string hex)
|
|
||||||
{
|
{
|
||||||
return hex
|
public static byte[] HexStringToByteArray(string hex)
|
||||||
.Replace("-", "")
|
{
|
||||||
.Replace(" ", "")
|
return hex
|
||||||
.ToUpper()
|
.Replace("-", "")
|
||||||
.Where(c => Uri.IsHexDigit(c))
|
.Replace(" ", "")
|
||||||
.Select((c, i) => new { c, i })
|
.ToUpper()
|
||||||
.GroupBy(x => x.i / 2)
|
.Where(c => Uri.IsHexDigit(c))
|
||||||
.Select(g => byte.Parse($"{g.First().c}{g.Last().c}", NumberStyles.HexNumber))
|
.Select((c, i) => new { c, i })
|
||||||
.ToArray();
|
.GroupBy(x => x.i / 2)
|
||||||
|
.Select(g => byte.Parse($"{g.First().c}{g.Last().c}", NumberStyles.HexNumber))
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
Loading…
Reference in New Issue