Skip to main content

modbus-connection

A small, backend-neutral Modbus connection abstraction.

The top-level modbus_connection package provides the abstract ModbusConnection, the ModbusUnit Protocol, and a tiny exception hierarchy. Two interchangeable backends implement them (tmodbus and pymodbus); the bare install pulls neither.

One physical Modbus link addresses many units (1–247). Sharing a single, internally-serialized connection across many consumers is strictly better than each opening a competing socket. This package is the connection abstraction that makes that sharing possible while keeping the backend swappable.

Install

pip install "modbus-connection[tmodbus]"    # tmodbus backend
pip install "modbus-connection[pymodbus]"   # pymodbus backend

Example

Model a device once, then construct, update, read, and write it. The optional modbus_connection.model framework maps a device's registers and coils to typed attributes and reads the whole device in as few Modbus calls as possible.

import asyncio

from modbus_connection import ModbusTcpParams
from modbus_connection.model import Component, gauge, uint32, coil
from modbus_connection.tmodbus import ModbusConnection


class Meter(Component):
    voltage = gauge(0, 0.1, unit="V")  # scaled 16-bit register
    """Grid voltage."""

    current = gauge(1, 0.1, unit="A")
    """Grid current."""

    energy = uint32(2, unit="Wh")  # 32-bit over two registers
    """Lifetime energy."""

    relay = coil(0, writable=True)
    """Load relay."""


async def main() -> None:
    conn = ModbusConnection(ModbusTcpParams(host="192.168.1.50", port=502))
    try:
        meter = Meter(conn.for_unit(1))

        await meter.async_update()  # one pooled block read
        print(meter.voltage, meter.current, meter.energy, meter.relay)

        await meter.write("relay", True)  # write a writable field
    finally:
        await conn.close()


asyncio.run(main())

Documentation

Everything else — the other transports (UDP, serial, TLS), the full field-type and read-planning reference, repeated sub-units, the SunSpec field types and model generator, the in-memory mock backend for tests, and the exception hierarchy — lives on the website:

https://home-assistant-libs.github.io/modbus-connection/

Develop

uv sync --extra tmodbus
uv run pytest

The suite runs both backends against a real in-process Modbus server (tmodbus's, over TCP, UDP, RTU-over-TCP, serial and TLS) rather than against mocks, so it covers actual framing and error responses — and running the pymodbus client against the tmodbus server checks the two implementations against each other. tests/conftest.py has the datastore and the server helpers.

Formatting/linting is ruff and type-checking is mypy, both enforced in CI. Run them locally with:

uv run mypy

Install the commit hook with prek so code is formatted on commit:

uvx prek install          # set up the git hook
uvx prek run --all-files  # format + lint everything now

Download files

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

Source Distribution

modbus_connection-4.4.0.tar.gz (234.4 kB view details)

Uploaded Source

Built Distribution

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

modbus_connection-4.4.0-py3-none-any.whl (74.0 kB view details)

Uploaded Python 3

File details

Details for the file modbus_connection-4.4.0.tar.gz.

File metadata

  • Download URL: modbus_connection-4.4.0.tar.gz
  • Upload date:
  • Size: 234.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for modbus_connection-4.4.0.tar.gz
Algorithm Hash digest
SHA256 02b8420e66535ebb182ff46de9c57c3c41242f6e10faedb21b5e5daf1b2b23fa
MD5 9b4ce9c095fb8a74748960a3d676571d
BLAKE2b-256 0a8f7b4fc7587c5a0d69e586b0d3f9e03e0c8f93b15956d6568a04eedccfe45a

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_connection-4.4.0.tar.gz:

Publisher: publish.yml on home-assistant-libs/modbus-connection

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

File details

Details for the file modbus_connection-4.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for modbus_connection-4.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 664c607d19555584619c492682ce4a25a58fbdb67410958c6b7b23c7fed97bce
MD5 39b05aebaf0e0f544d0db802e6b8af31
BLAKE2b-256 76fd01a80e0e2cd88d2286734cd3b18f43080031f8ee7bac263d9cacec02955c

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_connection-4.4.0-py3-none-any.whl:

Publisher: publish.yml on home-assistant-libs/modbus-connection

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

Release history Release notifications | RSS feed

4.8.1

2 files

4.8.0

2 files

4.7.0

2 files

4.6.1

2 files

4.6.0

2 files

4.5.1

2 files

4.5.0

2 files

This release

4.4.0 This release

2 files

4.3.0

2 files

4.2.0

2 files

4.1.0

2 files

4.0.0

2 files

3.9.0

2 files

3.8.1

2 files

3.8.0

2 files

3.7.0

2 files

3.6.0

2 files

3.5.0

2 files

3.4.2

2 files

3.4.1

2 files

3.4.0

2 files

3.3.0

2 files

3.2.0

2 files

3.1.0

2 files

3.0.0

2 files

2.0.1

2 files

2.0.0

2 files

1.0.0

2 files

Supported by

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