
本文共 3683 字,大约阅读时间需要 12 分钟。
本文列出了基于python开发的S7/Modbus/OPCUA/MQTT库:
Modbus: Modbus_tk, Pymodbus, Minimalmodbus, Umodbus
MQTT: paho
S7: python-snap7
OpcUa: python-opcua
介绍了安装,使用手册和例子。
具体的用法需要结合使用手册深入学习。
1 modbus
1.1主流modbus python库
Modbus_tk
Pymodbus
Minimalmodbus
Umodbus
1.2比较
库名称 |
installation |
Code |
3rd party依赖 |
主要功能 |
modbus_tk |
pip install modbus_tk |
pyserial |
Support modbus TCP and RTU, both master and slave Don’t support modbus ASCII 重量级 |
|
pymodbus |
pip install pymodbus |
pyserial |
Support modbus TCP/RTU/ASCII, master and slave 重量级 |
|
minimalmodbus |
pip install minimalmodbus |
https://github.com/pyhys/minimalmodbus |
pyserial |
Only support Modbus RTU and ASCII, and only work in master(client). 非常轻量级 |
uModbus |
pip install uModbus |
https://github.com/AdvancedClimateSystems/uModbus/ |
no |
Support both Modbus client and server (both TCP and RTU). Don’t support Modbus ASCII 轻量级 |
1.3例子:
选择哪一个modbus python库取决于具体的需求。我这里选择minimalmodbus,应为我的应用场景中至少需要modbus client端,并且server端是modbus ascii协议,这样选择轻量级的minimalmodbus就可以了。
下面是gitbub中给出的minimalmodbus的例子:
对于其他的库,在相应的github源码库中都有例子可以参考。
2 MQTT
The Paho Python Client provides a client class with support for both MQTT v3.1 and v3.1.1 on Python 2.7 or 3.x.
官方网站:
github源代码:
2.1安装
可以直接通过pip安装二进制包:
pip install paho-mqtt
或者下载最新的源码安装:
git clone https://github.com/eclipse/paho.mqtt.python.gitcd paho.mqtt.pythonpython setup.py install
2.2使用手册
2.3例子:
Paho的源代码中提供了大量例子程序:
这里给出一个设置用户名和密码,然后订阅话题的例子:
#!/usr/bin/python# -*- coding: utf-8 -*-import paho.mqtt.client as mqttdef on_connect(mqttc, obj, flags, rc):
print("connect rc: " + str(rc))def on_message(mqttc, obj, msg):
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))def on_publish(mqttc, obj, mid):
print("mid: " + str(mid))def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))def on_log(mqttc, obj, level, string):
print(string)# If you want to use a specific client id, use# mqttc = mqtt.Client("client-id")# but note that the client id must be unique on the broker. Leaving the client# id parameter empty will generate a random id for you.mqttc = mqtt.Client()mqttc.on_message = on_messagemqttc.on_connect = on_connectmqttc.on_publish = on_publishmqttc.on_subscribe = on_subscribemqttc.username_pw_set("usr123", "pw123")mqttc.connect("localhost", 1883, 60)mqttc.subscribe("/test/data", 0)mqttc.loop_forever()
3 S7
Snap7是S7的一个开源库,支持C/C++,Pascal,Phyton,Node.js等多种语言,官网为:
源码可以从下载。
在下载的源码中就包含相应的使用手册和例子。
python-snap7是对snap7库的python封装,网址为:
github源代码:
3.1 python-snap7安装
安装可以参考
3.1.1安装Snap7库
python-snap7依赖snap7库,所以需要首先安装snap7库。
- Linux Unbuntu下可以运行如下命令:
$ sudo add-apt-repository ppa:gijzelaar/snap7
$ sudo apt-get update
$ sudo apt-get install libsnap71 libsnap7-dev
2. Windows下的话,从 下载源码包,里面也包含了编译后的二进制库文件,直接拷贝相应的snap7.dll* 到一个目录下,然后修改PATH环境变量即可。以win64为例:
3. 也可以下载源码后,进行源码编译和安装。
3.1.2 安装python-snap7
直接运行
$ pip install python-snap7
即可。
可以从github下载源代码
在代码目录运行:
$ python ./setup.py install
3.2 使用手册
下面是完整手册
3.3例子:
Github源代码中包含了很好的例子:
4 OPCUA
FreeOpcUa 是一个开源项目,提供了整套的OPC-UA Server 和 Client库和工具包。它提供了C++和Python两种实现版本。 Web 网址为:
》C++ 库github地址:https://github.com/FreeOpcUa/freeopcua
》Python OPC-UA库, 纯python开发:
https://github.com/FreeOpcUa/python-opcua (安装使用pip install opcua)
支持python2.7和python3. 现在已经处于维护阶段,只会fix bug,不会开发新功能。
现在转向opcua-asyncio的开发。
》 opcua-asyncio:
python-opcua的未来版本,只支持python3.6及以上版本,异步模式,同时也在提供同步模式封装,最终目标是替换python-opcua。
》GUI客户端:
https://github.com/FreeOpcUa/opcua-client-gui (安装:pip install freeopcua-client)
》命令行工具: https://github.com/FreeOpcUa/python-opcua/tree/master/tools
》GUI工具用了创建OPC UA nodes并保存为xml:
https://github.com/FreeOpcUa/opcua-modeler
这里先主要使用python-opcua,毕竟opcua-asyncio对python版本的限制太大,也还在开发中。
4.1安装
安装很简单,直接运行:
pip install opcua
4.2文档
4.3例子
提供了很多例子
转载地址:https://blog.csdn.net/lclfans1983/article/details/106392538 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关于作者
