def do_build(self): packet = self.start packet += self.identity packet += self.control packet += self.sequence packet += self.__length__ packet += str(self.__data__) packet += crc_str(packet) return packet
def __repr__(self): if isinstance(self.__data__, C1218Request): repr_data = repr(self.__data__) else: repr_data = '0x' + str(self.__data__).encode('hex') return '<C1218Packet data=' + repr_data + ' data_len=' + str( len(self.__data__)) + ' crc=0x' + crc_str( self.start + self.identity + self.control + self.sequence + self.__length__ + str(self.__data__)).encode('hex') + ' >'
def parse(data): if len(data) < 8: raise Exception('invalid data (size)') if data[0] != '\xee': raise Exception('invalid start byte') identity = data[1] control = ord(data[2]) sequence = data[3] length = struct.unpack('>H', data[4:6])[0] chksum = data[-2:] if crc_str(data[:-2]) != chksum: raise Exception('invalid check sum') data = data[6:-2] if ord(data[0]) in C1218_REQUEST_IDS: data = C1218_REQUEST_IDS[ord(data[0])].parse(data) frame = C1218Packet(data, control, length) frame.identity = identity frame.sequence = sequence return frame
def __repr__(self): if isinstance(self.__data__, C1218Request): repr_data = repr(self.__data__) else: repr_data = '0x' + str(self.__data__).encode('hex') return '<C1218Packet data=' + repr_data + ' data_len=' + str(len(self.__data__)) + ' crc=0x' + crc_str(self.start + self.identity + self.control + self.sequence + self.__length__ + str(self.__data__)).encode('hex') + ' >'