HidClient 1.0.1

dotnet add package HidClient --version 1.0.1                
NuGet\Install-Package HidClient -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HidClient" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HidClient --version 1.0.1                
#r "nuget: HidClient, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

Nuget GitHub Workflow Status Testspace Coveralls

Common library class to receive updates from a USB HID and reconnect automatically when disconnected

USB-A plug

  1. Introduction
  2. Prerequisites
  3. Installation
  4. Usage
  5. Testing

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

Installation

dotnet add package HidClient

Usage

  1. 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) { }
    
    }
    
  2. Override the VendorId and ProductId properties to return the VID and PID of your device.
    • In Windows, these can be found in Device Manager as the hexadecimal VID and PID values under Hardware Ids.
    • In Linux, these can be found in the output of lsusb as the hexadecimal ID colon-delimited value.
    protected override int VendorId { get; } = 0x2474;
    protected override int ProductId { get; } = 0x0550;
    
  3. 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 });
    }
    
  4. 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);
    }
    
  5. To send commands to the device, call DeviceStream.Write(byte[]) or DeviceStream.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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
1.0.1 594 4/9/2023
1.0.0 169 4/9/2023