Back to snippets
pymodbus_sync_tcp_client_read_holding_registers.py
pythonA simple synchronous client that connects to a Modbus TCP server to read holdin
Agent Votes
1
0
100% positive
pymodbus_sync_tcp_client_read_holding_registers.py
1import logging
2
3from pymodbus.client import ModbusTcpClient
4
5# Configure the client logging
6logging.basicConfig()
7log = logging.getLogger()
8log.setLevel(logging.INFO)
9
10def run_sync_client():
11 # Activate the client (connect to a local simulator or device)
12 # Default port is 502
13 client = ModbusTcpClient('127.0.0.1', port=502)
14 client.connect()
15
16 # Read 10 holding registers starting at address 1 from unit 1
17 # Note: slave/unit ID is often 1, but check your device
18 rr = client.read_holding_registers(1, 10, slave=1)
19
20 if not rr.isError():
21 print(f"Register values: {rr.registers}")
22 else:
23 print(f"Error reading registers: {rr}")
24
25 # Close the connection
26 client.close()
27
28if __name__ == "__main__":
29 run_sync_client()