import sys
from snap7 import client
from PySide6.QtWidgets import *
from PySide6.QtUiTools import loadUiType
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
ui_form,widget = loadUiType("./pane.ui")
class Window(widget,ui_form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.byte = bytearray([0b0000_0000])
def on_btn0_click(self,checked): #v0.0
num = 1<<0
if checked:
# self.byte[0] = self.byte[0] | 0b0000_0001
self.byte[0] = self.byte[0]+num
else:
# self.byte[0] = self.byte[0] & 0b1111_1110
self.byte[0] = self.byte[0]-num
write_to_plc(0,self.byte)
def on_btn1_click(self,checked): #v0.1
num = 1<<1
if checked:
# self.byte[0] = self.byte[0] | 0b0000_0010 #0x02
self.byte[0] = self.byte[0] +num
else:
# self.byte[0] = self.byte[0] & 0b1111_1101 #0xfc
self.byte[0] = self.byte[0]-num
write_to_plc(0, self.byte)
def on_btn2_click(self,checked): #v0.2
if checked:
self.byte[0] = self.byte[0] | 0b0000_0100 # 0x04
else:
self.byte[0] = self.byte[0] & 0b1111_1011 # 0xfb
write_to_plc(0, self.byte)
def on_btn3_click(self,checked): #v0.3
if checked:
self.byte[0] = self.byte[0] | 0b0000_1000 # 0x08
else:
self.byte[0] = self.byte[0] & 0b1111_0111 # 0xf7
write_to_plc(0, self.byte)
def on_btn4_click(self,checked): #0.4
if checked:
self.byte[0] = self.byte[0] | 0b0001_0000 # 0x10
else:
self.byte[0] = self.byte[0] & 0b1110_1111 # 0xef
write_to_plc(0, self.byte)
def on_btn5_click(self,checked): #v0.5
if checked:
self.byte[0] = self.byte[0] | 0b0010_0000 # 0x20
else:
self.byte[0] = self.byte[0] & 0b1101_1111 # 0xcf
write_to_plc(0, self.byte)
def on_btn6_click(self,checked): #v0.6
if checked:
self.byte[0] = self.byte[0] | 0b0100_0000 # 0x40
else:
self.byte[0] = self.byte[0] & 0b1011_1111 # 0xbf
write_to_plc(0, self.byte)
def on_btn7_click(self,checked):
if checked:
self.byte[0] = self.byte[0] | 0b1000_0000 # 0x80
else:
self.byte[0] = self.byte[0] & 0b0111_1111 # 0x7f
write_to_plc(0, self.byte)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。