示例#1
0
    def from_raw_data(cls, data):
        """
        @param data: Bla
        @type data: bytes
        """
        if not data:
            raise EmptyMessageError()

        transmitted_crc = data[-1]
        computed_crc = crc8.calc(data[0:-1])

        if transmitted_crc != computed_crc:
            raise MessageCRCError(transmitted_crc=transmitted_crc, computed_crc=computed_crc)

        msg_type = int.from_bytes(data[0:3], byteorder='big')

        if msg_type == MessageTypes.MSG_PONG:
            msg = PongMessage.from_raw_data(data[3:-1])
        elif msg_type == MessageTypes.MSG_CONFIRMATION:
            msg = ConfirmationMessage.from_raw_data(data[3:-1])
        elif msg_type == MessageTypes.MSG_CUR_PARAMETER:
            msg = CurParameterMessage.from_raw_data(data[3:-1])
        else:
            raise UnknownMessageType()

        return msg
示例#2
0
    def encode_for_writing_without_msg_num(self):
        val = b''

        msg_type = self.message_type().to_bytes(3, byteorder='big')
        data = self.prepare_data()
        crc = crc8.calc(msg_type+data).to_bytes(1, byteorder='big')

        val += STX+STX+self.escape_bytes(msg_type)+self.escape_bytes(data)+self.escape_bytes(crc)+ETB
        return val
示例#3
0
    def encode_for_writing(self):
        val = b''

        msg_num = (self.message_number() if self.message_number() is not None else 0).to_bytes(3, byteorder='big')
        msg_type = self.message_type().to_bytes(3, byteorder='big')
        data = self.prepare_data()
        crc = crc8.calc(msg_num+msg_type+data).to_bytes(1, byteorder='big')

        val += STX+STX+self.escape_bytes(msg_num)+self.escape_bytes(msg_type)+self.escape_bytes(data)+self.escape_bytes(crc)+ETB
        return val