Z21lib 1.3.0

dotnet add package Z21lib --version 1.3.0
                    
NuGet\Install-Package Z21lib -Version 1.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Z21lib" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Z21lib" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Z21lib" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Z21lib --version 1.3.0
                    
#r "nuget: Z21lib, 1.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Z21lib@1.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Z21lib&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Z21lib&version=1.3.0
                    
Install as a Cake Tool

Z21lib

A modern, asynchronous, and high-performance .NET library for communication with Roco/Fleischmann Z21 digital command stations.

The library fully implements the Z21 LAN Protocol (Firmware V1.43) specification and is designed with an emphasis on maximum throughput and minimum memory footprint.

Features

  • Modern .NET: Native support for .NET 8, 9, and 10.
  • High Performance: Utilizes ReadOnlySpan<byte> and ArrayPool<T> to eliminate unnecessary allocations (zero-allocation message building).
  • Asynchronous I/O: Non-blocking network communication over UDP using ReceiveAsync and asynchronous channels (Channel<T>).
  • Multi-instance: Supports running multiple applications (clients) simultaneously on a single PC thanks to dynamic local port allocation.
  • Thread Safety: Fully thread-safe message sending and processing.
  • Modern Resource Disposal: Implements IAsyncDisposable for safe and clean connection termination.

Installation

The library is available as a NuGet package.

dotnet add package Z21lib

Basic usage

Example of connecting to the command station, subscribing to events, and sending basic commands:

using Z21lib;
using Z21lib.Structs;
using Z21lib.Messages;

// Set the IP address and port of the command station (default port is 21105)
var info = new Z21Info("192.168.0.111", 21105);

// Using 'await using' ensures asynchronous and safe disconnection (IAsyncDisposable)
await using var client = new Z21Client(info);

// Subscribe to events
client.MessageReceived += message =>
{
    if (message is SerialNumberMessage serialMsg)
    {
        Console.WriteLine($"Command station serial number: {serialMsg.SerialNumber}");
    }
    else
    {
        Console.WriteLine($"Received message: {message.MessageType}");
    }
};

// Connect to the command station
if (client.Connect())
{
    Console.WriteLine("Successfully connected to Z21.");

    // Send a request for the serial number
    client.LanGetSerialNumber();

    // Turn on track power
    client.LanXSetTrackPower(true);

    // Wait for incoming messages (simulating application run)
    await Task.Delay(5000);

    // Disconnect
    await client.DisconnectAsync();
}
else
{
    Console.WriteLine("Connection failed.");
}

Running Multiple Applications on a Single PC

By default, the library requests a dynamic local port from the operating system. This means you can run several applications using Z21lib on the same computer, and they will all communicate seamlessly with the same command station.

If you need to strictly bind the local port due to network configuration or firewall rules, pass the useFixedLocalPort: true parameter to the constructor:

var client = new Z21Client(info, useFixedLocalPort: true);

Advanced Commands

The library includes methods for all standard operations according to the specification, such as:

  • Locomotive control (driving, functions)

  • Reading and writing CV registers (POM and programming track)

  • Controlling turnouts and accessories

  • Processing RailCom data

  • Forwarding messages from LocoNet and CAN bus

Additional examples can be found in the TestClient test project within the repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.0 106 3/23/2026