Skip to main content

Async Python library for Z21 DCC command station communication

Project description

z21aio

Tests Documentation Status Python Version License: MIT

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)

Installation

pip install z21aio

Or install from source:

git clone https://github.com/botmonster/z21aio.git
cd z21-dcc
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())

Multi-Station Support

Connect to multiple Z21 stations simultaneously:

async def multi_station():
    async with await Z21Station.connect("192.168.0.111") as station1, \
               await Z21Station.connect("192.168.0.112") as station2:
        loco1 = await Loco.control(station1, address=3)
        loco2 = await Loco.control(station2, address=5)

        await loco1.drive(50.0)
        await loco2.drive(30.0)

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.

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

z21aio-0.1.0.tar.gz (32.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

z21aio-0.1.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file z21aio-0.1.0.tar.gz.

File metadata

  • Download URL: z21aio-0.1.0.tar.gz
  • Upload date:
  • Size: 32.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for z21aio-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8ae0b8d95d243878750dee2057d7671f2dc9e6e9784a3e439039ec59190b6552
MD5 b506d6a27c6653818291b41e30b48dd2
BLAKE2b-256 49a65b3b56d1c73e23ebb99a31734ad12e3f2131ca20c93d2a7e8c7d7a3dedc1

See more details on using hashes here.

Provenance

The following attestation bundles were made for z21aio-0.1.0.tar.gz:

Publisher: publish.yml on botmonster/z21aio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file z21aio-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: z21aio-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for z21aio-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3629e5c58fbb8df79a727fe62e505e94fa0788294ad8b98b04a4dd2b789ea095
MD5 c9cb4245e8e2206474e2dafd5495519b
BLAKE2b-256 76c88724b6f654d6431f6b7162910e4255f5c209e43fb60f36ee7ec956dcde7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for z21aio-0.1.0-py3-none-any.whl:

Publisher: publish.yml on botmonster/z21aio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page