SiLA2.SerialPort.Features
10.2.4
dotnet add package SiLA2.SerialPort.Features --version 10.2.4
NuGet\Install-Package SiLA2.SerialPort.Features -Version 10.2.4
<PackageReference Include="SiLA2.SerialPort.Features" Version="10.2.4" />
<PackageVersion Include="SiLA2.SerialPort.Features" Version="10.2.4" />
<PackageReference Include="SiLA2.SerialPort.Features" />
paket add SiLA2.SerialPort.Features --version 10.2.4
#r "nuget: SiLA2.SerialPort.Features, 10.2.4"
#:package SiLA2.SerialPort.Features@10.2.4
#addin nuget:?package=SiLA2.SerialPort.Features&version=10.2.4
#tool nuget:?package=SiLA2.SerialPort.Features&version=10.2.4
SiLA2.SerialPort.Features
A SiLA2 Feature package that exposes serial port communication capabilities via gRPC. This module provides a complete SiLA2 Feature Definition (FDL) and gRPC service implementation for serial port discovery, connection management, and data transfer.
Features
- SiLA2-Compliant Interface: Full FDL-defined feature with gRPC service implementation
- Port Discovery: Enumerate available serial ports with USB device metadata (VID/PID/Serial)
- Connection Management: Connect/disconnect to multiple ports simultaneously
- Text & Binary Transfer: Send text data with configurable encoding or raw binary data
- Observable Properties: Subscribe to real-time updates for available and connected ports
- USB Device Identification: Connect by VID/PID for reliable device identification
- Parameter Constraints: Validated parameters with Set constraints for enums and Unit constraints for timeouts
- Cross-Platform: Windows, Linux, and macOS support
Installation
dotnet add package SiLA2.SerialPort.Features
This package requires SiLA2.Core and SiLA2.SerialPort which will be installed automatically as dependencies.
Quick Start
Register Services
// Program.cs
builder.Services.AddSerialPortManager();
builder.Services.AddSingleton<SerialCommunicationService>();
var app = builder.Build();
// Initialize SiLA2 features
app.InitializeSiLA2Features(siLA2Server);
// Map gRPC service
app.MapGrpcService<SerialCommunicationService>();
Client Usage
// Create channel and client
var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new SerialCommunication.SerialCommunicationClient(channel);
// Connect to a port
await client.ConnectAsync(new Connect_Parameters
{
PortName = new String { Value = "COM3" },
BaudRate = new Integer { Value = 9600 },
DataBits = new Integer { Value = 8 },
StopBits = new String { Value = "One" },
Parity = new String { Value = "None" }
});
// Send text data with encoding
var textResponse = await client.SendTextDataAsync(new SendTextData_Parameters
{
PortName = new String { Value = "COM3" },
Data = new String { Value = "Hello, Device!" },
Encoding = new String { Value = "UTF-8" }
});
Console.WriteLine($"Sent {textResponse.BytesSent.Value} bytes");
// Send binary data
var binaryResponse = await client.SendBinaryDataAsync(new SendBinaryData_Parameters
{
PortName = new String { Value = "COM3" },
Data = new Binary { Value = ByteString.CopyFrom(new byte[] { 0x01, 0x02, 0x03 }) }
});
Console.WriteLine($"Sent {binaryResponse.BytesSent.Value} bytes");
// Read binary data
var readResponse = await client.ReadBinaryDataAsync(new ReadBinaryData_Parameters
{
PortName = new String { Value = "COM3" },
MaxBytes = new Integer { Value = 1024 },
Timeout = new Integer { Value = 5000 } // 5000 ms
});
Console.WriteLine($"Received {readResponse.Data.Value.Length} bytes");
// Disconnect
await client.DisconnectAsync(new Disconnect_Parameters
{
PortName = new String { Value = "COM3" }
});
Feature Definition
The SerialCommunication feature (FDL v1.0) provides:
Properties
| Property | Observable | Description |
|---|---|---|
AvailablePorts |
Yes | List of available serial ports with detailed metadata |
ConnectedPorts |
Yes | List of currently connected port names |
Commands
| Command | Description |
|---|---|
Connect |
Connect to a serial port with configuration options |
Disconnect |
Disconnect from a serial port |
SendTextData |
Send text data with specified encoding (UTF-8, ASCII, UTF-16, UTF-32, ISO-8859-1) |
SendBinaryData |
Send raw binary data to a connected port |
ReadLine |
Read a line of text from a port with timeout |
ReadBinaryData |
Read binary data from a port with max bytes and timeout |
GetPortInfo |
Get detailed information about a specific port |
ConnectByUsbId |
Connect by USB VID/PID for reliable device identification |
FindPortByUsbId |
Find ports matching VID/PID without connecting |
Parameter Constraints
The feature uses SiLA2 constraints for input validation:
| Parameter | Constraint | Values |
|---|---|---|
StopBits |
Set | None, One, OnePointFive, Two |
Parity |
Set | None, Odd, Even, Mark, Space |
DataBits |
Set | 5, 6, 7, 8 |
Encoding |
Set | UTF-8, ASCII, UTF-16, UTF-32, ISO-8859-1 |
BaudRate |
MinimalExclusive | > 0 |
BytesSent |
MinimalInclusive | >= 0 |
MaxBytes |
MinimalExclusive | > 0 |
Timeout |
Unit + MinimalExclusive | Milliseconds (ms), > 0 |
VendorId |
Pattern | [0-9A-Fa-f]{4} (4-digit hex) |
ProductId |
Pattern | [0-9A-Fa-f]{4} (4-digit hex) |
Defined Execution Errors
| Error | Description |
|---|---|
PortNotFound |
The specified serial port was not found |
PortInUse |
The serial port is in use by another process |
ConnectionFailed |
Failed to establish connection |
NotConnected |
Operation attempted on disconnected port |
TransmissionFailed |
Data transmission failed |
TimeoutError |
Operation timed out |
Data Types
SerialPortInfo - Structured information about a serial port:
PortName- System port name (e.g., "COM3", "/dev/ttyUSB0")Description- Friendly name or descriptionIsAvailable- Whether the port can be openedIsConnected- Whether this service has it connectedVendorId- USB VID (hexadecimal, e.g., "0403")ProductId- USB PID (hexadecimal, e.g., "6001")SerialNumber- USB device serial numberManufacturer- Device manufacturer name
Observable Properties
Subscribe to real-time port updates:
// Subscribe to available ports
using var call = client.Subscribe_AvailablePorts(new Subscribe_AvailablePorts_Parameters());
await foreach (var response in call.ResponseStream.ReadAllAsync())
{
foreach (var port in response.AvailablePorts)
{
Console.WriteLine($"{port.SerialPortInfo.PortName.Value}: " +
$"{port.SerialPortInfo.Description.Value}");
}
}
USB Device Identification
Connect to devices by VID/PID for reliable identification regardless of port assignment:
// Find FTDI devices (VID=0403)
var ftdiPorts = await client.FindPortByUsbIdAsync(new FindPortByUsbId_Parameters
{
VendorId = new String { Value = "0403" },
ProductId = new String { Value = "6001" }
});
// Connect by USB ID
var result = await client.ConnectByUsbIdAsync(new ConnectByUsbId_Parameters
{
VendorId = new String { Value = "0403" },
ProductId = new String { Value = "6001" },
SerialNumber = new String { Value = "" }, // Match any
BaudRate = new Integer { Value = 115200 },
DataBits = new Integer { Value = 8 },
StopBits = new String { Value = "One" },
Parity = new String { Value = "None" }
});
Console.WriteLine($"Connected to port: {result.PortName.Value}");
Text vs Binary Data Transfer
The feature provides two methods for sending data:
SendTextData
Use for string/text data that needs character encoding:
await client.SendTextDataAsync(new SendTextData_Parameters
{
PortName = new String { Value = "COM3" },
Data = new String { Value = "MEASURE?" },
Encoding = new String { Value = "ASCII" } // or UTF-8, UTF-16, UTF-32, ISO-8859-1
});
SendBinaryData
Use for raw byte transmission:
await client.SendBinaryDataAsync(new SendBinaryData_Parameters
{
PortName = new String { Value = "COM3" },
Data = new Binary { Value = ByteString.CopyFrom(new byte[] { 0x02, 0x00, 0x10, 0x03 }) }
});
Server Implementation
To implement the service in your SiLA2 server:
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add SiLA2 services
builder.Services.AddSingleton<ISiLA2Server, SiLA2Server>();
builder.Services.AddSerialPortManager();
builder.Services.AddSingleton<SerialCommunicationService>();
builder.Services.AddGrpc();
var app = builder.Build();
// Initialize SiLA2
var silaServer = app.Services.GetRequiredService<ISiLA2Server>();
app.InitializeSiLA2Features(silaServer);
// Map gRPC services
app.MapGrpcService<SerialCommunicationService>();
silaServer.Start();
app.Run();
}
}
Feature Definition Location
The SiLA2 Feature Definition XML file is embedded in the assembly:
Features/SerialCommunication-v1_0.sila.xml
At runtime, copy this file to your output directory or include it as embedded resource.
Related Packages
- SiLA2.SerialPort: Core serial port library (non-gRPC)
- SiLA2.Core: Core SiLA2 server implementation
- SiLA2.AspNetCore: ASP.NET Core integration
Platform Support
- Windows: Full support with native COM port and WMI-based USB enumeration
- Linux: Supported via .NET (ports at /dev/ttyUSB*, /dev/ttyACM*)
- macOS: Supported via .NET (ports 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
- SiLA2.Core (>= 10.2.4)
- SiLA2.SerialPort (>= 10.2.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.