This is my custom aiomodbus client
Project description
DM-aiomodbus
Urls
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())
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 = await client.read_holding_registers(258, count=2)
reg_256 = await client.read_holding_registers(256)
reg_260_2 = 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):
await client.write_register(256, 1)
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())
Project details
Release history Release notifications | RSS feed
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.5.tar.gz
(5.3 kB
view details)
Built Distribution
File details
Details for the file dm-aiomodbus-0.1.5.tar.gz
.
File metadata
- Download URL: dm-aiomodbus-0.1.5.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98820eda0ea74e311005328588ac70ba5cab465c249ecf9ba9cd27ae71a057c8 |
|
MD5 | 6c2072a6cda13e744741cd8f699d2070 |
|
BLAKE2b-256 | 7d5ef62c7890a8cb44e5a7655a68a8f6cfa7ab49eadd3961807d9f7d60a6c984 |
File details
Details for the file dm_aiomodbus-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: dm_aiomodbus-0.1.5-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42b11b6267cffe005326d06f62ac57eeb27af76e91c6a3ae938b4727ddc7885c |
|
MD5 | e8e0b3efd2c69b96c961e38824e6d708 |
|
BLAKE2b-256 | b01f453e32bc3f3f8105b44930b1c967886e0250330f67f1e14ef2812ef9a129 |