Python Library – pymodbus
Python pymodbus
Voor communicatie van
- Modbus RTU
- Modbus ASCII
- Modbus RTU over TCP
Niet voor
- Modbus TCP/IP
Installatie:
1 2 3 |
sudo apt-get update sudo apt-get install -y python-dev python-pip sudo pip install -U pymodbus |
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 pymodbus |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Collecting pymodbus3 Downloading https://www.piwheels.hostedpi.com/simple/pymodbus3/pymodbus3-1.0.0-py3-none-any.whl (76kB) 100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 81kB 1.5MB/s Collecting pyserial>=2.6 (from pymodbus3) Using cached pyserial-3.4-py2.py3-none-any.whl Collecting twisted>=12.2.0 (from pymodbus3) Downloading https://www.piwheels.hostedpi.com/simple/twisted/Twisted-17.9.0-cp35-cp35m-linux_armv7l.whl (3.0MB) 100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 3.0MB 98kB/s Collecting incremental>=16.10.1 (from twisted>=12.2.0->pymodbus3) Downloading incremental-17.5.0-py2.py3-none-any.whl Collecting constantly>=15.1 (from twisted>=12.2.0->pymodbus3) Downloading constantly-15.1.0-py2.py3-none-any.whl Collecting hyperlink>=17.1.1 (from twisted>=12.2.0->pymodbus3) Downloading hyperlink-17.3.1-py2.py3-none-any.whl (73kB) 100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 81kB 2.0MB/s Collecting zope.interface>=4.0.2 (from twisted>=12.2.0->pymodbus3) Downloading https://www.piwheels.hostedpi.com/simple/zope-interface/zope.interface-4.4.3-cp35-cp35m-linux_armv7l.whl (161kB) 100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 163kB 1.4MB/s Collecting Automat>=0.3.0 (from twisted>=12.2.0->pymodbus3) Downloading Automat-0.6.0-py2.py3-none-any.whl Collecting setuptools (from zope.interface>=4.0.2->twisted>=12.2.0->pymodbus3) Downloading setuptools-38.4.0-py2.py3-none-any.whl (489kB) 100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 491kB 549kB/s Collecting six (from Automat>=0.3.0->twisted>=12.2.0->pymodbus3) Downloading six-1.11.0-py2.py3-none-any.whl Collecting attrs (from Automat>=0.3.0->twisted>=12.2.0->pymodbus3) Downloading attrs-17.4.0-py2.py3-none-any.whl Installing collected packages: pyserial, incremental, constantly, hyperlink, setuptools, zope.interface, six, attrs, Automat, twisted, pymodbus3 Found existing installation: pyserial 3.2.1 Not uninstalling pyserial at /usr/lib/python3/dist-packages, outside environment /usr Found existing installation: setuptools 33.1.1 Not uninstalling setuptools at /usr/lib/python3/dist-packages, outside environment /usr Found existing installation: six 1.10.0 Not uninstalling six at /usr/lib/python3/dist-packages, outside environment /usr Successfully installed Automat-0.6.0 attrs-17.4.0 constantly-15.1.0 hyperlink-17.3.1 incremental-17.5.0 pymodbus3-1.0.0 pyserial-3.4 setuptools-38.4.0 six-1.11.0 twisted-17.9.0 zope.interface-4.4.3 |
Informatie (ENG):
Summary
Pymodbus is a full Modbus protocol implementation using twisted for its asynchronous communications core. It can also be used without any third party dependencies (aside from pyserial) if a more lightweight project is needed. Furthermore, it should work fine under any python version > 2.3 with a python 3.0 branch currently being maintained as well.
Features
Client Features
- Full read/write protocol on discrete and register
- Most of the extended protocol (diagnostic/file/pipe/setting/information)
- TCP, UDP, Serial ASCII, Serial RTU, and Serial Binary
- asynchronous(powered by twisted) and synchronous versions
- Payload builder/decoder utilities
Server Features
- Can function as a fully implemented modbus server
- TCP, UDP, Serial ASCII, Serial RTU, and Serial Binary
- asynchronous(powered by twisted) and synchronous versions
- Full server control context (device information, counters, etc)
- A number of backing contexts (database, redis, a slave device)
Use Cases
Although most system administrators will find little need for a Modbus server on any modern hardware, they may find the need to query devices on their network for status (PDU, PDR, UPS, etc). Since the library is written in python, it allows for easy scripting and/or integration into their existing solutions.
Continuing, most monitoring software needs to be stress tested against hundreds or even thousands of devices (why this was originally written), but getting access to that many is unwieldy at best. The pymodbus server will allow a user to test as many devices as their base operating system will allow (allow in this case means how many Virtual IP addresses are allowed).
Example Code
For those of you that just want to get started fast, here you go:
1 2 3 4 5 6 7 |
from pymodbus.client.sync import ModbusTcpClient client = ModbusTcpClient('127.0.0.1') client.write_coil(1, True) result = client.read_coils(1,1) print result.bits[0] client.close() |
For more advanced examples, check out the examples included in the respository. If you have created any utilities that meet a specific need, feel free to submit them so others can benefit.
[#/python/libraries/pymodbus” ]