def OnInit(self): """.""" self.frame1 = MyFrame(None, 'Checksum (CRC32, JSON)') self.frame1.CenterOnScreen() self.frame1.Show() return True
def pushSensorFrame(self) -> object: """ ### 生成面板更新传感器数据的帧 #### Returns: - frame: 更新面板上数据的帧 """ # 传感器未被加入时抛出异常 if self._sensor is None: raise RuntimeError("Sensor hasn't been added.") # 从传感器中获取数据 dataRaw = self._sensor.getData() head = 'fe' cmd = '24 5f' # 数据域结构: 地址, 功能码0110写多个寄存器, 起始地址, 寄存器数量, 字节数, 寄存器值, 校验码 data = super().getDevAddr() + '01 10' + '00 05' + '00 06' + '0c' # 定义一个温度计算的函数 t = lambda x: int(10 * x) + 400 data += super().getWordStr(t(dataRaw['温度'])) data += super().getWordStr(round(dataRaw['湿度'])) data += super().getWordStr(round(dataRaw['PM2.5'])) data += super().getWordStr(round(dataRaw['CO2'])) data += super().getWordStr(round(dataRaw['甲醛'])) data += super().getWordStr(round(dataRaw['VOC'])) # 任意输入校验码 data += '00 00' return MyFrame(head, cmd, data)
def _generateFrame(self, cdata) -> object: """ ### 生成数据帧的低层级接口, 从功能码到异或和之前的数据域自定义, 不改变对象内部属性 #### Parameters: - cdata: 数据域从功能码开始的内容 """ head = 'fe' cmd = '24 5f' data = self.getDevAddr() + ' ' + cdata return MyFrame(head, cmd, data)
def getValsFrame(self): """ ### 生成获取所有参数的命令帧 #### Returns: - __lastCallContents: 最新发送的命令 """ head = 'fe' cmd = '24 5f' data = super().getDevAddr() + ' 01 03 00 00 00 06' self.__lastCallContents = MyFrame(head, cmd, data) return self.__lastCallContents
def generateCloseFrame(self) -> MyFrame: """ ### 生成窗帘关闭的命令帧 #### Returns: - _lastWrite: 最新发送的修改命令 """ head = 'fe' cmd = '24 5f' data = super().getDevAddr() + ' 01 06 11 03 00 01 00 00' self._lastWrite = MyFrame(head, cmd, data) return self._lastWrite
def generateCallFrame(self) -> str: """ ### 生成获取灯具状态的命令帧 #### Returns: _lastCall: 最新发送的读取命令 """ head = 'fe' cmd = '24 5f' data = super().getDevAddr() + ' 01 03 00 00 00 03' self._lastCall = MyFrame(head, cmd, data) return self._lastCall
def generateCallFrame(self) -> object: """ ### 生成获取所有参数的命令帧 #### Returns: - _lastCall: 最新发送的命令 """ head = 'fe' cmd = '24 5f' data = super().getDevAddr() + ' 01 03 00 00 00 06' self._lastCall = MyFrame(head, cmd, data) return self._lastCall
def getValFrame(self, valType): """ ### 生成获取某一个参数的命令帧 #### Parameters: - valType: 获取的数据种类, int #### Returns: - __lastCallContents: 最新发送的命令 """ if valType not in range(6): raise ValueError('Un') head = 'fe' cmd = '24 5f' data = super().getDevAddr() + ' 01 03 00 0' + hex(valType).lstrip( '0x') + ' 00 01' self.__lastCallContents = MyFrame(head, cmd, data) return self.__lastCallContents
def generateValFrame(self, valType: int) -> object: """ ### 生成获取某一个参数的命令帧 #### Parameters: - valType: 获取的数据种类, int: 0-5 #### Returns: - _lastCall: 最新发送的命令 """ if valType not in range(6): raise ValueError('Input out of range. Expected in [0, 5]') head = 'fe' cmd = '24 5f' data = super().getDevAddr() + ' 01 03 00 ' + super().getWordStr( valType) + ' 00 01' self._lastCall = MyFrame(head, cmd, data) return self._lastCall
def generateWriteFrame(self, degree, ddt, drt) -> MyFrame: """ ### 生成控制窗帘开度的命令帧 #### Parameters: - degree: 开度 - ddt: 延迟执行时间 - drt: 延迟回复时间 #### Returns: - _lastWrite: 最新发送的修改命令 """ head = 'fe' cmd = '24 5f' data = super().getDevAddr() + ' 01 10 00 07 00 03 06' for item in [ddt, drt, degree]: data += ' ' + super().getWordStr(item) self._lastWrite = MyFrame(head, cmd, data) return self._lastWrite
def generateWriteFrame(self, switch=None, luminance=None, ct=None, vt=0, ddt=0, drt=0) -> MyFrame: """ 注: 旧版接口不推荐使用. 21.06.15 by Li Mingkai. ### 生成修改灯具状态的命令帧 注: 预期在值为None时进行保持, 还未实现, 目前需填入所有参数 #### Parameters: - switch: 开关 - luminance: 亮度 - ct: 色温 - vt: 变化时间 - ddt: 延迟执行时间 - drt: 延迟回复时间 #### Returns: - _lastWrite: 最新发送的修改命令 """ args = [switch, luminance, ct, vt, ddt, drt] if self._lastWrite is not None: data = self._lastWrite.Data.hex(' ') data_new = data[:11] for i in range(6): if args[i] is not None: # 非none表示需要修改的量 data_new += ' ' + super().getWordStr(args[i]) else: # none则使用上次调用的量 cursor = len(data_new) data_new += data[cursor:cursor + 6] else: head = 'fe' cmd = '24 5f' data = super().getDevAddr() + ' 01 10 00 00 00 06 0c' for item in args: data += ' ' + super().getWordStr(item) self._lastWrite = MyFrame(head, cmd, data) return self._lastWrite
def main(): f = MyFrame() f.pack() f.mainloop()
import wx from mp3panel import Mp3Panel from myframe import MyFrame if __name__ == '__main__': app = wx.App(False) frame = MyFrame() app.MainLoop()
print(end - start) print(str(res)) ser.close() # %% # 测试命令帧抽象 import sys import serial sys.path.append('D:\house app\物联网\lmk\IoTLab\myframe.py') from myframe import MyFrame port_name = 'COM5' ser = serial.Serial(port_name) if ser.isOpen() == False: ser.open() # 获取传感器的全部数据 frame = MyFrame('fe', '24 5f', 'e0 03 01 03 00 00 00 06') # frame = MyFrame('fe', '24 5f', 'e1 03 01 03 00 00 00 06') # 获取传感器的某个数据 frame = MyFrame('fe', '24 5f', 'e0 03 01 03 00 01 00 01') ser.write(frame.toBytes()) res = ser.read(12) ser.close() # %% # 测试数据响应帧抽象 from resframe import ResFrame rf = ResFrame(res, 2) info = rf.getData() # %% # 4.26测试设备抽象