主要步骤:
1. python 安装snap7 的库,指令【使用清华库】:
pip install python-snap7 -i https://pypi.tuna.tsinghua.edu.cn/simple
2. 在python中使用
2.1 加载需要的库文件
import snap7#use client
from snap7.util import *#use function get_bool or set_int for type converting
from snap7.types import * #including variant type and Area ID
2.2 创建client并建立连接
client=snap7.client.Client()#init client
client.connect(ip,rack=0,slot=1)# create connection, for tplc 1500, use rack 0 and slot 1
#针对不同设备的slot的使用,博图组态里有说明,
2.3 读DB变量
data=client.read_area(snap7.types.Areas.DB,1,4,2)#DB1.DBW4的数值,返回bytearray,由于我们使用DBW有2个字节,所以最后参数是2