Skip to main content

Async ModBus python library

Project description

Async ModBus

Async Modbus

AIO Modbus updates

Asynchronous (as in python async/await) modbus python 3 client library. It provides an OO client API. It is a thin layer on on top of the venerable umodbus library.

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

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

from async_modbus import modbus_for_url


async def main():
    client = modbus_for_url("tcp://localhost:15020")

    values = [1, 0, 1, 1]
    reply = await client.write_multiple_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


asyncio.run(main())

RTU over remove 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_multiple_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


asyncio.run(main())

asyncio TCP streams

import asyncio

from async_modbus import AsyncTCPClient


async def main():
    reader, writer = await asyncio.open_connection('localhost', 15020)
    client = AsyncTCPClient((reader, writer))
    values = [1, 0, 1, 1]
    reply = await client.write_multiple_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

    writer.close()
    await writer.wait_closed()


asyncio.run(main())

async serial line RTU using remote raw TCP

import asyncio

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 = [1, 0, 1, 1]
    reply = await client.write_multiple_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

    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)
    stream = sock.as_stream()
    client = AsyncTCPClient(stream)
    values = [1, 0, 1, 1]
    reply = await client.write_multiple_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
    await sock.close()

Credits

Development Lead

Contributors

None yet. Why not be the first?

Special thanks to

History

0.1.0 (2020-11-08)

  • First release on PyPI.

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

async_modbus-0.1.0.tar.gz (9.7 kB view details)

Uploaded Source

File details

Details for the file async_modbus-0.1.0.tar.gz.

File metadata

  • Download URL: async_modbus-0.1.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0.post20201006 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.7

File hashes

Hashes for async_modbus-0.1.0.tar.gz
Algorithm Hash digest
SHA256 86f4f98e64a5a3e8032532e51d8d1a8986433605b3b9d7274541c7e406fbf76a
MD5 7b2c4cb7994e63149e2945b0aeee4418
BLAKE2b-256 13da0a8f590375a6a5085a4a9070e82fd5bdc648b9a75c947f709c0233e09b89

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page