Skip to main content

modbus-connection

A backend-neutral Modbus connection abstraction for Python.

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

One physical Modbus link addresses many units (1–247). Many consumers can share one serialized connection instead of 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 read per space
        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

The website documents the rest: the other transports (UDP, serial, TLS), the field types and read planning, repeated sub-units, the Device base class for a device library, the SunSpec field types and model generator, the in-memory mock backend for tests, and the exception hierarchy.

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

Develop

uv sync --extra tmodbus
uv run pytest

The suite runs both backends against an in-process tmodbus server over TCP, UDP, RTU-over-TCP, serial and TLS, so it covers real framing and error responses. Running the pymodbus client against the tmodbus server also checks the two implementations against each other. tests/conftest.py has the datastore and the server helpers.

Formatting and linting is ruff and type checking is mypy. CI enforces both. 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

Metadata

Release files for modbus-connection 4.12.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modbus-connection 4.12.4
File Size Uploaded
modbus_connection-4.12.4.tar.gz 278.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for modbus-connection 4.12.4
File Interpreter ABI Platform
modbus_connection-4.12.4-py3-none-any.whl Python 3 none any Details

Total release size: 365.5 kB

Release files / modbus_connection-4.12.4.tar.gz

Download URL modbus_connection-4.12.4.tar.gz
Size 278.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5fbdf3f62e6e9be4c85998f29a06310128712a2f0ddcc73bcadb60cd64146c23
BLAKE2b-256 checksum
How to use checksums
3e168973af2108a9648e93ad589aaa0e5f00a38d713a1eab6a347bf0877b6df5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 9, 2026.

Transparency log

Release files / modbus_connection-4.12.4-py3-none-any.whl

Download URL modbus_connection-4.12.4-py3-none-any.whl
Size 86.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e87b63079c40058fc1844475afd92939c1072da36382e8eb376e9b9f763ad4ac
BLAKE2b-256 checksum
How to use checksums
780e041ba969a0014283174332b64a2a15a61652ea8263520d780b04d4746755
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 9, 2026.

Transparency log

Release history Release notifications | RSS feed

4.12.5

2 release files

This release

4.12.4 This release

2 release files

4.12.3

2 release files

4.12.2

2 release files

4.12.1

2 release files

4.12.0

2 release files

4.10.0

2 release files

4.9.0

2 release files

4.8.1

2 release files

4.8.0

2 release files

4.7.0

2 release files

4.6.1

2 release files

4.6.0

2 release files

4.5.1

2 release files

4.5.0

2 release files

4.4.0

2 release files

4.3.0

2 release files

4.2.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.9.0

2 release files

3.8.1

2 release files

3.8.0

2 release files

3.7.0

2 release files

3.6.0

2 release files

3.5.0

2 release files

3.4.2

2 release files

3.4.1

2 release files

3.4.0

2 release files

3.3.0

2 release files

3.2.0

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page