Skip to main content

modbus-connection

A small, backend-neutral Modbus connection abstraction.

The top-level modbus_connection package is a pure interface — the ModbusConnection / ModbusUnit Protocols and a tiny exception hierarchy — so consumers can type against it without committing to a backend. Two interchangeable backends implement it (pymodbus and tmodbus); 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[pymodbus]"   # pymodbus backend
pip install "modbus-connection[tmodbus]"    # tmodbus backend

Example

Model a device once, then connect, 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.tmodbus import connect_tcp
from modbus_connection.model import Component, gauge, uint32, coil


class Meter(Component):
    voltage = gauge(0, 0.1, unit="V")     # scaled 16-bit register
    current = gauge(1, 0.1, unit="A")
    energy = uint32(2, unit="Wh")         # 32-bit over two registers
    relay = coil(0, writable=True)


async def main() -> None:
    conn = await connect_tcp("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 query helper, 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 pymodbus
uv run pytest

Formatting/linting is ruff, enforced in CI. 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-3.4.0.tar.gz (163.3 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-3.4.0-py3-none-any.whl (62.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modbus_connection-3.4.0.tar.gz
  • Upload date:
  • Size: 163.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for modbus_connection-3.4.0.tar.gz
Algorithm Hash digest
SHA256 b64031484327cd4cd8eabce7349c9066a49642d912830dece8fab9b9d359e523
MD5 f777f9c4e8b37ec4928571c9a0969710
BLAKE2b-256 b92a208279534a5acf04aadc7c3bc41b95f3d28bb7f4d3547ebc97b317a81d44

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_connection-3.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-3.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for modbus_connection-3.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac989dd88d4270e8580576c85d0ab08bab946972c76c810d16bd7d62d679d8c4
MD5 d8736dcd2cc45a14ab6e0038ad83e7f5
BLAKE2b-256 32ea0ffe96b3fa4b55a23546afa0a58dcdbd05ded953c3777fda1caf0d46d86d

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_connection-3.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.

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