Skip to main content

Serial Port

Serial channels connect USB-to-serial adapters, RS232/RS485 converters, and other devices exposed as COM ports. Zeus opens, closes, writes, and raises receive events so you do not need to use System.IO.Ports.SerialPort directly.

Confirm Three Things First

CheckExampleWhere to find it
Port nameCOM3Windows Device Manager
Parameters9600, 8N1Device manual or field configuration
ProtocolRaw bytes, custom frame, Modbus RTUDevice protocol manual

Knowing only the COM port is not enough. A wrong baud rate, parity, stop bit, or protocol may still let the port open but return invalid data.

Minimal Usage

await using var app = ZeusHost.Create(builder =>
{
builder.AddSerialPort("meter", "COM3", 9600);
});

var meter = app.Channels.Get("meter");
meter.DataReceived += (_, e) =>
{
Console.WriteLine(Convert.ToHexString(e.Data.Span));
};

await app.StartAsync();
await meter.WriteAsync(new byte[] { 0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B });

meter is the Zeus channel name; COM3 is the operating-system port name.

Full Options

builder.AddSerialPort("meter", options =>
{
options.PortName = "COM3";
options.BaudRate = 9600;
options.DataBits = 8;
options.Parity = System.IO.Ports.Parity.Even;
options.StopBits = System.IO.Ports.StopBits.One;
options.ReadTimeoutMilliseconds = 500;
options.WriteTimeoutMilliseconds = 500;
});

Option Meaning

OptionDefaultMeaning
PortNameCOM1OS-visible port name
BaudRate115200Bits per second; must match the device
DataBits8Data bits, usually 8
ParityNoneNone / Even are common
StopBitsOneUsually one stop bit
ReadTimeoutMilliseconds1000Read timeout
WriteTimeoutMilliseconds1000Write timeout

Pairing with Protocols

Device protocolRecommended approach
Manual gives fixed hex commandsUse WriteAsync and process DataReceived
Header + length + checksumUse Custom Framing
Modbus RTUUse Modbus; do not hand-build CRC
Omron Host LinkUse Omron Host Link

Common Issues

SymptomLikely causeFix
Port is busyAnother program owns itClose serial tools and retry
COM3 not foundPort changed or driver is missingCheck Device Manager
Port opens but no dataWiring, baud rate, parity, or device behaviorVerify with a serial tool
Garbled textBinary protocol displayed as textInspect raw bytes with Convert.ToHexString
Modbus timeoutUnit id, function code, RTU/TCP mode, or CRC issueTest with a virtual Modbus slave first

Without hardware, keep using Virtual Channel.