CosmoSerialPort 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CosmoSerialPort --version 0.1.0
                    
NuGet\Install-Package CosmoSerialPort -Version 0.1.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="CosmoSerialPort" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CosmoSerialPort" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="CosmoSerialPort" />
                    
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 CosmoSerialPort --version 0.1.0
                    
#r "nuget: CosmoSerialPort, 0.1.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 CosmoSerialPort@0.1.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=CosmoSerialPort&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=CosmoSerialPort&version=0.1.0
                    
Install as a Cake Tool

CosmoSerialPort

A modern, cross-platform serial port library for .NET 10 — a semantic port of serialport-rs built for industrial automation, robotics, SCADA, telemetry and other long-running, latency-sensitive workloads.

  • Windows (Win32 overlapped I/O + IOCP), Linux (termios2/BOTHER, poll), macOS (termios + IOSSIOSPEED, IOKit enumeration)
  • SerialPort derives from Stream: works with StreamReader, System.IO.Pipelines, etc.
  • Real async: pooled IValueTaskSource operations over a poll reactor (Unix) / IOCP (Windows) — no Task.Run wrappers, zero steady-state allocations
  • Deterministic disposal: Dispose() wakes blocked readers, even with infinite timeouts
  • Port enumeration with USB VID/PID, serial number, manufacturer and product strings
  • Modem pin control (RTS/DTR out, CTS/DSR/RI/CD in), break, buffer discard, XON/XOFF and RTS/CTS flow control
  • Opt-in text layer (ReadLine/WriteLine, NewLine, Encoding) and a race-free DataReceived event — the binary hot path stays untouched until you use them
  • Nullable, analyzer-clean, AOT- and trimming-compatible
  • Targets net10.0 and netstandard2.0 — usable from .NET Framework 4.7.2+, validated on .NET Framework 4.8 (see samples/CosmoSerialPort.Probe48). The ns2.0 build uses classic DllImport interop and a small polyfill layer; behavior is identical, though async continuations there always dispatch via the thread pool

Quick start

using CosmoSerialPort;

// Discover ports
foreach (SerialPortInfo info in SerialPort.GetPortInfos())
    Console.WriteLine($"{info.PortName} [{info.PortType}] {info.UsbInfo?.Product}");

// Open with the builder (an immutable record — share and reconfigure freely)
using SerialPort port = SerialPort.Create("/dev/ttyUSB0", 115200)
    .WithParity(Parity.None)
    .WithStopBits(StopBits.One)
    .WithFlowControl(FlowControl.None)
    .WithReadTimeout(TimeSpan.FromSeconds(2))
    .Open();

// Sync: returns as soon as at least one byte is available,
// throws SerialPortTimeoutException after ReadTimeout.
byte[] buffer = new byte[256];
int read = port.Read(buffer);

// Async with cancellation
int n = await port.ReadAsync(buffer.AsMemory(), cancellationToken);
await port.WriteAsync("AT\r\n"u8.ToArray());

// Pipelines for protocol parsing
var reader = port.CreatePipeReader();

// Control surface
port.SetRts(true);
ModemPins pins = port.GetModemPins();
port.DiscardBuffers(ClearBuffer.Input);

// Text layer (opt-in; UTF-8 and "\n" by default)
port.WriteLine("AT");
string? reply = port.ReadLine();
string? reply2 = await port.ReadLineAsync(cancellationToken);

// DataReceived: a background watcher owns the receive side while subscribed,
// so reading inside the handler is safe (unlike System.IO.Ports).
port.DataReceived += (_, e) =>
{
    if (!e.IsEndOfStream && port.ReadLine() is { } line)
        Console.WriteLine($"got: {line}");
};

Timeouts

Value Sync behavior Async behavior
Timeout.Infinite (default) Block until data Wait until data or cancellation
> 0 SerialPortTimeoutException after the timeout Same
0 Fail immediately if nothing buffered (the serialport-rs default) Same

SerialPortTimeoutException derives from TimeoutException for System.IO.Ports compatibility; all other errors derive from SerialPortException (an IOException) with the native error code preserved.

Thread safety

One thread may read while another writes on the same instance. Multiple concurrent readers (or writers) are rejected. Control operations are individually thread-safe.

Repository layout

Path Contents
src/CosmoSerialPort The library
tests/CosmoSerialPort.Tests xunit suite; loopback tests run against kernel pseudo-terminals on Unix
bench/CosmoSerialPort.Benchmarks BenchmarkDotNet loopback benchmarks
samples/CosmoSerialPort.Probe Hardware smoke test / enumeration demo for a real or virtual port
ARCHITECTURE.md Analysis of serialport-rs and the porting decisions
dotnet build -c Release        # warning-clean, analyzers on
dotnet test  -c Release        # 31 tests; PTY loopback suite on macOS/Linux
dotnet run --project samples/CosmoSerialPort.Probe -c Release -- COM1   # against an echo peer
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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  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. 
.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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 113 7/20/2026
1.0.0 93 7/20/2026
0.2.0 107 7/19/2026
0.1.1 99 7/19/2026
0.1.0 107 7/18/2026