pyModbusTCP

raw JSON →
0.3.0 verified Mon Apr 27 auth: no python

A simple Modbus/TCP library for Python. Current version 0.3.0, released irregularly. Provides client and server functionality for Modbus TCP communication.

pip install pyModbusTCP
error ModuleNotFoundError: No module named 'pyModbusTCP'
cause Library not installed or wrong import casing.
fix
pip install pyModbusTCP (capital M, B, T, C, P)
error pyModbusTCP.client.exceptions.ConnectionError: Connection refused
cause Modbus server not running or wrong IP/port.
fix
Verify server is running and reachable: ping 127.0.0.1, check port 502
error ImportError: cannot import name 'ModbusClient' from 'pyModbusTCP'
cause Direct import from top-level package, not from submodule.
fix
Use 'from pyModbusTCP.client import ModbusClient'
breaking v0.2.0 and v0.3.0 have breaking changes. Check CHANGES file before upgrading.
fix Review CHANGES on GitHub: https://github.com/sourceperl/pyModbusTCP/blob/master/CHANGES
gotcha ModbusClient does not auto-close connection; must call close() or use context manager.
fix Use with statement: with ModbusClient() as c: ...
gotcha Default timeout is 1 second; may cause timeout on slow networks.
fix Set timeout parameter: ModbusClient(timeout=5)

Create a Modbus TCP client, connect, and read holding registers.

from pyModbusTCP.client import ModbusClient

try:
    c = ModbusClient(host='127.0.0.1', port=502, auto_open=True)
    if c.read_holding_registers(0, 1):
        print('success')
except Exception as e:
    print(f'Error: {e}')