snap7讀寫PLC變數

不愿透露姓名的小村村發表於2024-05-08

1. 讀DB

'''
#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

'''
#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. 寫中文

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中不存在字元型別,字元也當作字串處理

相關文章