SiLA2.SerialPort 10.2.4

dotnet add package SiLA2.SerialPort --version 10.2.4
                    
NuGet\Install-Package SiLA2.SerialPort -Version 10.2.4
                    
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="SiLA2.SerialPort" Version="10.2.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SiLA2.SerialPort" Version="10.2.4" />
                    
Directory.Packages.props
<PackageReference Include="SiLA2.SerialPort" />
                    
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 SiLA2.SerialPort --version 10.2.4
                    
#r "nuget: SiLA2.SerialPort, 10.2.4"
                    
#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 SiLA2.SerialPort@10.2.4
                    
#: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=SiLA2.SerialPort&version=10.2.4
                    
Install as a Cake Addin
#tool nuget:?package=SiLA2.SerialPort&version=10.2.4
                    
Install as a Cake Tool

SiLA2.SerialPort

A .NET library for discovering and controlling serial port connections (USB/RS232) in SiLA2 applications. This module provides port discovery, multi-port connection management, and data transfer capabilities commonly needed for laboratory instrument communication.

Features

  • Port Discovery: Enumerate available serial ports with USB device metadata (VID/PID/Serial)
  • Multi-Port Management: Single manager handles multiple simultaneous connections
  • Async Data Transfer: Send and receive text or binary data with async/await patterns
  • Event-Driven: Subscribe to data received, errors, and status changes
  • Configuration via Options Pattern: Easy integration with .NET configuration
  • Dependency Injection: Full DI support with extension methods

Installation

dotnet add package SiLA2.SerialPort

Quick Start

Register Services

// In Program.cs or Startup.cs
builder.Services.AddSerialPortManager();

Discover Ports

public class MyService
{
    private readonly ISerialPortDiscovery _discovery;

    public MyService(ISerialPortDiscovery discovery)
    {
        _discovery = discovery;
    }

    public void ListPorts()
    {
        foreach (var port in _discovery.GetAvailablePorts())
        {
            Console.WriteLine($"{port.PortName}: {port.Description}");
            if (!string.IsNullOrEmpty(port.VendorId))
            {
                Console.WriteLine($"  USB: VID={port.VendorId}, PID={port.ProductId}");
            }
        }
    }
}

Connect and Communicate

public class InstrumentController
{
    private readonly ISerialPortManager _portManager;

    public InstrumentController(ISerialPortManager portManager)
    {
        _portManager = portManager;
    }

    public async Task SendTextDataAsync(string portName, string data)
    {
        var connection = await _portManager.ConnectAsync(new SerialPortOptions
        {
            PortName = portName,
            BaudRate = 9600,
            DataBits = 8,
            StopBits = StopBits.One,
            Parity = Parity.None
        });

        // Send text data (encoded as bytes)
        var bytes = Encoding.UTF8.GetBytes(data);
        var result = await connection.SendAsync(bytes);
        Console.WriteLine($"Sent {result.BytesTransmitted} bytes");
    }

    public async Task SendBinaryDataAsync(string portName, byte[] data)
    {
        var connection = _portManager.GetConnection(portName);
        var result = await connection.SendAsync(data);
        Console.WriteLine($"Sent {result.BytesTransmitted} bytes");
    }

    public async Task<byte[]> ReadBinaryDataAsync(string portName, int maxBytes)
    {
        var connection = _portManager.GetConnection(portName);
        return await connection.ReadAsync(maxBytes);
    }

    public async Task<string> ReadLineAsync(string portName)
    {
        var connection = _portManager.GetConnection(portName);
        return await connection.ReadLineAsync();
    }

    public async Task DisconnectAllAsync()
    {
        await _portManager.DisconnectAllAsync();
    }
}

With Configuration

// appsettings.json
{
  "SerialPort": {
    "BaudRate": 19200,
    "DataBits": 8,
    "Parity": "None",
    "StopBits": "One",
    "ReadTimeoutMs": 2000
  }
}

// Program.cs
builder.Services.AddSerialPortManager(options =>
{
    builder.Configuration.GetSection("SerialPort").Bind(options);
});

Configuration Options

Option Default Description
PortName null Serial port name (e.g., "COM3" or "/dev/ttyUSB0")
BaudRate 9600 Communication speed (must be positive)
DataBits 8 Number of data bits (5, 6, 7, or 8)
StopBits One Stop bits (None, One, OnePointFive, Two)
Parity None Parity mode (None, Odd, Even, Mark, Space)
Handshake None Flow control (None, XOnXOff, RequestToSend, RequestToSendXOnXOff)
ReadTimeoutMs 1000 Read timeout in milliseconds
WriteTimeoutMs 1000 Write timeout in milliseconds
NewLine "\r\n" Line terminator for text operations
AutoReconnect false Automatically reconnect on connection loss
ReconnectDelayMs 5000 Delay between reconnection attempts

Events

ISerialPortConnection Events

connection.DataReceived += (sender, data) =>
{
    Console.WriteLine($"Received {data.Length} bytes");
};

connection.ErrorOccurred += (sender, ex) =>
{
    Console.WriteLine($"Error: {ex.Message}");
};

connection.StatusChanged += (sender, status) =>
{
    Console.WriteLine($"Status: {status}");
};

ISerialPortManager Events

manager.ConnectionOpened += (sender, connection) =>
{
    Console.WriteLine($"Connected to {connection.PortName}");
};

manager.ConnectionClosed += (sender, portName) =>
{
    Console.WriteLine($"Disconnected from {portName}");
};

SiLA2 Feature Integration

For gRPC exposure of serial communication, use the SiLA2.SerialPort.Features package which provides a complete SiLA2 Feature Definition and service implementation.

Platform Support

  • Windows: Full support with native COM port enumeration and WMI-based USB metadata
  • Linux: Supported via .NET 6+ (ports typically at /dev/ttyUSB* or /dev/ttyACM*)
  • macOS: Supported via .NET 6+ (ports typically at /dev/tty.*)

License

MIT License - See LICENSE file for details.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SiLA2.SerialPort:

Package Downloads
SiLA2.SerialPort.Features

SiLA2 Feature package for serial port communication (USB/RS232). Provides a complete SiLA2 Feature Definition (FDL) and gRPC service implementation for port discovery, connection management, and data transfer for laboratory instruments.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.2.4 92 3/13/2026
10.2.3 83 3/7/2026
10.2.2 98 3/1/2026