def encode(self, encoder): encoder.encode_str(DL645_HEAD) encoder.encode_str(self.address[::-1]) encoder.encode_str(DL645_HEAD) encoder.encode_byte(self.cmd) if self.cmd == 0x11: did_data = encoder.object2data(self.did_unit) encoder.encode_byte(len(did_data)) did_data = data_encrypt(did_data) encoder.encode_str(did_data) elif self.cmd == 0x13: encoder.encode_byte(0x00) encoder.encode_byte(checksum(encoder.get_data())) encoder.encode_str(DL645_TAIL)
def encode(self, encoder): self.padding_address() # encoder.encode_byte(0xfe) # encoder.encode_byte(0xfe) # encoder.encode_byte(0xfe) # encoder.encode_byte(0xfe) a_encoder = BinaryEncoder() a_encoder.encode_str(CJT188_HEAD) a_encoder.encode_byte(self.meter_type) a_encoder.encode_str(self.address[::-1]) a_encoder.encode_byte(self.cmd) length = len(a_encoder.object2data(self.did_unit)) a_encoder.encode_byte(length) self.did_unit.serial = self.serial a_encoder.encode_object(self.did_unit) a_encoder.encode_byte(checksum(a_encoder.get_data())) a_encoder.encode_str(CJT188_TAIL) encoder.encode_str(a_encoder.get_data())
def find_frame_in_buff(data): """ >>> receive_str =" fe fe 68 11 11 11 11 11 11 68 01 02 43 C3 3F 16 " >>> receive_data = hexstr2str(receive_str) >>> protocol = DL645Protocol() >>> protocol.find_frame_in_buff(receive_data) (True, 2, 14) >>> receive_data = hexstr2str("68 10 01 02 03 04 05 ") >>> protocol.find_frame_in_buff(receive_data) (False, 0, 0) """ start_pos = 0 found = 0 while start_pos < (len(data) - 11): start_pos = find_head(data, start_pos, DL645_HEAD) if start_pos == -1: break frame_data = data[start_pos:] if len(frame_data) < 10: break data_len = ord(frame_data[9]) if data_len + 12 > len(frame_data): start_pos += 1 continue if ord(frame_data[10 + data_len]) != checksum( frame_data[0:data_len + 10]): start_pos += 1 continue if frame_data[11 + data_len] != DL645_TAIL: start_pos += 1 continue found = 1 break if found: return True, start_pos, data_len + 12 else: return False, 0, 0
def find_frame_in_buff(data): """ >>> receive_str = "00 00 00 00 FE FE FE FE FE FE " >>> receive_str+="68 10 01 02 03 04 05 06 07 81 16 90 1F 96 00 55 55 05 2C 00 55 55 05 2C 00 00 00 00 00 00 00 00 00 26 16" >>> receive_data = hexstr2str(receive_str) >>> protocol = CJT188Protocol() >>> protocol.find_frame_in_buff(receive_data) (True, 10, 35) >>> receive_data = hexstr2str("68 10 01 02 03 04 05 ") >>> protocol.find_frame_in_buff(receive_data) (False, 0, 0) """ start_pos = 0 found = 0 while start_pos < (len(data) - 11): start_pos = find_head(data, start_pos, CJT188_HEAD) if start_pos == -1: break frame_data = data[start_pos:] if len(frame_data) < 11: break data_len = ord(frame_data[10]) if data_len + 13 > len(frame_data): start_pos += 1 continue if ord(frame_data[11 + data_len]) != checksum( frame_data[0:data_len + 11]): start_pos += 1 continue if frame_data[12 + data_len] != CJT188_TAIL: start_pos += 1 continue found = 1 break if found: return True, start_pos, data_len + 13 else: return False, 0, 0
# Conexión de socket TCP y se habilita el socket UDP del cliente try: tpc_client.connect((server, tcp_port)) udp_client.bind((client, udp_port)) except socket.error as error: print( f'ERROR: no se pudo establecer una conexión con el servidor [{error} in {timeout} seg]' ) udp_client.close() exit() except OverflowError as error: print(f'ERROR: puerto definido fuera de rango [{error}]') udp_client.close() exit() # Flujo de comunicación con el servidor try: helloiam(tpc_client, user_name) msgLength = int(msglen(tpc_client)) msg = givememsg(tpc_client, udp_client, udp_port, msgLength) checksum(tpc_client, msg) bye(tpc_client) except ValueError as error: print(error) except socket.timeout as error: print( f'ERROR: no se pudo establecer la conexión en el tiempo definido por defecto [{error} in {timeout}]' ) finally: udp_client.close()