Python Library – pymodbusTCP
Python pymodbusTCP
Voor communicatie van
- Modbus TCP/IP
Niet voor
- Modbus RTU
- Modbus ASCII
- Modbus RTU over TCP
Installatie:
1 2 3 |
sudo apt-get update sudo apt-get install -y python-dev python-pip sudo pip install -U pymodbusTCP |
Installatie voor Python 3:
1 2 3 |
sudo apt-get update sudo apt-get install -y python3 python3-dev python-pip sudo pip3 install -U pymodbusTCP |
Mijn voorbeeld:
1 2 3 4 5 6 7 |
#!/usr/bin/env python from pyModbusTCP.client import ModbusClient client = ModbusClient(host="192.168.0.0", port=502, auto_open=True, auto_close=True, timeout=10) data = client.read_holding_registers(0, 1) # FUNCTIE 03 - Lees register 0, 1 lang print "Waarde register: ", data[0] client.close() |
Informatie (ENG):
Summary
A simple Modbus/TCP library for Python
A simple Modbus/TCP client library for Python.
Since version 0.1.0, a server is also available for test purpose only (don’t use in project).
pyModbusTCP is pure Python code without any extension or external module dependency.
Usage example
See examples/ for full scripts.
include (for all samples)
1 |
from pyModbusTCP.client import ModbusClient |
module init (TCP always open)
1 2 |
# TCP auto connect on first modbus request c = ModbusClient(host="localhost", port=502, auto_open=True) |
module init (TCP open/close for each request)
1 2 |
# TCP auto connect on modbus request, close after it c = ModbusClient(host="127.0.0.1", auto_open=True, auto_close=True) |
module init (with accessor functions)
1 2 3 4 5 |
c = ModbusClient() c.host("localhost") c.port(502) # managing TCP sessions with call to c.open()/c.close() c.open() |
Read 2x 16 bits registers at modbus address 0:
1 2 3 4 5 |
regs = c.read_holding_registers(0, 2) if regs: print(regs) else: print("read error") |
Write value 44 and 55 to registers at modbus address 10:
1 2 3 4 |
if c.write_multiple_registers(10, [44,55]): print("write ok") else: print("write error") |
Download pymodbusTCP @ pypi.python.org
[#/python/libraries/pymodbustcp” ]