Skip to main content

Async ModBus

Async Modbus Python Versions CI Status License pre-commit

Asynchronous (as in python async/await) modbus python 3 client library. A thin layer on on top of the venerable umodbus library providing an object oriented client API.

async_modbus is async library agnostic. You should be able to use it with asyncio, curio, trio or anyio.

It expects an object with the same interface as StreamReader and StreamWriter so you may need to write a thin wrapper if you are not using asyncio. The examples below show how to use it with curio.

Note: the modbus_for_url() relies on the connio library which relies on the asyncio event loop so you it's usage is limited to asyncio applications.

Why another modbus library?

This library is not a re-implementation of the ModBus communication protocol. You can view it instead as a complement to the umodbus library.

Here is what async_modbus provides on top of umodbus:

  • Simple yet powerful object oriented API
  • Convenient modbus_for_url() helper function. Type an URL and you're ready to go.
  • when appropriate, numpy arrays are used. It's usage not only reduces the memory footprint and increases speed, but also makes it easy for users to efficiently reformat data.
  • Compatible with the connio, sockio and serialio libraries which provide transparent socket re-connection among other features.

Installation

From within your favorite python environment type:

$ pip install async_modbus

Numpy will be used if installed. You can install it yourself, or include the optional dependency:

$ pip install async_modbus[numpy]

Library

The core of the async_modbus library consists of a modbus_for_url() function and the two classes AsyncTCPClient and AsyncRTUClient.

Here are some examples:

asyncio examples

simple TCP client

import asyncio

import numpy

from async_modbus import modbus_for_url


async def main():

    client = modbus_for_url("tcp://localhost:15020")

    values = numpy.array([1, 0, 1, 1])  # would also work with list<bool or int>
    reply = await client.write_coils(slave_id=1, starting_address=1, values=values)
    assert reply is len(values)

    reply = await client.read_coils(slave_id=1, starting_address=1, quantity=len(values))
    assert (reply == values).all()


asyncio.run(main())

RTU on local serial line with custom serial options

import asyncio

from async_modbus import modbus_for_url


async def main():

    client = modbus_for_url("serial:///dev/ttyS0", {"baudrate":19200, "parity":"E"})

    values = [1, 0, 1, 1]
    reply = await client.write_coils(slave_id=1, starting_address=1, values=values)
    assert reply is len(values)

    reply = await client.read_discrete_inputs(slave_id=1, starting_address=1, quantity=len(values))
    assert (reply == values).all()


asyncio.run(main())

RTU over remote serial line using RFC2217

import asyncio

from async_modbus import modbus_for_url


async def main():

    client = modbus_for_url("rfc2217://moxa.acme.org:6610")

    values = [1, 0, 1, 1]
    reply = await client.write_coils(slave_id=1, starting_address=1, values=values)
    assert reply is len(values)

    reply = await client.read_discrete_inputs(slave_id=1, starting_address=1, quantity=len(values))
    assert (reply == values).all()


asyncio.run(main())

asyncio TCP streams

import asyncio

import numpy

from async_modbus import AsyncTCPClient


async def main():

    reader, writer = await asyncio.open_connection('localhost', 15020)
    client = AsyncTCPClient((reader, writer))

    values = numpy.array([0, 2**15 - 1, 10, 3, 32766])
    reply = await client.write_registers(slave_id=1, starting_address=1, values=values)
    assert reply is len(values)

    reply = await client.read_holding_registers(slave_id=1, starting_address=1, quantity=len(values))
    assert (reply == values).all()

    writer.close()
    await writer.wait_closed()


asyncio.run(main())

async serial line RTU using remote raw TCP

import asyncio

import numpy

from async_modbus import AsyncRTUClient
from serial_asyncio import open_serial_connection


async def main():

    reader, writer = await open_serial_connection(url="socket://moxa.acme.org:6610")
    client = AsyncRTUClient((reader, writer))

    values = [0, 2**15 - 1, 10, 3, 32766]
    reply = await client.write_registers(slave_id=1, starting_address=1, values=values)
    assert reply is len(values)

    reply = await client.read_input_registers(slave_id=1, starting_address=1, quantity=len(values))
    assert (reply == values).all()

    writer.close()
    await writer.wait_closed()


asyncio.run(main())

curio examples

curio TCP streams

import curio
from async_modbus import AsyncTCPClient


async def main():

    sock = await curio.open_connection("0", 15020)
    client = AsyncTCPClient(sock.as_stream())

    values = [1, 0, 1, 1]
    reply = await client.write_coils(slave_id=1, starting_address=1, values=values)
    assert reply is len(values)

    reply = await client.read_coils(slave_id=1, starting_address=1, quantity=len(values))
    assert (reply == values).all()

    await sock.close()

Credits

Development Lead

Contributors

None yet. Why not be the first?

Special thanks to

Metadata

Release files for async-modbus 0.2.3

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

Source distribution (sdist)

Source distribution for async-modbus 0.2.3
File Size Uploaded
async_modbus-0.2.3.tar.gz 11.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for async-modbus 0.2.3
File Interpreter ABI Platform
async_modbus-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 20.6 kB

Release files / async_modbus-0.2.3.tar.gz

Download URL async_modbus-0.2.3.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c1dc66834209d4682b664a4e951bdd00333a8d98ff15e8d6ff5f541d437b18d8
BLAKE2b-256 checksum
How to use checksums
a59b3f03426e98532717234910ff2de3dd6036d3a6a5859bdf5eb652ba713716
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / async_modbus-0.2.3-py3-none-any.whl

Download URL async_modbus-0.2.3-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1de256788cd51f9e1b2d818ad9f186a09e402c9edd55fb800d65a370caf187e5
BLAKE2b-256 checksum
How to use checksums
3810f35879eae908a45017f6c9c5a8717bafbb39d7b45ef0fedb782ead8d495a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

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