Skip to main content

This is my custom aiomodbus client

Project description

DM-aiomodbus

Asynchronous Modbus clients for Python with TCP and Serial connection support.

Links


Section Description
Installation How to install the package
Usage How to use the package
Types Types and classes used in the package
Inner Client Methods List of methods available for inner client

Installation

Check if you have Python 3.12.6 or higher installed:

python3 --version

Install the package using pip:

pip install dm-aiomodbus

Usage

Windows Setup

import asyncio
import sys

if sys.platform == "win32":
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

Code Example

import asyncio
from dm_aiomodbus import (
   DMAioModbusSerialClient, DMAioModbusSerialClientConfig,
   DMAioModbusTcpClient, DMAioModbusTcpClientConfig,
   DMAioModbusInnerClient
)


async def main():
    # Initialize Serial client
    serial_modbus_client = DMAioModbusSerialClient(
        config=DMAioModbusSerialClientConfig(
            port="/dev/ttyUSB0",
            baudrate=9600,  # default value
            bytesize=8,  # default value
            stopbits=2,  # default value
            parity="N",  # default value
            disconnect_timeout_s=20,  # default value
            error_logging=False  # default value
        )
    )

    # Initialize TCP client
    tcp_modbus_client = DMAioModbusTcpClient(
        config=DMAioModbusTcpClientConfig(
            host="129.168.1.5",
            port=502,  # default value
            disconnect_timeout_s=20,  # default value
            error_logging=False  # default value
        )
    )

    # Read/write register(s)
    async def callback(client: DMAioModbusInnerClient):
        await client.write_register(256, 1)
        result = await client.read_holding_registers(256, count=3)
        print(result)

    # Execute callback
    await serial_modbus_client.execute(callback)
    # or
    await tcp_modbus_client.execute(callback)


if __name__ == "__main__":
    asyncio.run(main())

Note: All read/write methods should be called inside your callback function


Types

Serial Client Config

class DMAioModbusSerialClientConfig:
    port: str  # Serial port name. Example: "/dev/ttyS0" - Linux UART, "/dev/ttyUSB0" - linux USB, "COM1" - Windows USB
    baudrate: int = 9600  # Baudrate in bits per second
    bytesize: Literal[7, 8] = 8  # Number of data bits
    stopbits: Literal[1, 2] = 2  # Number of stop bits
    parity: Literal["N", "E", "O"] = "N"  # Parity mode. N - None, E - Even, O - Odd
    disconnect_timeout_s: int = 20  # Timeout in seconds
    error_logging: bool = False  # Enable error logging

TCP Client Config

class DMAioModbusTcpClientConfig:
    host: str  # IP address of the device
    port: int = 502  # Port number
    disconnect_timeout_s: int = 20  # Timeout in seconds
    error_logging: bool = False  # Enable error logging

Read Response

class DMAioModbusReadResponse:
    data: list[int]  # List of values read from device
    error: str  # Error message if operation failed

Note: This class has to_dict() method that returns a dictionary

Warning: If the operation failed, the data field will be an empty list

Write Response

class DMAioModbusWriteResponse:
    status: bool  # Boolean indicating success or failure
    error: str  # Error message if operation failed

Note: This class has to_dict() method that returns a dictionary

Warning: The status is considered True, if the operation did not result in an error.


Inner Client Methods

Method Arguments Response Type
read_coils - address int
- count int = 1
- slave int= 1
ReadResponse
read_discrete_inputs - address int
- count int = 1
- slave int= 1
ReadResponse
read_holding_registers - address int
- count int = 1
- slave int= 1
ReadResponse
read_input_registers - address int
- count int = 1
- slave int= 1
ReadResponse
write_coil - address int
- value int
- slave int = 1
WriteResponse
write_register - address int
- value int
- slave int = 1
WriteResponse
write_coils - address int
- values list[int] | int
- slave int= 1
WriteResponse
write_registers - address int
- values list[int] | int
- slave int= 1
WriteResponse

Parameters Description

  • address: Register address (single integer)
  • count: Number of items to read (default: 1)
  • value/values: Value(s) to write (single integer or list of integers)
  • slave: Slave unit address (default: 1)

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

dm_aiomodbus-0.3.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

dm_aiomodbus-0.3.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file dm_aiomodbus-0.3.1.tar.gz.

File metadata

  • Download URL: dm_aiomodbus-0.3.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dm_aiomodbus-0.3.1.tar.gz
Algorithm Hash digest
SHA256 ebdacac970acf04c27cb71c887f2f77ac7cc72964b18c7c015dabf105b637a1a
MD5 7193defcf4a8fcae48f2a33d74e19f80
BLAKE2b-256 c34c82b1fcc93a23a94bc79cc30ac8c9eb348bcf277c3cbd5d6eeb18383fdcbd

See more details on using hashes here.

File details

Details for the file dm_aiomodbus-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: dm_aiomodbus-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dm_aiomodbus-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3f16adfb9b95b252bd73c5ff5cd6ea66c94bb2f47adc3892d376e0b3c1ef1f39
MD5 4e0ef108513aad350fbde7d44cb1fe93
BLAKE2b-256 1e8e1c823328cee372baf729c6d8b9c05195225a6d5121237ddadb6a1da17e37

See more details on using hashes here.

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