def deal645Frame(frame): l = [False] if len(frame) < MIN_LEN_645FRAME: return l frame = frame.upper() for i in range(0, len(frame), 2): if frame[i:i + 2] == '68' and frame[( i + POS_64507_HEAD2):(i + POS_64507_HEAD2) + 2] == '68': dataLen = int(frame[(i + POS_64507_LEN):(i + POS_64507_LEN + 2)], 16) * 2 if dataLen + POS_64507_LEN < len(frame): frameLen = i + dataLen + POS_64507_LEN checkSum = calcCheckSum(frame[i:(frameLen + 2)]) checkSum = checkSum[-2:] checkSum = checkSum.upper() if checkSum == frame[frameLen + 2:frameLen + 4] and \ frame[frameLen + 4:frameLen + 6] == '16': addr = frame[i + POS_64507_ADDR:i + POS_64507_ADDR + 12] ctrl = frame[i + POS_64507_CTRL:i + POS_64507_CTRL + 2] data = frame[i + POS_64507_DATA:i + POS_64507_DATA + dataLen] l[0] = True l += [addr, ctrl, data] return l return l
def dl645_makeframe(dt): # datavalue 转换 if 'datavalue' not in dt or dt['datavalue'] == None or len( dt['datavalue']) == 0: dt['data'] = [0x02] dt['ctrl'] |= 0xC0 else: dt['data'] += fat.str2hex(dt['datavalue'], 0) # 计算长度 dlen = len(dt['data']) dt['dlen'] = fat.hex2str([dlen], 0) dt['ctrl'] = fat.hex2str([dt['ctrl'] | 0x80], 0) dt['addr'] = pfun._strReverse(dt['addr']) dt['data'] = fat.hex2str(dt['data'], 1) # hex frame = '68' + dt['addr'] + '68' + dt['ctrl'] + dt['dlen'] + dt[ 'data'] # + dt['cs'] + '16' # 计算CRC dt['cs'] = pfun.calcCheckSum(frame) frame += dt['cs'] + '16' # print('Send:', frame) # 字节间增加空格 framespace = '' for i in range(0, len(frame), 2): framespace += frame[i:i + 2] + ' ' return framespace
def dl645_dealframe(frame): frame = frame.replace(' ', '') if len(frame) < MIN_LEN_645FRAME: return False, None frame = frame.upper() dt = {'addr': '', 'ctrl': '', 'data': ''} for i in range(0, len(frame), 2): if frame[i:i + 2] == '68' and frame[( i + POS_64507_HEAD2):(i + POS_64507_HEAD2) + 2] == '68': dataLen = int(frame[(i + POS_64507_LEN):(i + POS_64507_LEN + 2)], 16) * 2 if dataLen + POS_64507_LEN < len(frame): frameLen = i + dataLen + POS_64507_LEN checkSum = calcCheckSum(frame[i:(frameLen + 2)]) checkSum = checkSum[-2:] checkSum = checkSum.upper() if checkSum == frame[frameLen + 2:frameLen + 4] and \ frame[frameLen + 4:frameLen + 6] == '16': addr = frame[i + POS_64507_ADDR:i + POS_64507_ADDR + 12].upper() dt['addr'] = pfun._strReverse(addr) # dt['ctrl'] = frame[i + POS_64507_CTRL:i + POS_64507_CTRL + 2] dt['ctrl'] = int( frame[i + POS_64507_CTRL:i + POS_64507_CTRL + 2], 16) # dt['data'] = frame[i + POS_64507_DATA:i + POS_64507_DATA + dataLen] datastr = frame[i + POS_64507_DATA:i + POS_64507_DATA + dataLen] dt['data'] = fat.str2hex(datastr, 1) if dt['ctrl'] & 0x80 == 0: # 只响应抄读帧 return True, dt return False, None
def make645Frame(head, addr, ctrl, data): frame = '68' + addr + '68' + ctrl datalen = hex(len(data) // 2).replace('0x', '0000')[-2:] frame += datalen frame += data checkSum = calcCheckSum(frame) checkSum = checkSum[-2:] frame += (checkSum + '16') frame = head + frame return frame
def make645Frame(head, addr, ctrl, data, mode): frame = '68' + addr + '68' + ctrl datalen = str(hex(len(data) // 2)) if len(datalen) < 4: datalen = '0' + datalen[-1] frame += datalen frame += data checkSum = calcCheckSum(frame) checkSum = checkSum[-2:] frame += (checkSum + '16') frame = head + frame return frame
def make3761Frame(self, du): self.frame['ctrl'] = '40' datastr = self.frame['ctrl'] + self.frame['A1'] + self.frame['A2'] + self.frame['A3'] datastr += self.frame['AFN'] + self.frame['SEQ'] datastr += self.frame['DA1'] + self.frame['DA2'] datastr += self.frame['DT1'] + self.frame['DT2'] datastr += du # 数据单元 self.frame['crc'] = calcCheckSum(datastr) datalen = int(len(datastr)/2) datalenstr = hex(datalen<<2).replace('0x','00')[-2:] + '00' self.frame['len'] = datalenstr[-4:] frame = '68' + self.frame['len'] + self.frame['len'] + '68' + datastr + self.frame['crc'] + '16' return frame
def deal3761Frame(self, frame): for i in range(0, len(frame), 2): if frame[i:i + 2] == '68' and frame[i + 10:i + 12] == '68': framelen = int(frame[i + 4:i + 6] + frame[i + 2:i + 4], 16) >> 2 self.frame['end'] = frame[i + 12 + framelen * 2 + 2:i + 12 + framelen * 2 + 4] self.frame['crc'] = frame[i + 12 + framelen * 2 + 0:i + 12 + framelen * 2 + 2] if calcCheckSum(frame[i + 12:i + 12 + framelen * 2]) != self.frame['crc']: return False self.frame['ctrl'] = frame[i + 12:i + 14] self.frame['A1'] = frame[i + 14:i + 18] self.frame['A2'] = frame[i + 18:i + 22] self.frame['A3'] = frame[i + 22:i + 24] self.frame['AFN'] = frame[i + 24:i + 26] self.frame['SEQ'] = frame[i + 26:i + 28] self.frame['DA1'] = frame[i + 28:i + 30] self.frame['DA2'] = frame[i + 30:i + 32] self.frame['pn'] = self.Data2Fun(int(self.frame['DA1'], 16), int(self.frame['DA2'], 16)) self.frame['DT1'] = frame[i + 30:i + 32] self.frame['DT2'] = frame[i + 32:i + 34] self.frame['Fn'] = self.Data2Fun(int(self.frame['DT1'], 16), int(self.frame['DT2'], 16)) return True return False