z21aio
Async Python library for Z21 DCC command station communication using UDP protocol.
Features
- Pure Python asyncio implementation
- Control locomotives (speed, direction, functions F0-F31)
- Track power control (on/off)
- System state monitoring
- Support for multiple simultaneous Z21 connections
- No external dependencies (stdlib only)
Requirements
- Python 3.10+
- Z21 command station (Roco/Fleischmann/RailBOX)
Installation
pip install z21aio
Or install from source:
git clone https://github.com/botmonster/z21aio.git
cd z21aio
pip install -e .
Quick Start
import asyncio
from z21aio import Z21Station, Loco
async def main():
# Connect to Z21 station
async with await Z21Station.connect("192.168.0.111") as station:
# Get serial number
serial = await station.get_serial_number()
print(f"Connected to Z21, serial: {serial}")
# Turn on track power
await station.voltage_on()
# Control locomotive at address 3
loco = await Loco.control(station, address=3)
await loco.set_headlights(True)
await loco.drive(50.0) # 50% forward
await asyncio.sleep(5)
await loco.stop()
await station.voltage_off()
asyncio.run(main())
Documentation
https://z21aio.readthedocs.io/en/stable/
API Reference
Z21Station
Main class for communicating with a Z21 command station.
# Connect to station
station = await Z21Station.connect(host, port=21105, timeout=2.0)
# Track power control
await station.voltage_on()
await station.voltage_off()
# Get station info
serial = await station.get_serial_number()
# Subscribe to system state updates
station.subscribe_system_state(callback, freq_hz=1.0)
# Clean disconnect
await station.logout()
await station.close()
Loco
Control a locomotive on the track.
# Get control of locomotive
loco = await Loco.control(station, address=3)
# Speed control (-100 to 100, negative = reverse)
await loco.drive(50.0) # 50% forward
await loco.drive(-30.0) # 30% reverse
await loco.stop() # Normal stop (with braking)
await loco.halt() # Emergency stop
# Function control (F0-F31)
await loco.set_headlights(True) # F0
await loco.function_on(2) # F2 on
await loco.function_off(2) # F2 off
await loco.function_toggle(3) # Toggle F3
# Get current state
state = await loco.get_state()
print(f"Speed: {state.speed_percentage}%")
DccThrottleSteps
Throttle step modes for locomotive control.
from z21aio import DccThrottleSteps
# Available modes
DccThrottleSteps.STEPS_14 # 14-step mode
DccThrottleSteps.STEPS_28 # 28-step mode
DccThrottleSteps.STEPS_128 # 128-step mode (default)
Examples
See the examples/ directory for complete working examples:
| File | Description |
|---|---|
| basic.py | Quick start - connect, power on, drive locomotive |
| multi_station.py | Connect to multiple Z21 stations simultaneously |
| speed.py | Speed control - forward, reverse, stop, emergency halt |
| functions.py | Locomotive function control (F0-F31) |
| monitor.py | System state monitoring (current, voltage, temperature) |
| loco_state.py | Get and subscribe to locomotive state updates |
Protocol Documentation
This library implements the Z21 LAN Protocol as documented by Roco/Fleischmann.
Development
Setup
git clone https://github.com/botmonster/z21aio.git
cd z21aio
python -m venv .venv
.venv/Scripts/activate # Windows
# source .venv/bin/activate # Linux/macOS
pip install -e ".[dev]"
Enable Pre-commit Hooks
To enable the pre-commit hooks that run flake8 and pytest before each commit:
git config core.hooksPath .githooks
This ensures code quality checks run locally before pushing to the repository.
Running Tests
pytest tests/ -v
License
MIT License - see LICENSE file for details.
Metadata
Release files for z21aio 1.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| z21aio-1.0.7.tar.gz | 42.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| z21aio-1.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 65.8 kB
Release files / z21aio-1.0.7.tar.gz
| Download URL | z21aio-1.0.7.tar.gz |
|---|---|
| Size | 42.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
732acdfafe56a65ae1f779737eace75fcf482fa31c93cd058c978c3380b690bb
|
|
BLAKE2b-256 checksum How to use checksums |
ca99b53a3ed41d88c938c676e843683d55854eda46438b993a794ba261d79c6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 28, 2026.
Transparency logRelease files / z21aio-1.0.7-py3-none-any.whl
| Download URL | z21aio-1.0.7-py3-none-any.whl |
|---|---|
| Size | 23.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7895842c30302ec846bbfbb494c21a619846bacb32d9967f93c59ac603007115
|
|
BLAKE2b-256 checksum How to use checksums |
80f16a9e3e3a75b8eca03260f9923b9dfb79a530b33d28ddddcaf8f4c52d6946
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 28, 2026.
Transparency log