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.
Metadata
Release files for modbuspy 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| modbuspy-0.1.5.tar.gz | 20.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modbuspy-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 46.9 kB
Release files / modbuspy-0.1.5.tar.gz
| Download URL | modbuspy-0.1.5.tar.gz |
|---|---|
| Size | 20.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3b52f78bd3322cdfea42c2a59c52d145af3a3b13eb22df96846836b364fae1cf
|
|
BLAKE2b-256 checksum How to use checksums |
ef44654004d1d630ed52349248dee0c51224effcee179b8ad35b7912519a9037
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
|
Release files / modbuspy-0.1.5-py3-none-any.whl
| Download URL | modbuspy-0.1.5-py3-none-any.whl |
|---|---|
| Size | 26.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3b98a01b79fef674f1dbf3886c1c6e16dab25a11f17cbf611919d3e1fe705979
|
|
BLAKE2b-256 checksum How to use checksums |
fe11611ccf99d433523c577b5b2b03a4cbbff5da4c23a367f6366a4c834814ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
|