资源下载链接为:
https://pan.quark.cn/s/67c535f75d4c
Python 借助 snap7 库,与 S7-1200 PLC 通讯,实现地址级读写。
安装依赖
pip install python-snap7
建立连接
import snap7
plc = snap7.client.Client()
plc.connect('192.168.0.1', 0, 1) # IP, 机架0, 插槽1
读取地址
value = plc.read_area(snap7.types.Areas.DB, 1, 0, 1)
bool_val = snap7.util.get_bool(value, 0, 0)
buffer = plc.read_area(snap7.types.Areas.DB, 1, 2, 2)
int_val = snap7.util.get_int(buffer, 0)
buffer = plc.read_area(snap7.types.Areas.DB, 1, 4, 4)
real_val = snap7.util.get_real(buffer, 0)
写入地址
buffer = bytearray(1)
snap7.util.set_bool(buffer, 0, 0, True)
plc.write_area(snap7.types.Areas.DB, 1, 0, buffer)
buffer = bytearray(2)
snap7.util.set_int(buffer, 0, 1234)
plc.write_area(snap7.types.Areas.DB, 1, 2, buffer)
buffer = bytearray(4)
snap7.util.set_real(buffer, 0, 3.14)
plc.writ