Skip to main content

Serial

Overview

PortDIC provides a serial port communication handler (ISerialHandler) backed by portserial.dll (Rust FFI). It supports RS232 and RS485 communication with configurable baud rate, data bits, parity, stop bits, background reading, auto-reconnection, and file logging.

Each [Serial] class registers its own independent COM port — multiple ports can be open simultaneously.


Quick Setup

1. Define a serial handler class

using Portdic;
using Portdic.Serial;

[Serial]
public class MySerialHandler
{
[SerialHandler]
public ISerialHandler handler { get; set; } = null!;

[Preset]
private void Preset()
{
handler.SetPortName("COM3");
handler.SetBaudRate(9600);
handler.SetDataBits(8);
handler.SetParity(SerialParity.None);
handler.SetStopBits(SerialStopBits.One);
handler.SetTimeout(1000);

handler.OnDataReceived += OnData;
handler.OnEvent += OnEvent;
}

private void OnData(string portName, byte[] data, string hex)
{
Console.WriteLine($"[{portName}] Received: {hex}");
}

private void OnEvent(string portName, string eventType, string description)
{
Console.WriteLine($"[{portName}] {eventType}: {description}");
}
}

2. Register and run

Port.Add<MySerialHandler>("serial_com3");
Port.Run();

Multiple COM Ports

Each [Serial] class registration creates its own independent port instance:

Port.Add<SerialHelper_COM3>("serial_com3");
Port.Add<SerialHelper_COM4>("serial_com4");
[Serial]
public class SerialHelper_COM3
{
[SerialHandler]
public ISerialHandler handler { get; set; } = null!;

[Preset]
private void Preset()
{
handler.SetPortName("COM3");
handler.SetBaudRate(9600);
handler.SetDataBits(8);
handler.SetParity(SerialParity.None);
handler.SetStopBits(SerialStopBits.One);
handler.SetTimeout(1000);
handler.OnDataReceived += OnData;
handler.OnEvent += OnEvent;
}

private void OnData(string portName, byte[] data, string hex)
=> Console.WriteLine($"[COM3] {hex}");

private void OnEvent(string portName, string eventType, string description)
=> Console.WriteLine($"[COM3] {eventType}: {description}");
}

[Serial]
public class SerialHelper_COM4
{
[SerialHandler]
public ISerialHandler handler { get; set; } = null!;

[Preset]
private void Preset()
{
handler.SetPortName("COM4");
handler.SetBaudRate(115200);
handler.SetDataBits(8);
handler.SetParity(SerialParity.None);
handler.SetStopBits(SerialStopBits.One);
handler.SetTimeout(500);
handler.OnDataReceived += OnData;
handler.OnEvent += OnEvent;
}

private void OnData(string portName, byte[] data, string hex)
=> Console.WriteLine($"[COM4] {hex}");

private void OnEvent(string portName, string eventType, string description)
=> Console.WriteLine($"[COM4] {eventType}: {description}");
}

API Reference

Attributes

AttributeTargetDescription
[Serial]ClassMarks the class as a serial handler container
[SerialHandler]PropertyInjects the ISerialHandler instance
[Preset]MethodCalled before Open() to configure the handler

Configuration methods

MethodDescription
SetPortName(string portName)COM port name (e.g., "COM3", "COM4")
SetBaudRate(int baudRate)Baud rate (e.g., 9600, 19200, 38400, 57600, 115200)
SetDataBits(int dataBits)Data bits: 5, 6, 7, or 8 (default)
SetParity(SerialParity parity)Parity mode (see table below). Default: None
SetStopBits(SerialStopBits stopBits)Stop bits. Default: One
SetTimeout(int timeoutMs)Read timeout in ms. Default: 1000

SerialParity enum

ValueDescription
SerialParity.NoneNo parity bit
SerialParity.OddOdd parity
SerialParity.EvenEven parity

SerialStopBits enum

ValueDescription
SerialStopBits.One1 stop bit
SerialStopBits.Two2 stop bits

Connection methods

MethodReturnsDescription
Open()ERROR_CODEOpen the serial port and start background reading
Close()ERROR_CODEClose the serial port
IsConnectedbooltrue if the port is currently open
GetAvailablePorts()string[]Returns all available COM port names on the system

Send methods

MethodReturnsDescription
Send(byte[] data)intSend raw bytes
Send(string text)intSend UTF-8 string

Returns the number of bytes sent, or -1 on error.

Reading

MethodDescription
StartReading()Start background reading (auto-called by Open())
StopReading()Stop background reading

Auto-reconnection

MethodDescription
SetAutoConnection(bool enable, int intervalMs)Enable auto-reconnect when the port disconnects. Retries every intervalMs ms
TryReconnect()Manually restart the reconnect loop
handler.SetAutoConnection(true, 1000); // retry every 1 second

Logging

MethodDescription
SetLogger(string rootPath)Enable hourly-rotated SEND/RECV log files at rootPath
SetLogger(string rootPath, PortLogConfiguration conf)Logging with custom rotation/retention settings
WriteLog(string v)Write a custom entry to the active log file

Log file format: serial_{portName}_{date}_{hour}.log


Events

OnDataReceived

Fired when data arrives from the serial port.

handler.OnDataReceived += (string portName, byte[] data, string hex) =>
{
Console.WriteLine($"[{portName}] {data.Length} bytes: {hex}");
};
ParameterDescription
portNameCOM port name (e.g., "COM3")
dataRaw received bytes
hexHex string (e.g., "48 65 6C 6C 6F")

OnEvent

Fired on connection state changes.

handler.OnEvent += (string portName, string eventType, string description) =>
{
Console.WriteLine($"[{portName}] {eventType}: {description}");
};
eventTypeTriggered When
CONNECTEDSerial port opened successfully
DISCONNECTEDSerial port closed
ERRORError opening or communicating (description contains detail)

Port Discovery

List all available COM ports before configuring:

var ports = handler.GetAvailablePorts();
foreach (var port in ports)
Console.WriteLine(port); // e.g., COM3, COM4, COM7

Error Codes

CodeValueMeaning
ERR_CODE_NO_ERROR1Success
ERR_CODE_OPEN-1Open failed
ERR_CODE_DLL_NOT_LOADED-2portserial.dll not loaded
ERR_CODE_PORTNAME_EMPTY-3Port name not set
ERR_CODE_DLL_FUNC_NOT_CONFIRM-4Required DLL function unavailable
ERR_CODE_CONNECT_FAILED-5Connection attempt failed

Common Configurations

Device TypeBaudDataParityStopNotes
Arduino / general96008None1Default settings
Industrial RS485192008None1Half-duplex
High-speed device1152008None1Fast transfer
Legacy equipment96007Even2Older protocols

  • TCP — Ethernet TCP client/server communication
  • MQTT — Lightweight pub/sub messaging
  • SECS/GEM — Semiconductor equipment protocol (HSMS over TCP)