Skip to main content

Fast, full-featured Modbus TCP + Serial bindings for Python — powered by Rust

Project description

modbus-rs (Python)

Fast Modbus TCP + Serial bindings for Python, powered by Rust.

  • PyPI package: modbus-rs
  • Import name: modbus_rs

Licensing

This package is available under GNU GPL v3.0 for open-source use.

Commercial licenses are also available for proprietary/closed-source use. Contact: ch.raghava44@gmail.com

Install

pip install modbus-rs

Quick Start

Synchronous TCP client

import modbus_rs

with modbus_rs.TcpClient("192.168.1.10", port=502, unit_id=1) as client:
    client.connect()
    regs = client.read_holding_registers(0, 10)
    print(regs)

Async TCP client

import asyncio
import modbus_rs

async def main():
    async with modbus_rs.AsyncTcpClient("192.168.1.10", unit_id=1) as client:
        regs = await client.read_holding_registers(0, 10)
        print(regs)

asyncio.run(main())

Serial client (RTU)

import modbus_rs

with modbus_rs.SerialClient("/dev/ttyUSB0", baud_rate=9600, unit_id=1) as client:
    client.connect()
    regs = client.read_holding_registers(0, 5)
    print(regs)

Async TCP server

import asyncio
import modbus_rs

class MyApp(modbus_rs.ModbusApp):
    def handle_read_holding_registers(self, address, count):
        return [address + i for i in range(count)]

async def main():
    server = modbus_rs.AsyncTcpServer("0.0.0.0", MyApp(), port=5020, unit_id=1)
    await server.serve_forever()

asyncio.run(main())

Exceptions

  • ModbusError
  • ModbusTimeout
  • ModbusConnectionError
  • ModbusProtocolError
  • ModbusDeviceException
  • ModbusConfigError
  • ModbusInvalidArgument

Build from Source

cd mbus-ffi
maturin develop --features python,full

Run Python Tests

cd mbus-ffi
maturin develop --features python,full
cd ..
pytest mbus-ffi/tests/python/ -q

Run Python Examples

The examples live in this repository under mbus-ffi/examples.

1) Build/install the extension from source

git clone https://github.com/Raghava-Ch/modbus-rs.git
cd modbus-rs/mbus-ffi
maturin develop --features python,full

2) Start the example server (terminal 1)

cd ../examples/python_server
python3 python_server.py --host 127.0.0.1 --port 5020 --unit-id 1

3) Run the sync client (terminal 2)

cd ../python_client
python3 python_client.py --host 127.0.0.1 --port 5020 --unit-id 1

4) Run the async client (terminal 2)

cd ../python_async_client
python3 async_client.py --host 127.0.0.1 --port 5020 --unit-id 1

Optional: multi-server async demo

Start 3 servers on ports 5020, 5021, and 5022, then run:

cd ../python_async_client
python3 async_client.py --host 127.0.0.1 --port 5020 --multi

Modbus TCP Gateway (python-gateway feature)

The python-gateway feature exposes a thread-safe sync gateway and an asyncio-friendly async gateway that forward inbound Modbus/TCP requests to one or more downstream Modbus/TCP servers based on a unit-id routing table.

Build with the gateway feature enabled:

cd mbus-ffi
maturin develop --features python,python-gateway,full

Sync gateway

import modbus_rs

gw = modbus_rs.TcpGateway("0.0.0.0:5020")
ch = gw.add_tcp_downstream("192.168.1.10", 502)
gw.add_unit_route(unit=1, channel=ch)
gw.serve_forever()  # blocks; call gw.stop() from another thread to exit

Async gateway

import asyncio
import modbus_rs

async def main():
    gw = modbus_rs.AsyncTcpGateway("0.0.0.0:5020")
    ch = await gw.add_tcp_downstream("192.168.1.10", 502)
    await gw.add_unit_route(unit=1, channel=ch)
    await gw.serve_forever()  # cancel the task or call gw.stop() to exit

asyncio.run(main())

Note: the optional event_handler= constructor argument is reserved for future telemetry callbacks. It is currently a forward-compatibility hook and does not yet receive frame events.

More Docs

  • Project docs: documentation/python_bindings.md
  • Full crate README (C/WASM/Python): mbus-ffi/README.md

License

Copyright (C) 2026 Raghava Challari

This project is licensed under GNU GPL v3.0. See LICENSE for details.

Commercial licenses for proprietary use are available via ch.raghava44@gmail.com.

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

modbus_rs-0.10.0.tar.gz (5.3 MB view details)

Uploaded Source

Built Distributions

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

modbus_rs-0.10.0-cp311-cp311-win_amd64.whl (444.2 kB view details)

Uploaded CPython 3.11Windows x86-64

modbus_rs-0.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (586.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

modbus_rs-0.10.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (625.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

modbus_rs-0.10.0-cp311-cp311-macosx_11_0_arm64.whl (513.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

modbus_rs-0.10.0-cp311-cp311-macosx_10_12_x86_64.whl (494.9 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

Details for the file modbus_rs-0.10.0.tar.gz.

File metadata

  • Download URL: modbus_rs-0.10.0.tar.gz
  • Upload date:
  • Size: 5.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for modbus_rs-0.10.0.tar.gz
Algorithm Hash digest
SHA256 91e502cd489a744e0a8b0b610e20408f8351411ab0f58367309dd61ddca1a62e
MD5 31339e9580741dff7e229d62fb2483bf
BLAKE2b-256 ef3313b99625b7b420de3920704472ed970254d74ac7a15e755fb92ac9524430

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.10.0.tar.gz:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

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_rs-0.10.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: modbus_rs-0.10.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 444.2 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for modbus_rs-0.10.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7fdaf7fd0c11880b17200b8b0bf5aba962d16b3fa47e42cf7b493d56f4a79e9f
MD5 15698c659b563565a9aff7c6b134853e
BLAKE2b-256 1e6b527ea775385b1c7997a074961bef03b4a5d6fc11fa5a39346daeedc954d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.10.0-cp311-cp311-win_amd64.whl:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

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_rs-0.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 72a6b2ed3a63e89e435dbd3709179f0e51962fe8e3aca0fc09d3d517462bae38
MD5 828e5817a779afacb187a8e04d7bcb21
BLAKE2b-256 027bc0d8b5f88152b781306a3a4090b31d3ee3a5e5b95f93f36401f45ddbd1cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

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_rs-0.10.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.10.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b4586e119c0590560b660864cf17d0687e0e5714b77e69b12cd0f36a6be94af0
MD5 e9d9ec85084bab134df48eee041a8cfe
BLAKE2b-256 2c90ef77a50ae7f93218d79116440141181c0baeace32b46b0e512443033c5c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.10.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

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_rs-0.10.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.10.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eae42b4df7fd864a80dce180c74146bf60bfba3d444638d9a59f0e352e6e9892
MD5 3ca3f05bd847f7c84f28403395b79c92
BLAKE2b-256 469c340a7a3e5a7231584a7d257eb5a728429f4037beb112dd674f008f0d65a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.10.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

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_rs-0.10.0-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.10.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 fc138f30ab4b20f6061e4033d6c52efd51725e20fcf3ea19154cd255c6548da3
MD5 67012b3f81b1259c3655ba920521c1e5
BLAKE2b-256 94cd600018de4f95b1dbe0ff9a22c6fba5a46bbae84b2890c9062c25f13d97f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.10.0-cp311-cp311-macosx_10_12_x86_64.whl:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

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