Skip to main content

This is my custom aiomodbus client

Project description

DM-aiomodbus

Urls

Usage

Connection

  • Serial

    from dm_aiomodbus import DMAioModbusSerialClient
    
    modbus_client = DMAioModbusSerialClient(
        port="/dev/ttyUSB0",
        baudrate=9600,
        bytesize=8,
        stopbits=2,
        parity="N",
        name_tag="my_serial_plc"
    )
    
  • TCP

    from dm_aiomodbus import DMAioModbusTcpClient
    
    modbus_client = DMAioModbusTcpClient(
        host="192.168.0.0",
        port=501,
        name_tag="my_tcp_plc"
    )
    

Requests

from dm_aiomodbus import DMAioModbusTcpClient, DMAioModbusTempClientInterface
import asyncio


async def main():
    # create client
    modbus_client = DMAioModbusTcpClient(
        host="192.168.0.0",
        port=501,
        name_tag="my_tcp_plc"
    )

    # create read callback
    async def read_callback(client: DMAioModbusTempClientInterface):
        reg_258_259, err1 = await client.read_holding_registers(258, count=2)  # get values and error if any
        reg_256, err2 = await client.read_holding_registers(256)
        reg_260_2, err3 = await client.read_holding_registers(address=260, slave=2)  # read second slave-device
        print(reg_258_259, reg_256, reg_260_2)

    # create read callback
    async def write_callback(client: DMAioModbusTempClientInterface):
        status, err = await client.write_register(256, 1)  # get write status and error if any
        await client.write_register(260, value=0, slave=2)  # write second slave-device

    # request to plc
    modbus_client.execute(read_callback)  # execute without waiting result
    # or
    await modbus_client.execute_and_return(write_callback, timeout=3)  # execute and wait result with timeout 3s (default 5)


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

Optional init parameters

Parameter Type Default Value Description
disconnect_timeout_s int 20 timeout waiting for an active connection after the last request (s)
after_execute_timeout_ms int 3 timeout between requests (ms)

Set custom logger

If you want set up custom logger

from dm_aiomodbus import DMAioModbusTcpClient  # or another client


# create custom logger
class MyLogger:
    def debug(self, message):
        pass

    def info(self, message):
        pass

    def warning(self, message):
        print(message)

    def error(self, message):
        print(message)


# set up custom logger for all clients
DMAioModbusTcpClient.set_logger(MyLogger())

Run in Windows

If you run async code in Windows, set correct selector

import asyncio
import sys

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

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.1.6.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

dm_aiomodbus-0.1.6-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dm_aiomodbus-0.1.6.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for dm_aiomodbus-0.1.6.tar.gz
Algorithm Hash digest
SHA256 d13df8593b18aff00abacce3eac690f7741d8a07a401c9cec1c40a33fcca0993
MD5 c0f1208980903c1dc230e9744cdba7b8
BLAKE2b-256 9ebd284bbcbe1641dd24601ebf440fe9ac88d18b6828002fddc3bcd71cdb57c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dm_aiomodbus-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1eea649f677e03d86f6c749c25c96a72cf8f846d45fca39d5aef9d4645d581c2
MD5 5e5a2a8db589bee9fdd6d76c595210cb
BLAKE2b-256 50e780882627c8dbb2c2a2f7f69cfe9b64b0e79c6867ce1c8d47e9ea2d597883

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