1.先去掉DB211优化块访问的勾
2.打开PLC连接机制,勾上允许来自远程对象的PUT/GET通信访问。
3.DB211数据监视界面
4.Python代码(写入PLC的DB211数据块的数据)
import snap7
from snap7 import util
# 创建通讯客户端实例
plcObj = snap7.client.Client()
# 连接至PLC
plcObj.connect('192.168.8.20', 0, 1)
print("开始发送!")
# 写入bool
bool1 = 0 ##bool1--》第0个字节段的第1个位,就是偏移量为0.0的bool量的值
bool2 = 1
bool3 = 0
bool4 = 1
bool5 = 1
bool6 = 0
bool7 = 0
bool8 = 0
boolData = bytearray(1)
util.set_bool(boolData, 0, 0, bool(bool1))
util.set_bool(boolData, 0, 1, bool(bool2))
util.set_bool(boolData, 0, 2, bool(bool3))
util.set_bool(boolData, 0, 3, bool(bool4))
util.set_bool(boolData, 0, 4, bool(bool5))
util.set_bool(boolData, 0, 5, bool(bool6))
util.set_bool(boolData, 0, 6, bool(bool7))
util.set_bool(boolData, 0, 7, bool(bool8))
plcObj.db_write(211, 0, boolData)
# 写入int
intData = bytearray(2)
util.set_int(intData, 0, 5)
plcObj.db_write(211, 2, intData)
# 写入real
realData = bytearray(4)
util.set_real(realData, 0, 99.9)
plcObj.db_write(211, 4, realData)
# 写入string
# sss = 'qqq'
# print(len(sss))
# stringData = bytearray(len(sss) + 2)
# util.set_string(stringData, 0, sss, 256)
# stringData[0] = 254
# plcObj.db_write(211, 8, stringData)
string = 'db211'
data = int.to_bytes(254, 1, 'big') + int.to_bytes(len(string), 1, 'big') + string.encode(encoding='ascii')
plcObj.write_area(snap7.client.Areas.DB, 211, 8, data)
plcObj.disconnect()
print("发送成功!")
5.运行结果
开始解析数据
{'bool1:': False, 'bool2:': True, 'bool3:': False, 'bool4:': True, 'bool5:': True, 'bool6:': False, 'bool7:': False, 'bool8:': False}
int: 5
real: 99.9000015258789
string: db211
进程已结束,退出代码0
import snap7
import struct
from snap7 import util
# 创建通讯客户端实例
plcObj = snap7.client.Client()
# 连接至PLC
plcObj.connect('192.168.10.230', 0, 1)
# 读取数据
data = plcObj.db_read(10, 0, 776)
# 关闭连接
plcObj.disconnect()
# python解析
selfBool = bool.from_bytes(data[0:1], byteorder='big')
selfInt = int.from_bytes(data[2:4], byteorder='big')
selfReal = struct.unpack('>f', data[4:8])[0]
selfString = data[10:264].decode(encoding="ascii")
selfWString = data[268:].decode(encoding="utf-16be")
print("python自身函数解析:")
print(
f"bool:{selfBool}; int:{selfInt}; real:{selfReal}; string:{selfString}; wstring:{selfWString}"
)
# snap7解析
snap7Bool = util.get_bool(data, 0, 0)
snap7Int = util.get_int(data, 2)
snap7Real = util.get_real(data, 4)
snap7String = util.get_string(data, 8, 256)
snap7WString = util.get_string(data, 264, 508)
print("snap7函数解析:")
print(
f"bool:{snap7Bool}; int:{selfInt}; real:{snap7Real}; string:{snap7String}; wstring:{snap7WString}"
)
import snap7
import struct
# 创建通讯客户端实例
plcObj = snap7.client.Client()
# 连接至PLC
plcObj.connect('192.168.10.230', 0, 1)
# 写入DB10.0 —— bool值
plcObj.write_area(snap7.client.Areas.DB, 10, 0, bool.to_bytes(False, 1, 'big'))
# 写入DB10.2
plcObj.write_area(snap7.client.Areas.DB, 10, 2, int.to_bytes(200, 2, 'big'))
# plcObj.write_area(snap7.client.Areas.DB, 10, 2, struct.pack(">h", 112))
# 写入DB10.4 —— real值
plcObj.write_area(snap7.client.Areas.DB, 10, 4, struct.pack(">f", 10.1))
# 写入DB10.8 —— string值
str = 'hello python'
data = int.to_bytes(254, 1, 'big') + int.to_bytes(len(str), 1, 'big') + str.encode(encoding='ascii')
plcObj.write_area(snap7.client.Areas.DB, 10, 8, data)
# 写入DB10.264 —— wstring值
str = '中国北京市'
data = int.to_bytes(508, 2, 'big') + int.to_bytes(len(str), 2, 'big') + str.encode(encoding='utf-16be')
plcObj.write_area(snap7.client.Areas.DB, 10, 264, data)
# 关闭连接
plcObj.disconnect()
参考文献:https://blog.csdn.net/XUMENGCAS/article/details/122695234