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.11.0.tar.gz (5.4 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.11.0-cp311-cp311-win_amd64.whl (458.9 kB view details)

Uploaded CPython 3.11Windows x86-64

modbus_rs-0.11.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (603.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

modbus_rs-0.11.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (644.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

modbus_rs-0.11.0-cp311-cp311-macosx_11_0_arm64.whl (526.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

modbus_rs-0.11.0-cp311-cp311-macosx_10_12_x86_64.whl (506.4 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: modbus_rs-0.11.0.tar.gz
  • Upload date:
  • Size: 5.4 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.11.0.tar.gz
Algorithm Hash digest
SHA256 8c26500b50ea17896faa89090ae4dd0146ede1453846906bae702223c6e86ec7
MD5 777edf469ded6d74377ca506a371a546
BLAKE2b-256 2c80c3a95f964aa90ce4cac8fb1180c9238bbb92535419d76a4df2271758f501

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.11.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.11.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: modbus_rs-0.11.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 458.9 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.11.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2cde9df3e182ef424e8412bb8e24bdb2764c149c996bfe896dab00d88e984a18
MD5 c723b33835143ce9c53c1a6fea099c24
BLAKE2b-256 919acba358f59111a11edc0b59de850dbeaf6183e9036cfb76068337c5b605e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.11.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.11.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.11.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 58bc7fa13bfffedf1abd00a459d9f5cf04931b7f5c52e7859a89b91e3ee46c74
MD5 4a21d9ea6f32d647b514ef52cd7a4428
BLAKE2b-256 ab25a2ed2a1df070731223571f2de96e246bfe71cae88cbe93a536b74d1f1b6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.11.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.11.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.11.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 75f674d3f9fd4ce3e5ab4ff412cd9d7f35e2cf42cd9ce32b9d07df615530b715
MD5 890ec2ba501ee052ede68275179bdcf0
BLAKE2b-256 1afa47ce8e59899a7a356e29939112d697e45f57e371faf15902f9151a7be1c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.11.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.11.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.11.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5faeda3da5cd811d41c800080ea37dc267ef3a2ccfac454d9ee2dbba58216dfc
MD5 db0c23074ae548c155132b92b1e06f60
BLAKE2b-256 d89a0ff79da0a498916e0dcb2e37041ceba5b16cade90ffd85b02b3d1193bdbb

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.11.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.11.0-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.11.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ecb36e991f47bebd7efef2e8d6c322e0c22c4a77d1dc4d06dbc826b45c55a34b
MD5 e89ad96116283853ad129ef5151c39ca
BLAKE2b-256 899a45a6a9111f9bd425cda09fdffe50bab01dab848a9298a90e33479d230e94

See more details on using hashes here.

Provenance

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