Example #1
0
    def receive_command(self, len_of_response):
        for i in range(len_of_response):

            try:
                c = bus.read_byte(DEVICE_ADDRESS)
            except:
                #print("error in " + str(i))
                return self.BYTE_READ_FAILED

            # print("Recieved byte: " + str(hex(c)))
            msg = self.check_command(c)

        if msg != None and msg != -1 and msg != self.CRC_CHECK_FAILED:
            self.buffer_receive.clear()
            return msg
        elif msg == self.CRC_CHECK_FAILED:
            raise crc_check_failed("CRC check failed!")
        else:
            return None
    def check_command(self, received_byte):
        global buffer_receive
        global buffer_receive_index
        datalen = 0

        if buffer_receive_index == 0 and received_byte != START_BYTE_RECIEVED:
            return -1

        buffer_receive.append(received_byte)
        buffer_receive_index += 1

        if buffer_receive_index < PROTOCOL_HEADER_SIZE:
            return -1

        datalen = (buffer_receive[3] << 8) | buffer_receive[4]

        if datalen > 32:
            # print("="*50)
            # print('[{}]'.format(', '.join(hex(x) for x in buffer_receive)))
            buffer_receive_index = 0
            buffer_receive.clear()

        if buffer_receive_index == (PROTOCOL_FRAME_SIZE + datalen):

            crc_received = (
                buffer_receive[PROTOCOL_FRAME_SIZE + datalen - 2] << 8
            ) | buffer_receive[PROTOCOL_FRAME_SIZE + datalen - 1]
            # print("CRC Received: " + str(crc_received))

            crc_calculated = self.calculate_crc16(
                buffer_receive[0 : PROTOCOL_HEADER_SIZE + datalen], 1
            )
            # print("CRC Calculated: " + str(crc_calculated))

            if crc_calculated == crc_received:
                # print("CRC Check OK")
                # print('[{}]'.format(', '.join(hex(x) for x in buffer_receive)))
                buffer_receive_index = 0
                return buffer_receive[0 : PROTOCOL_FRAME_SIZE + datalen]
            else:
                print("CRC Check FAILED!")
                buffer_receive_index = 0
                raise crc_check_failed("CRC check failed!")
Example #3
0
    def check_command(self, received_byte):
        datalen = 0

        if self.buffer_receive_index == 0 and received_byte != START_BYTE_RECIEVED:
            return -1

        self.buffer_receive.append(received_byte)
        self.buffer_receive_index += 1

        if self.buffer_receive_index < PROTOCOL_HEADER_SIZE:
            return -1

        datalen = (self.buffer_receive[3] << 8) | self.buffer_receive[4]

        if datalen > 32:
            # print("="*50)
            # print('[{}]'.format(', '.join(hex(x) for x in self.buffer_receive)))
            self.buffer_receive_index = 0
            self.buffer_receive.clear()

        if self.buffer_receive_index == (PROTOCOL_FRAME_SIZE + datalen):

            crc_received = (
                self.buffer_receive[PROTOCOL_FRAME_SIZE + datalen - 2] <<
                8) | self.buffer_receive[PROTOCOL_FRAME_SIZE + datalen - 1]
            # print("CRC Received: " + str(crc_received))

            crc_calculated = self.calculate_crc16(
                self.buffer_receive[0:PROTOCOL_HEADER_SIZE + datalen], 1)
            # print("CRC Calculated: " + str(crc_calculated))

            #debugging invalid information received

            try:
                #print("CRC Check OK")
                #print('[{}]'.format(', '.join(hex(x) for x in self.buffer_receive)))
                self.buffer_receive_index = 0
                return self.buffer_receive[0:PROTOCOL_FRAME_SIZE + datalen]
            except Exception as e:
                #print("CRC Check FAILED!")
                #print(e)
                self.buffer_receive_index = 0
                raise crc_check_failed("CRC check failed!")