b = bytes([230, 136, 145, 232, 166, 129, 232, 135, 170, 229, 173, 166, 231, 189, 145])
# print(b)
# #訪問(wèn)
# print(b[0])
# #
# # #解析
# print(b.decode('utf8'))
# print(int.from_bytes(b[0:2]))
# #
# #修改
# b[0] = 20
#
# data = bytearray(b)
#
# print(data)
#
# data[0] = 20
# print(list(data))
mydata = bytearray(12)
mydata[0:1] = int.to_bytes(121,1) #VB0
mydata[1:2] = int.to_bytes(-66,1,signed=True) #VB1
mydata[2:4] = int.to_bytes(7878,2) #vw2
mydata[4:8] = int.to_bytes(7677876,4)
import struct
mydata[8:12] = struct.pack(">f",89897.67)
if __name__ == '__main__':
from snap7 import client
def connect_plc() -> client.Client:
plc = client.Client()
plc.set_connection_type(3)
plc.connect("192.168.5.20", 0, 1)
return plc
def write_to_plc(start_addr, byte: bytes):
plc = connect_plc()
plc.db_write(1, start_addr, byte)
plc.disconnect()
def read_plc(start_addr, num) -> bytearray:
plc = connect_plc()
data = plc.db_read(1, start_addr, num)
plc.disconnect()
return data
write_to_plc(0,mydata)
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。