Oakrey.Usb
3.0.3
dotnet add package Oakrey.Usb --version 3.0.3
NuGet\Install-Package Oakrey.Usb -Version 3.0.3
<PackageReference Include="Oakrey.Usb" Version="3.0.3" />
<PackageVersion Include="Oakrey.Usb" Version="3.0.3" />
<PackageReference Include="Oakrey.Usb" />
paket add Oakrey.Usb --version 3.0.3
#r "nuget: Oakrey.Usb, 3.0.3"
#:package Oakrey.Usb@3.0.3
#addin nuget:?package=Oakrey.Usb&version=3.0.3
#tool nuget:?package=Oakrey.Usb&version=3.0.3
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
UsbAdapterand the simplified send-onlyUsbOutStreamAdapter. - Device monitoring -
UsbNotifierraises arrival and removal events for any USB device connected to the system. - Filtered subscriptions -
UsbNotifier.RegisterUsbNotificationaccepts a predicate so consumers receive only the events relevant to their VID, PID, or serial number. - Parsed device identity -
ProcessedDeviceInfoextracts VID, PID, serial number, and device ID from the PnP symbolic link automatically.
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]
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();
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) |
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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-windows7.0 is compatible. |
-
net10.0-windows7.0
- System.Management (>= 10.0.8)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Oakrey.Usb:
| Package | Downloads |
|---|---|
|
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.