snap7读写PLC变量

1. 读DB

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'''
#copy snap7.dll & snap7.lib files to python root path first
pip install snap7
'''
import snap7
import struct
from snap7 import util


# 创建通讯客户端实例
plc = snap7.client.Client()

# 连接至PLC
plc.connect('192.168.0.1', 0, 1)

# 读取数据
data = plc.db_read(17,0,878)

# 关闭连接
plc.disconnect()

d1=data[0:256]
print(str(d1))

d2=data[256:768]
print(d2.decode('utf-16be'))

selfint=int.from_bytes(data[852:854],byteorder='big')
print(selfint)

# python解析
selfInt = int.from_bytes(data[852:854], byteorder='big')
selfReal = struct.unpack('>f', data[874:878])[0]
selfString = data[0:254].decode(encoding="ascii")
selfWString = data[256:].decode(encoding="utf-16be")
print("python自身函数解析:")
print(
f"bool:{selfBool}; int:{selfInt}; real:{selfReal}; string:{selfString}; wstring:{selfWString}"
)

# snap7解析
snap7Int = util.get_int(data, 852)
snap7Real = util.get_real(data, 874)
snap7String = util.get_string(data,0, 254)
snap7WString = util.get_string(data,256,766)
print("snap7函数解析:")
print(
f"bool:{snap7Bool}; int:{selfInt}; real:{snap7Real}; string:{snap7String}; wstring:{snap7WString}"
)

2. 写DB

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'''
#copy snap7.dll & snap7.lib files to python root path first
pip install snap7
'''
import snap7
from snap7 import util

# 创建通讯客户端实例
plc = snap7.client.Client()

# 连接至PLC
plc.connect('192.168.0.1', 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))
plc.db_write(211, 0, boolData)

# 写入int
intData = bytearray(2)
util.set_int(intData, 0, 5)
plc.db_write(211, 2, intData)

# 写入real
realData = bytearray(4)
util.set_real(realData, 0, 99.9)
plc.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
# plc.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')
#utf-8可以写中文,但是解码错误
#unicode不能写
#strBYTE长度只有254,剩下两个长度是头文件
plc.write_area(snap7.client.Areas.DB, 211, 8, data)

plc.disconnect()

print("发送成功!")

#PLC中,wstring---unicode格式/string----ascii格式

3. 写中文

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
36
37
import snap7
import struct

# 创建通讯客户端实例
plc = snap7.client.Client()

# 连接至PLC
plc.connect('192.168.0.1', 0, 1)


# 写入DB17.DBX878.0 —— bool值
plc.write_area(snap7.client.Areas.DB,17,878,bool.to_bytes(True,1,'big'))

# 写入DB17.DBW852
plc.write_area(snap7.client.Areas.DB,17,852,int.to_bytes(2001,2,'big'))
# plc.write_area(snap7.client.Areas.DB, 10, 2, struct.pack(">h", 112))

# 写入DB17.DBD874 —— real值
plc.write_area(snap7.client.Areas.DB,17,874, struct.pack(">f", 10.1))

# 写入P#DB17.DBX0.0 BYTE 256 —— string值
str = 'hello python'
#字符串第一个字节存的是最大字节数,第二个字节存的是当前占有的字节数,后面才是使用的字节数
data = int.to_bytes(254, 1, 'big') + int.to_bytes(len(str), 1, 'big') + str.encode(encoding='ascii')
plc.write_area(snap7.client.Areas.DB,17,0,data)

# 写入P#DB17.DBX256.0 BYTE 512 —— wstring值
str = '不吃火锅 就吃烤匠'
data = int.to_bytes(508,2,'big') + int.to_bytes(len(str), 2, 'big') + str.encode(encoding='utf-16be')
plc.write_area(snap7.client.Areas.DB,17,256,data)

# 关闭连接
plcObj.disconnect()

#Big,大小端模式中的大端模式
#python中不存在字符类型,字符也当作字符串处理


snap7读写PLC变量
http://example.com/2024/07/21/snap7读写PLC变量/
作者
xiao cuncun
发布于
2024年7月21日
许可协议