Skip to main content

This is my custom aiomodbus client

Project description

DM-aiomodbus

Urls

Example

Connection

  • Serial

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

    from dm_aiomodbus import DMAioModbusTcpClient
    
    modbus_client = DMAioModbusTcpClient(
        host="192.168.0.0",
        port=501
    )
    
  • Simulator (always returns mock data)

    from dm_aiomodbus import DMAioModbusSimulatorClient
    
    modbus_client = DMAioModbusSimulatorClient()
    

Usage

  • Usual client
from dm_aiomodbus import DMAioModbusTcpClient
import asyncio


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

    # read registers
    reg_258_259, = await modbus_client.read_holding_registers(258, count=2)
    reg_256 = await modbus_client.read_holding_registers(256)
    # read second slave-device
    reg_260_2 = await modbus_client.read_holding_registers(address=260, slave=2)
    print(reg_258_259, reg_256, reg_260_2)

    # write registers
    status_256 = await modbus_client.write_register(256, 1)
    print(status_256)
    # write second slave-device
    await modbus_client.write_register(260, value=0, slave=2)


if __name__ == "__main__":
    asyncio.run(main())
  • Return-errors client

Error messages are returned with the execution result

from dm_aiomodbus import DMAioModbusTcpClient
import asyncio


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

    # read registers
    # get values and error (if present, else "")
    reg_258_259, err1 = await modbus_client.read_holding_registers(258, count=2)
    print(reg_258_259, err1)

    # write registers
    # get write status and error (if present, else "")
    status_256, err2 = await modbus_client.write_register(256, 1)
    print(status_256, err2)


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

Optional init parameters

Parameter Type Default Value Description
return_errors bool False Error messages are returned with the execution result
execute_timeout_s int 5 requests timeout (s)
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)
name_tag str auto name tag for logger suffix

Set custom logger

If you want set up custom logger

from dm_aiomodbus import DMAioModbusTcpClient


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

Uploaded Source

Built Distribution

dm_aiomodbus-0.2.4-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for dm_aiomodbus-0.2.4.tar.gz
Algorithm Hash digest
SHA256 880fd2ecb885c79c56857d2e298369e8d3268f5346d2d28b9f3b9067f4706044
MD5 70a9b28b7bd63630209b8faf464861dc
BLAKE2b-256 4e6be7bf3233ea5413c38323411bb52adf0be9d93d93c119abc6dcfd508f9935

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dm_aiomodbus-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 91e4865cde67f3626e3e8947cf83a147636e81ecd6bd4eb76c2be64e3fa3db58
MD5 a8ce4fe927a1cc5ab1328e2dd40316f1
BLAKE2b-256 2a573a3c87a61f033a84a8565a8b9f0a89c995bcba182896bb29bec3779e9656

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