Oakrey.Usb 3.2.0

dotnet add package Oakrey.Usb --version 3.2.0
                    
NuGet\Install-Package Oakrey.Usb -Version 3.2.0
                    
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="Oakrey.Usb" Version="3.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Oakrey.Usb" Version="3.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Oakrey.Usb" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Oakrey.Usb --version 3.2.0
                    
#r "nuget: Oakrey.Usb, 3.2.0"
                    
#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.
#:package Oakrey.Usb@3.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Oakrey.Usb&version=3.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Oakrey.Usb&version=3.2.0
                    
Install as a Cake Tool

Oakrey.Usb

Windows .NET library for WinUSB bulk-transfer communication and real-time USB plug-and-play device monitoring.

Main features

  • WinUSB bulk transfer - synchronous read/write over WinUSB pipes via UsbAdapter and the simplified send-only UsbOutStreamAdapter.
  • Device monitoring - UsbNotifier raises arrival and removal events for any USB device connected to the system.
  • Filtered subscriptions - UsbNotifier.RegisterUsbNotification accepts a predicate so consumers receive only the events relevant to their VID, PID, or serial number.
  • Reactive Extensions - consume device changes as IObservable<UsbEventArgs> and compose them with standard System.Reactive operators.
  • Safe device identity parsing - ProcessedDeviceInfo validates and extracts VID, PID, serial number, and device ID from a PnP symbolic link; TryParse handles untrusted input without exceptions.

Architecture

graph TD
    subgraph Oakrey.Usb.Adapter
        IUsbAdapter
        UsbAdapter -->|implements| IUsbAdapter
        UsbOutStreamAdapter -->|wraps| UsbAdapter
    end

    subgraph Oakrey.Usb.Notifier
        UsbNotifier
        UsbNotifierFilter -->|filters| UsbNotifier
        IUsbNotifierFilter
        UsbNotifierFilter -->|implements| IUsbNotifierFilter
    end

    UsbNotifier -->|uses| Nefarius[Nefarius.Utilities.DeviceManagement]
    UsbNotifier -->|exposes| Rx[System.Reactive]
    UsbAdapter -->|uses| WinUSBNet

Requirements

  • .NET 10 (Windows only - net10.0-windows)
  • The target USB device must have the WinUSB kernel driver installed (use Zadig or a custom INF).

Installation

.NET CLI

dotnet add package Oakrey.Usb

Package Manager Console

Install-Package Oakrey.Usb

NuGet Package Manager

Search for Oakrey.Usb in Visual Studio under Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

Usage

Reading and writing over WinUSB

using Oakrey.Usb.Adapter;

// guid  - DeviceInterfaceGUID registered in the WinUSB INF
// serial - device serial number string
using UsbAdapter adapter = new("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}", "MY_SERIAL");

Console.WriteLine($"Connected: {adapter.Name} ({adapter.Serial})");

// Write
byte[] command = [0x01, 0x02, 0x03];
adapter.Write(command, 0, command.Length);

// Read
byte[] response = new byte[64];
int bytesRead = adapter.Read(response, 0, response.Length);

Send-only shortcut with UsbOutStreamAdapter:

using UsbOutStreamAdapter stream = new("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}", "MY_SERIAL");
stream.Send([0x01, 0x02, 0x03]);

Monitoring device arrival and removal

Subscribe to all USB devices:

using Oakrey.Usb.Notifier;

using IUsbNotifierFilter subscription = UsbNotifier.RegisterUsbNotification(_ => true);

subscription.UsbEvent += (sender, e) =>
{
    Console.WriteLine($"{e.UsbDeviceChangeStatus}: VID={e.DeviceInfo.VID} PID={e.DeviceInfo.PID} Serial={e.DeviceInfo.Serial}");
};

Filter by VID and PID:

using IUsbNotifierFilter subscription = UsbNotifier.RegisterUsbNotification(
    device => device.VID == "1234" && device.PID == "ABCD");

subscription.UsbEvent += (sender, e) =>
{
    if (e.UsbDeviceChangeStatus == UsbDeviceChangeStatus.Inserted)
    {
        Console.WriteLine($"Target device arrived: {e.DeviceInfo.Serial}");
    }
};

Dispose the subscription to stop receiving events:

subscription.Dispose();

Reactive USB notifications

Observe all USB device changes using Reactive Extensions:

using Oakrey.Usb.Notifier;
using System.Reactive.Linq;

using IDisposable subscription = UsbNotifier
    .ObserveUsbNotifications()
    .Where(e => e.UsbDeviceChangeStatus == UsbDeviceChangeStatus.Inserted)
    .Subscribe(e =>
        Console.WriteLine($"Connected: {e.DeviceInfo.VID}:{e.DeviceInfo.PID} ({e.DeviceInfo.Serial})"));

Filter devices before they enter the RX pipeline:

using IDisposable subscription = UsbNotifier
    .ObserveUsbNotifications(device => device.VID == "1234" && device.PID == "ABCD")
    .Subscribe(e => Console.WriteLine($"{e.UsbDeviceChangeStatus}: {e.DeviceInfo.Serial}"));

An existing UsbNotifier or IUsbNotifierFilter can be converted using ToObservable(). Disposing the RX subscription detaches its event handler.

ProcessedDeviceInfo properties

Property Description
VID Vendor ID (4 hex digits)
PID Product ID (4 hex digits)
Serial Device serial number string
SymbolicLink Raw PnP symbolic link
DeviceId Formatted device ID (USB\VID_xxxx&PID_xxxx\serial)

For symbolic links received from an untrusted or optional source, use the non-throwing parser:

if (ProcessedDeviceInfo.TryParse(symbolicLink, out ProcessedDeviceInfo? device))
{
    Console.WriteLine($"{device.VID}:{device.PID} ({device.Serial})");
}

Project information

Field Value
Author Oakrey
License MIT
Repository https://dev.azure.com/oakrey/OpenPackages/_git/Drivers
Project URL http://www.oakrey.cz/opkg_drivers_usb

License

MIT License. See LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net10.0-windows7.0 is compatible. 
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 Oakrey.Usb:

Package Downloads
Oakrey.Guru.Adapter

Base adapter library for Oakrey Guru USB hardware devices. Provides a structured packet-framing protocol with CRC16-CCITT validation, and transceiver, transmitter, and receiver abstractions over USB.

Oakrey.Applications.UsbNotifying

Windows-only .NET service that wraps the Windows PnP device notification API and exposes USB device arrival and removal events as an IObservable<UsbEventArgs> stream, backed by a ReplaySubject from System.Reactive.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.2.0 181 7/14/2026
3.0.3 202 5/15/2026
3.0.2 110 5/14/2026
3.0.1 198 2/2/2026
3.0.0 140 1/7/2026
2.0.0 337 11/14/2025
1.0.0 5,179 4/17/2025