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
| Check | Example | Where to find it |
|---|---|---|
| Port name | COM3 | Windows Device Manager |
| Parameters | 9600, 8N1 | Device manual or field configuration |
| Protocol | Raw bytes, custom frame, Modbus RTU | Device 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
| Option | Default | Meaning |
|---|---|---|
PortName | COM1 | OS-visible port name |
BaudRate | 115200 | Bits per second; must match the device |
DataBits | 8 | Data bits, usually 8 |
Parity | None | None / Even are common |
StopBits | One | Usually one stop bit |
ReadTimeoutMilliseconds | 1000 | Read timeout |
WriteTimeoutMilliseconds | 1000 | Write timeout |
Pairing with Protocols
| Device protocol | Recommended approach |
|---|---|
| Manual gives fixed hex commands | Use WriteAsync and process DataReceived |
| Header + length + checksum | Use Custom Framing |
| Modbus RTU | Use Modbus; do not hand-build CRC |
| Omron Host Link | Use Omron Host Link |
Common Issues
| Symptom | Likely cause | Fix |
|---|---|---|
| Port is busy | Another program owns it | Close serial tools and retry |
COM3 not found | Port changed or driver is missing | Check Device Manager |
| Port opens but no data | Wiring, baud rate, parity, or device behavior | Verify with a serial tool |
| Garbled text | Binary protocol displayed as text | Inspect raw bytes with Convert.ToHexString |
| Modbus timeout | Unit id, function code, RTU/TCP mode, or CRC issue | Test with a virtual Modbus slave first |
Without hardware, keep using Virtual Channel.