class Motor:
def __init__(self,rbytes:bytearray,wbytes:bytearray,isRun:int = None,fault:int=None,out:int=None):
'''
:param rbytes: iniput字节流 I区 V区 M区 DB区
:param wbytes: output字节流 Q区 V区 M区 DB区
:param isRun: pLC运行信号地址 I区
:param fault: PLC故障信号地址 I区
:param out: PLC输出地址 Q区
'''
self.__rbytes: bytearray = rbytes
self.__wbytes: bytearray = wbytes
self.__runAddr:int = isRun
self.__faultAddr:int = fault
self.__out:int = out
@property
def isRun(self)->bool:return self.__get_bool(self.__rbytes,self.__runAddr//8,self.__runAddr%8)
'''是否运行'''
@property
def fault(self)->bool:return self.__get_bool(self.__rbytes,self.__faultAddr//8,self.__faultAddr%8)
'''是否故障'''
def conditions(self,*args)->bool: return all([i for i in args])
'''条件'''
def start(self):self.__set_bool(self.__wbytes,self.__out//8,self.__out%8,True)
'''启动'''
def stop(self):self.__set_bool(self.__wbytes,self.__out//8,self.__out%8,False)
'''停止'''
@staticmethod
def __get_bool(bytearray_: bytearray, byte_index: int, bool_index: int) -> bool:
index_value = 1 << bool_index
byte_value = bytearray_[byte_index]
current_value = byte_value & index_value
return current_value == index_value
@staticmethod
def __set_bool(bytearray_: bytearray, byte_index: int, bool_index: int, value: bool):
if value not in {0, 1, True, False}:
raise TypeError(f"Value value:{value} is not a boolean expression.")
current_value = Motor.__get_bool(bytearray_, byte_index, bool_index)
index_value = 1 << bool_index
# check if bool already has correct value
if current_value == value:
return
if value:
bytearray_[byte_index] += index_value
else:
bytearray_[byte_index] -= index_value
写个电机类,便于对字节流操作
于 2024-04-12 15:06:40 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)