No project description provided
Project description
ModbusPy
ModbusPy is a Python library for Modbus protocol. Implemented with asyncio and supports TCP, Serial.
This work is licensed under CC BY-NC-ND 4.0. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-nd/4.0/
Installation
pip install modbuspy
Usage
TCP Server
from modbuspy.Server.TCPServer import TCPServer
server = TCPServer(host="127.0.0.1", port=1502)
# Add a slave to the server
slave = server.add_slave(1)
# Add coils to the slave
coils = {i: bool(i % 2) for i in range(10)} # Alternating True and False
slave.add_coils(coils)
# Add discrete inputs to the slave
discrete_inputs = {i: bool((i + 1) % 2) for i in range(10)} # Alternating False and True
slave.add_discrete_inputs(discrete_inputs)
# Add holding registers to the slave
registers = {i: struct.pack("<h", i) for i in range(10)} # 10 holding registers starting from 0
slave.add_holding_registers(registers)
# Add input registers to the slave
input_registers = {i: struct.pack("<h", i) for i in range(10)} # 10 input registers starting from 0
slave.add_input_registers(input_registers)
# Add a variable to the slave
slave.add_variable(12345, 11, 2, signed=False, writable=True) # 12345 is the value, 11 is the starting address, 2 is the number of registers, writable determine if it is a holding register or input register
# Start the server
await server.start()
TCP Client
from modbuspy.Client.TCPClient import TCPClient
# Connect to the server
client = TCPClient(host="127.0.0.1", port=1502)
await client.connect()
# Read coils
coils = await client.read_coils(1, 0, 10)
print(coils)
# Write to coils
await client.write_coils(1, 0, [True, False, True, False, True, False, True, False, True, False])
# Read discrete inputs
inputs = await client.read_discrete_inputs(1, 0, 10)
print(inputs)
# Read holding registers
registers = await client.read_holding_registers(1, 0, 10)
print(registers)
# Write to holding registers
await client.write_holding_registers(1, 0, [struct.pack("<h", i) for i in range(10)])
# Read input registers
input_registers = await client.read_input_registers(1, 0, 10)
print(input_registers)
# Read a variable
variable = await client.read_variable(1, 11, 2)
print(variable)
Serial Server and Client
They follow the same structure as the TCP server and client, but uses Serial as the transport layer.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modbuspy-0.1.5.tar.gz.
File metadata
- Download URL: modbuspy-0.1.5.tar.gz
- Upload date:
- Size: 20.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b52f78bd3322cdfea42c2a59c52d145af3a3b13eb22df96846836b364fae1cf
|
|
| MD5 |
16b568b16e88d76b45535faadc7e41a3
|
|
| BLAKE2b-256 |
ef44654004d1d630ed52349248dee0c51224effcee179b8ad35b7912519a9037
|
File details
Details for the file modbuspy-0.1.5-py3-none-any.whl.
File metadata
- Download URL: modbuspy-0.1.5-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b98a01b79fef674f1dbf3886c1c6e16dab25a11f17cbf611919d3e1fe705979
|
|
| MD5 |
f0de68501c56a1b4da9ea040c745bf28
|
|
| BLAKE2b-256 |
fe11611ccf99d433523c577b5b2b03a4cbbff5da4c23a367f6366a4c834814ee
|