Oakrey.UHD
3.0.3
dotnet add package Oakrey.UHD --version 3.0.3
NuGet\Install-Package Oakrey.UHD -Version 3.0.3
<PackageReference Include="Oakrey.UHD" Version="3.0.3" />
<PackageVersion Include="Oakrey.UHD" Version="3.0.3" />
<PackageReference Include="Oakrey.UHD" />
paket add Oakrey.UHD --version 3.0.3
#r "nuget: Oakrey.UHD, 3.0.3"
#:package Oakrey.UHD@3.0.3
#addin nuget:?package=Oakrey.UHD&version=3.0.3
#tool nuget:?package=Oakrey.UHD&version=3.0.3
Oakrey.UHD
A .NET P/Invoke wrapper for the Ettus Research UHD (USRP Hardware Driver) native library.
It provides TX streaming, frequency and gain tuning, stream metadata handling, and structured
logging via IObservable<ILogEvent> for USRP software-defined radio devices.
Main features
- TX streaming -
UhdStreamextendsSystem.IO.Streamand implementsIObservable<ILogEvent>. Write raw IQ sample data directly from any .NET stream source, including files. - Frequency and gain tuning -
TuneRequest/TuneResultwith configurable RF and DSP policies. Actual vs. requested values are compared and reported through the log observable. - Sample rate control - Per-channel TX sample rate get/set via
UhdWrapper. - TX metadata -
TxMetadataHandlesupports time-spec, start-of-burst, and end-of-burst flags. - RX infrastructure -
RxStreamerHandle,RxMetadataHandle,MetadataRxWrapper, andStreamCommandare available for receive-side integration. - Structured logging -
ILogEventwith concrete types:InfoEventArgs,SetEventArgs,SetWarningEventArgs,ErrorEventArgs. - Safe native handles - All UHD handles (
DeviceHandle,TxStreamerHandle,RxStreamerHandle,TxMetadataHandle,RxMetadataHandle,AsyncMetadataHandle) extendSafeHandleZeroOrMinusOneIsInvalidand are disposed automatically. - Error handling - Every native call is checked; failures throw
UhdExceptionwith the UHD error code.
Architecture
graph TD
A[Consumer code] -->|new UhdSettings| B[UhdStream]
B -->|Init / Write| C[UhdWrapper]
C -->|P/Invoke safe| D[UsrpSafeNativeMethods]
C -->|P/Invoke unsafe| E[UsrpUnsafeNativeMethods]
D --> F[uhd.dll]
E --> F
B -->|IObservable| G[ILogEvent stream]
G --> H[InfoEventArgs]
G --> I[SetEventArgs / SetWarningEventArgs]
G --> J[ErrorEventArgs]
B --> K[DeviceHandle]
B --> L[TxStreamerHandle]
B --> M[TxMetadataHandle]
Project structure
| Path | Purpose |
|---|---|
UhdStream.cs |
Main public API - write-only Stream + IObservable<ILogEvent> |
UhdSettings.cs |
Configuration struct passed to UhdStream |
Wrapper/UhdWrapper.cs |
Static facade converting P/Invoke results to exceptions |
Wrapper/UhdException.cs |
Exception type thrown on non-zero UHD error codes |
Handles/ |
Safe handle wrappers for device, streamer, and metadata objects |
NativeMethods/ |
Internal P/Invoke declarations against uhd.dll |
DataTypes/ |
Interop structs: TuneRequest, TuneResult, StreamArgs, ErrorCode |
Logging/ |
ILogEvent and concrete event argument types |
Requirements
- .NET 10 or higher
- Windows x64
uhd.dllfrom the Ettus Research UHD driver placed alongside the application binary (bundled in the NuGet package)- A supported Ettus Research / National Instruments USRP device
Installation
dotnet add package Oakrey.UHD
Or via the Visual Studio NuGet Package Manager: search for Oakrey.UHD.
Configuration
UhdSettings is a value type. All properties have defaults suitable for a single-channel TX scenario.
| Property | Default | Description |
|---|---|---|
Frequency |
(required) | TX center frequency in Hz |
Channel |
0 |
USRP channel index |
CpuFormat |
sc16 |
Host-side sample format (sc16 or fc32) |
DeviceArgs |
"" |
UHD device arguments string (e.g. "addr=192.168.10.2") |
Gain |
50 |
TX gain in dB |
GainName |
"" |
Named gain stage; empty selects the default |
Rate |
2.6e6 |
Sample rate in samples per second |
TimeOut |
3 |
Send timeout in seconds |
HastTimeSpecify |
false |
Enable time-spec on TX metadata |
FullSecs |
0 |
Integer seconds part of time-spec |
FracSecs |
0.1 |
Fractional seconds part of time-spec |
Example usage
Stream raw IQ samples from a file to a USRP device at GPS L1 (1575.42 MHz):
using Oakrey.UHD;
using Oakrey.UHD.Logging;
UhdSettings settings = new(frequency: 1_575_420_000.0)
{
DeviceArgs = "addr=192.168.10.2",
Gain = 60,
Rate = 2_600_000
};
await using UhdStream uhdStream = new(settings);
// Subscribe to log events before initialising
IDisposable subscription = uhdStream.Subscribe(Observer.Create<ILogEvent>(
e => Console.WriteLine(e.Message)));
uhdStream.Init();
using FileStream iqFile = File.OpenRead("samples.bin");
await uhdStream.CopyFrom(iqFile, fileBlockSize: 4096, CancellationToken.None);
subscription.Dispose();
Development notes
uhd.dllmust match the architecture of the host process (x64).- The native UHD API reference used for P/Invoke mapping: https://files.ettus.com/manual/usrp_8h.html
UhdStreamis write-only (CanRead = false,CanSeek = false). TheReadandSeekoverrides throwInvalidOperationException.- Sample buffers are managed with
ArrayPool<short>to reduce GC pressure during streaming. UhdWrapperis astaticclass; handle lifetime is managed entirely by theSafeHandlesubclasses.AllowUnsafeBlocksis required for the fixed-pointer send path inUsrpUnsafeNativeMethods.
License
MIT - Copyright (c) Oakrey 2016-present.
See LICENSE or the PackageLicenseExpression field in the .csproj.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Oakrey.Reflection (>= 2.0.1)
- System.Reactive (>= 6.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.