Skip to main content

Console Sample

This page walks through the smallest loop: create a host, declare a channel, start, write, receive an echo, and stop.

Run the Repository Sample

From the repository code directory:

dotnet run --project samples/Zeus.Samples.Console.Headless

If you see PING, the basic Zeus pipeline works.

Complete Code

using System.Text;
using Zeus;

await using var app = ZeusHost.Create(builder =>
{
builder.AddVirtualChannel("meter");
});

var meter = app.Channels.Get("meter");
meter.DataReceived += (_, e) =>
{
var text = Encoding.ASCII.GetString(e.Data.Span);
Console.WriteLine($"Echo received: {text}");
};

await app.StartAsync();
Console.WriteLine($"Channel {meter.Name} state: {meter.State}");

await meter.WriteAsync(Encoding.ASCII.GetBytes("PING"));
await Task.Delay(200);

await app.StopAsync();
Console.WriteLine("Host stopped.");

What Each Part Does

CodeMeaning
ZeusHost.CreateBuilds the Zeus host and registers components
AddVirtualChannel("meter")Creates a virtual channel that echoes writes
app.Channels.Get("meter")Retrieves the channel by Zeus name
DataReceivedReceives bytes from real or virtual channels
StartAsyncOpens all registered channels
WriteAsyncWrites bytes to the channel
StopAsyncStops services and closes channels

Replace with a Real Serial Port

After the virtual channel works, replace:

builder.AddVirtualChannel("meter");

with:

builder.AddSerialPort("meter", "COM3", 9600);

Keep the remaining code unchanged. That isolates whether a failure is in the application structure or the field connection.

Next: First WinForms App or First WPF App.