SiLA2 SerialPort module for discovering and controlling serial connections (USB/RS232). Provides port discovery, multi-port connection management, and data transfer capabilities for laboratory instruments.
$ dotnet add package SiLA2.SerialPortA .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.
dotnet add package SiLA2.SerialPort
// In Program.cs or Startup.cs
builder.Services.AddSerialPortManager();
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}");
}
}
}
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();
}
}
// 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);
});
| 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 |
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}");
};
manager.ConnectionOpened += (sender, connection) =>
{
Console.WriteLine($"Connected to {connection.PortName}");
};
manager.ConnectionClosed += (sender, portName) =>
{
Console.WriteLine($"Disconnected from {portName}");
};
For gRPC exposure of serial communication, use the SiLA2.SerialPort.Features package which provides a complete SiLA2 Feature Definition and service implementation.
MIT License - See LICENSE file for details.