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.
$ dotnet add package SiLA2.SerialPort.FeaturesA 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.
dotnet add package SiLA2.SerialPort.Features
This package requires SiLA2.Core and SiLA2.SerialPort which will be installed automatically as dependencies.
// 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>();
// 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 a SCPI command and get response
var response = await client.SendCommandAsync(new SendCommand_Parameters
{
PortName = new String { Value = "COM3" },
Command = new String { Value = "*IDN?" },
TimeoutMs = new Integer { Value = 5000 }
});
Console.WriteLine($"Device ID: {response.Response.Value}");
The SerialCommunication feature (FDL v1.0) provides:
| Property | Observable | Description |
|---|---|---|
AvailablePorts | Yes | List of available serial ports with detailed metadata |
ConnectedPorts | Yes | List of currently connected port names |
| Command | Description |
|---|---|
Connect | Connect to a serial port with configuration options |
Disconnect | Disconnect from a serial port |
SendData | Send string data to a connected port |
SendCommand | Send command and wait for response (SCPI-style) |
ReadLine | Read a line of text from a port |
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 |
| 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 |
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 nameSubscribe 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}");
}
}
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}");
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();
}
}
The SiLA2 Feature Definition XML file is embedded in the assembly:
Features/SerialCommunication-v1_0.sila.xmlAt runtime, copy this file to your output directory or include it as embedded resource.
MIT License - See LICENSE file for details.