Modbus protocol is an industrial standard widely used for communication between electronic devices, especially in industrial automation systems. Due to its simplicity and flexibility, Modbus has become one of the preferred protocols for control and monitoring equipment in various industries.
This article will show you how to communicate with devices that support the Modbus protocol through Python.
Preparation: First, make sure you have Python installed on your system and have basic Python programming knowledge and understanding of the serial communication library.
Libraries for Modbus in Python: To get started with Modbus in Python, we need several auxiliary libraries. Two of the main Python Modbus implementation libraries are "pymodbus" and "minimalmodbus". The "pymodbus" library provides rich functionality and supports multiple Modbus variants, while "minimalmodbus" is a more lightweight, concise library that focuses on basic functionality.
Install the library: Install "pymodbus" by the following command:
pip install pymodbus
To install "minimalmodbus", you can use the following command:
pip install minimalmodbus
Establishing a Modbus connection: After installing the library, you can now establish a Modbus connection. First, determine the type of Modbus connection you need: Modbus TCP or Modbus RTU
Modbus TCP: For devices connected via Ethernet, we use TCP connections. In Python, you can create a Modbus TCP client through the "pymodbus" library, as shown below:
from pymodbus.client.sync import ModbusTcpClient
# Create a Modbus TCP client
client = ModbusTcpClient('192.168.0.1') # Replace with your device's IP address
# Connect to the Modbus TCP server
client.connect()
# Perform Modbus operations here
# Close the connection
client.close()
Modbus RTU: For devices connected via serial communication (such as RS485), we use RTU connection. In Python, you can use the "minimalmodbus" library for Modbus RTU communication, as shown below:
import minimalmodbus
# Create a Modbus RTU instrument
instrument = minimalmodbus.Instrument('/dev/ttyUSB0', slaveaddress=1) # Replace with your device's serial port and slave address
# Set the communication parameters (baudrate, parity, etc.)
instrument.serial.baudrate = 9600
instrument.serial.parity = minimalmodbus.serial.PARITY_NONE
# Perform Modbus operations here
# Close the connection
instrument.serial.close()
Performing Modbus Operations: Once the Modbus connection is established, we can perform various Modbus operations such as reading and writing register data.
Use "pymodbus" to read the holding register value (for example, register 100), the code is as follows:
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.payload import BinaryPayloadBuilder
# Read a holding register value
result = client.read_holding_registers(address=100, count=1, unit=1)
if result.isError():
print("Error reading register!")
else:
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, byteorder=Endian.Big)
value = decoder.decode_32bit_float()
print("Register value:", value)
Use "minimalmodbus" to write a value to a holding register (for example, register 200) with the following code:
# Write a value to a holding register instrument.write_register(200, 42)
In this tutorial, we explored how to cleverly combine Python and the Modbus protocol to achieve communication with various devices that support the protocol. It not only covers the installation steps of the necessary libraries, but also deeply practices establishing two types of connections, Modbus TCP and Modbus RTU, and demonstrates how to perform basic read and write operations.
With Python's wide compatibility with the Modbus protocol, you can make full use of this powerful tool and apply it to various industrial automation scenarios to improve project efficiency and reliability.