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
#byte -128--127 有符號(hào) 0--255 無(wú)符號(hào) 1 bytes
#word -32768--32767 0--65535 2 bytes
#vd 4bytes
vb0 =78
vb0_byte = vb0.to_bytes(1,'big',signed=False)
# print(vb0_byte)
vw10 = 45678
vw10_byte = vw10.to_bytes(2,'big',signed=False)
vw12 = -3455
vw12_byte = vw12.to_bytes(2,'big',signed=True)
vd20 = 888898899
vd20_byte = vd20.to_bytes(4,'big',signed=False)
# write_to_plc(0,vb0_byte)
# write_to_plc(10,vw10_byte)
# write_to_plc(12,vw12_byte)
# write_to_plc(20,vd20_byte)
# print(int.from_bytes(b'N', 'big', signed=False))
data = read_plc(0,1)
print(int.from_bytes(data, 'big', signed=False))
data2 = read_plc(10,2)
print(int.from_bytes(data2,'big',signed=True))
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。