HidClient 1.0.1
dotnet add package HidClient --version 1.0.1
NuGet\Install-Package HidClient -Version 1.0.1
<PackageReference Include="HidClient" Version="1.0.1" />
paket add HidClient --version 1.0.1
#r "nuget: HidClient, 1.0.1"
// Install HidClient as a Cake Addin #addin nuget:?package=HidClient&version=1.0.1 // Install HidClient as a Cake Tool #tool nuget:?package=HidClient&version=1.0.1
HidClient
Common library class to receive updates from a USB HID and reconnect automatically when disconnected
Introduction
This library provides AbstractHidClient
, a class that layers on top of HidSharp and provides the following useful abstractions.
- Automatically connect to a device with the given Vendor ID and Product ID
- If the device is not physically connected yet, wait for it to be available and use it automatically as soon as it's ready
- If the device disconnects, wait for it and automatically reconnect when it's available again
- Properties and events that let you observe the connection state
- Automatically run a message pump thread to receive data from the device
This common logic was extracted from Aldaviva/PowerMate so it could be reused in Aldaviva/WebScale.Net. It is intended to help developers write device-specific HID libraries without duplicating boilerplate connection management code in each project.
Prerequisites
- Any .NET runtime that supports .NET Standard 2.0 or later:
- Operating systems:
- Windows
- MacOS
- Linux (although HidSharp seems to be unable to detect most devices on Linux)
Installation
dotnet add package HidClient
Usage
- Create a subclass of
AbstractHidClient
and stub out the mandatory overrides.public class WebScale: AbstractHidClient { public WebScale() { } public WebScale(DeviceList deviceList): base(deviceList) { } protected override int VendorId { get; } protected override int ProductId { get; } protected override void OnHidRead(byte[] readBuffer) { } }
- Override the
VendorId
andProductId
properties to return the VID and PID of your device.- In Windows, these can be found in Device Manager as the hexadecimal
VID
andPID
values under Hardware Ids. - In Linux, these can be found in the output of
lsusb
as the hexadecimalID
colon-delimited value.
protected override int VendorId { get; } = 0x2474; protected override int ProductId { get; } = 0x0550;
- In Windows, these can be found in Device Manager as the hexadecimal
- If you need to run initialization logic each time the device connects, for example resetting LED brightness that the device doesn't persist on its own, you may optionally override the
OnConnect()
method.protected override void OnConnect() { DeviceStream?.SetFeature(new byte[]{ 0x00, 0x41, 0x01, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00 }); }
- Override the
OnHidRead(byte[])
method to handle the bytes read from the device.protected override void OnHidRead(byte[] readBuffer) { double ounces = BitConverter.ToInt16(readBuffer, 4) / 10.0; Weight = Force.FromOunceForce(ounces); }
- To send commands to the device, call
DeviceStream.Write(byte[])
orDeviceStream.WriteAsync(byte[], int, int)
.public void Tare() { DeviceStream?.Write(new byte[]{ 0x04, 0x01 }); }
Testing
See Aldaviva/PowerMate and Aldaviva/WebScale.Net for examples of unit testing HidClient. These test suites mock HidSharp using FakeItEasy so they don't need real devices connected to the build machines.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- HidSharp (>= 2.1.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on HidClient:
Package | Downloads |
---|---|
PowerMate
Receive events and control the light on a Griffin PowerMate USB device |
|
WebScale
Measure weight using a Stamps.com digital USB postage scale |
GitHub repositories
This package is not used by any popular GitHub repositories.