SiLA2.SerialPort
10.2.2
See the version list below for details.
dotnet add package SiLA2.SerialPort --version 10.2.2
NuGet\Install-Package SiLA2.SerialPort -Version 10.2.2
<PackageReference Include="SiLA2.SerialPort" Version="10.2.2" />
<PackageVersion Include="SiLA2.SerialPort" Version="10.2.2" />
<PackageReference Include="SiLA2.SerialPort" />
paket add SiLA2.SerialPort --version 10.2.2
#r "nuget: SiLA2.SerialPort, 10.2.2"
#:package SiLA2.SerialPort@10.2.2
#addin nuget:?package=SiLA2.SerialPort&version=10.2.2
#tool nuget:?package=SiLA2.SerialPort&version=10.2.2
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 metadata
- Multi-Port Management: Single manager handles multiple simultaneous connections
- Async Data Transfer: Send and receive data with async/await patterns
- Command-Response Pattern: Built-in support for common lab instrument protocols
- 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}");
}
}
}
Connect and Communicate
public class InstrumentController
{
private readonly ISerialPortManager _portManager;
public InstrumentController(ISerialPortManager portManager)
{
_portManager = portManager;
}
public async Task<string> GetDeviceIdAsync(string portName)
{
// Connect with custom options
var connection = await _portManager.ConnectAsync(new SerialPortOptions
{
PortName = portName,
BaudRate = 9600,
NewLine = "\r\n"
});
// Send SCPI command and get response
return await connection.SendCommandAsync("*IDN?");
}
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 |
DataBits |
8 | Number of data bits |
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
- 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 | 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
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.3)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.3)
- Microsoft.Extensions.Options (>= 10.0.3)
- System.IO.Ports (>= 9.0.0)
- System.Management (>= 9.0.0)
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.