PyModbus Documentation
Complete Python Modbus library for industrial automation. Supports TCP, RTU, ASCII protocols with client/server implementation for PLC, sensor, and device communication.
Modern Python library for Modbus communication. Industrial-grade, async-ready, fully featured.
What is PyModbus? Complete Python implementation of the Modbus protocol stack. Connect to PLCs, sensors, meters, and industrial devices using TCP, RTU, or ASCII protocols.
Why PyModbus?
Complete Protocol
Full Modbus TCP, RTU, and ASCII support
Client & Server
Build both masters and slaves
Async Support
Modern async/await for high performance
Production Ready
Battle-tested in industrial environments
PyModbus handles all protocol complexities so you can focus on your automation logic.
Quick Start
Install PyModbus
pip install pymodbus
conda install -c conda-forge pymodbus
# Ubuntu/Debian
sudo apt install python3-pymodbus
# Fedora
sudo dnf install python3-pymodbus
Connect to Device
from pymodbus.client import ModbusTcpClient
# Connect to Modbus TCP device
client = ModbusTcpClient('192.168.1.100')
client.connect()
# Read 10 holding registers starting at address 0
result = client.read_holding_registers(0, 10, unit=1)
if not result.isError():
print(f"Registers: {result.registers}")
client.close()
Read Data
# Read different register types
holding = client.read_holding_registers(0, 10)
input_regs = client.read_input_registers(0, 10)
coils = client.read_coils(0, 16)
discrete = client.read_discrete_inputs(0, 16)
# Write data
client.write_register(0, 1234)
client.write_registers(0, [10, 20, 30, 40])
client.write_coil(0, True)
Common Use Cases
PLC Communication
Connect to Siemens, Allen-Bradley, Schneider PLCs
Energy Monitoring
Read power meters and energy analyzers
Building Automation
Control HVAC, lighting, access systems
Industrial IoT
Bridge legacy devices to modern systems
Protocol Support
Modbus TCP
- Ethernet-based communication
- Default port 502
- Multiple simultaneous connections
- No checksum needed (TCP handles it)
Modbus RTU
- Serial communication (RS485/RS232)
- Binary protocol with CRC
- Most common in field devices
- Compact and efficient
Modbus ASCII
- Serial communication
- Human-readable ASCII format
- Easier debugging
- Less efficient than RTU
Real-World Example
Reading temperature and pressure from industrial sensor:
from pymodbus.client import ModbusTcpClient
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.constants import Endian
# Connect to Modbus device
client = ModbusTcpClient('192.168.1.50', port=502)
client.connect()
try:
# Read temperature (32-bit float at address 100)
result = client.read_holding_registers(100, 2, unit=1)
if not result.isError():
decoder = BinaryPayloadDecoder.fromRegisters(
result.registers,
byteorder=Endian.Big,
wordorder=Endian.Big
)
temperature = decoder.decode_32bit_float()
print(f"Temperature: {temperature:.2f}°C")
# Read pressure (32-bit float at address 102)
result = client.read_holding_registers(102, 2, unit=1)
if not result.isError():
decoder = BinaryPayloadDecoder.fromRegisters(
result.registers,
byteorder=Endian.Big,
wordorder=Endian.Big
)
pressure = decoder.decode_32bit_float()
print(f"Pressure: {pressure:.2f} bar")
# Alert on high pressure
if pressure > 10:
print("⚠️ High pressure alert!")
except Exception as e:
print(f"Error: {e}")
finally:
client.close()
Error Handling
Robust error handling for industrial applications:
from pymodbus.client import ModbusTcpClient
from pymodbus.exceptions import ModbusException, ConnectionException
client = ModbusTcpClient('192.168.1.100', timeout=3, retries=3)
try:
if not client.connect():
raise ConnectionException("Failed to connect")
result = client.read_holding_registers(0, 10, unit=1)
if result.isError():
print(f"Modbus error: {result}")
else:
print(f"Data: {result.registers}")
except ConnectionException as e:
print(f"Connection failed: {e}")
except ModbusException as e:
print(f"Modbus error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
finally:
client.close()
Next Steps
Installation
Complete setup guide for all platforms
Quick Start
Your first Modbus connection
TCP Client
Connect over Ethernet networks
RTU Client
Serial communication guide
Ready to Connect: PyModbus provides everything you need for reliable industrial communication. Start building your automation solutions today!
How is this guide?