from dataclasses import dataclass
from snap7 import util
from PySide6.QtCore import QObject,QTimer
@dataclass
class Input:
isMainRun: bool = None
isStarRun: bool = None
isTranRun: bool = None
fault: bool = None
interval: int = None
bytes:bytearray = None
def __post_init__(self):
self.isMainRun = bool(self.bytes[self.isMainRun//8]>>(self.isMainRun%8)&1)
self.isStarRun = bool(self.bytes[self.isStarRun//8]>>(self.isStarRun%8)&1)
self.isTranRun = bool(self.bytes[self.isTranRun//8]>>(self.isTranRun%8)&1)
self.fault = bool(self.bytes[self.fault//8]>>(self.fault%8)&1)
class OutPut:
def __init__(self,mainout: int = None,starout: int = None,tranout: int = None,bytes: bytearray = None):
self.__mainOut:bool = mainout
self.__starOut:bool = starout
self.__tranOut:bool = tranout
self.__bytes:bytearray = bytes
self.mainstate = util.get_bool(bytes, mainout // 8, mainout % 8)
self.starstate = util.get_bool(bytes, starout // 8, starout % 8)
self.transtate = util.get_bool(bytes, tranout // 8, tranout % 8)
def start_main(self):util.set_bool(self.__bytes, self.__mainOut // 8, self.__mainOut % 8, True)
def stop_main(self):util.set_bool(self.__bytes, self.__mainOut // 8, self.__mainOut % 8, False)
def start_star(self):util.set_bool(self.__bytes, self.__starOut // 8, self.__starOut % 8, True)
def stop_star(self):util.set_bool(self.__bytes, self.__starOut // 8, self.__starOut % 8, False)
def start_tran(self):util.set_bool(self.__bytes, self.__tranOut // 8, self.__tranOut % 8, True)
def stop_tran(self):util.set_bool(self.__bytes, self.__tranOut // 8, self.__tranOut % 8, False)
def __str__(self):
return f"mainstate:{self.mainstate}; starstate:{self.starstate}; transtate:{self.transtate}"
#星三角启动模块
class StarDelaStart(QObject):
def __init__(self,intput:Input=None,output:OutPut=None):
super().__init__()
self.input: Input = intput
self.output: OutPut = output
self.timer = QTimer(self)
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.on_timer)
def on_timer(self):
self.output.stop_star()
self.output.start_tran()
print("三角已经启动")
def start(self,state):
if state:
if not all([self.input.isTranRun,self.input.isStarRun,self.input.isMainRun]):
print("星启动")
self.output.start_star()
self.output.start_main()
print("sta",self.output.mainstate,self.output.starstate)
if self.output.mainstate and self.output.starstate:
if not self.timer.isActive():
self.timer.start(self.input.interval)
#故障报警
if self.input.fault:
if self.timer.isActive():
self.timer.stop()
self.output.stop_main()
self.output.stop_tran()
self.output.stop_star()
#停止
if not state:
self.output.stop_main()
self.output.stop_tran()
self.output.stop_star()
d = StarDelaStart()
d.input = Input(0,1,2,3,4000,bytearray(2))
d.output = OutPut(5,6,7,bytearray(2))
while True:
d.start(True)