Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modbuspy-0.1.4.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

modbuspy-0.1.4-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file modbuspy-0.1.4.tar.gz.

File metadata

  • Download URL: modbuspy-0.1.4.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Windows/11

File hashes

Hashes for modbuspy-0.1.4.tar.gz
Algorithm Hash digest
SHA256 b3a6d65b923e13f80bf4d6ed056821b95768405f80d576290e787195e2488d41
MD5 b9ea96595fcd386592424c4beaabc696
BLAKE2b-256 afb7fc124f031583ef8ab5a4dc31d795a62eba272d1eca6343b94a5d5202c28b

See more details on using hashes here.

File details

Details for the file modbuspy-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: modbuspy-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Windows/11

File hashes

Hashes for modbuspy-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e806039f1596689cbbc53919bcb98a28e1bfbc0be25f79e90a2690abe7a866e2
MD5 43b8be26b87e87a0825c47c0b17c41fb
BLAKE2b-256 e04726196dbf6c4fbe8b8d6230e8780174d4e70d8d1415c8950009e3af19ce6e

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