Exemplo n.º 1
0
    def searchTemplate(self):
        """
        Search the finger characteristiccs in CharBuffer in database.

        Return a tuple that contain the following information:
        0: integer(2 bytes) The position number of found template.
        1: integer(2 bytes) The accuracy score of found template.

        @return tuple
        """

        # CharBuffer1 and CharBuffer2 are the same in this case
        charBufferNumber = 0x01

        # Begin search at page 0x0000 for 0x00A3 (means 163) templates
        positionStart = 0x0000
        templatesCount = 0x00A3

        packetPayload = (
            FINGERPRINT_SEARCHTEMPLATE,
            charBufferNumber,
            utilities.rightShift(positionStart, 8),
            utilities.rightShift(positionStart, 0),
            utilities.rightShift(templatesCount, 8),
            utilities.rightShift(templatesCount, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if (receivedPacketType != FINGERPRINT_ACKPACKET):
            raise Exception('The received packet is no ack packet!')

        # DEBUG: Found template
        if (receivedPacketPayload[0] == FINGERPRINT_OK):

            positionNumber = utilities.leftShift(receivedPacketPayload[1], 8)
            positionNumber = positionNumber | utilities.leftShift(
                receivedPacketPayload[2], 0)

            accuracyScore = utilities.leftShift(receivedPacketPayload[3], 8)
            accuracyScore = accuracyScore | utilities.leftShift(
                receivedPacketPayload[4], 0)

            return (positionNumber, accuracyScore)

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION):
            raise Exception('Communication error')

        # DEBUG: Did not found a matching template
        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_NOTEMPLATEFOUND):
            return (-1, -1)

        else:
            raise Exception('Unknown error')
Exemplo n.º 2
0
    def searchTemplate(self):
        """
        Search the finger characteristiccs in CharBuffer in database.

        Return a tuple that contain the following information:
        0: integer(2 bytes) The position number of found template.
        1: integer(2 bytes) The accuracy score of found template.

        @return tuple
        """

        ## CharBuffer1 and CharBuffer2 are the same in this case
        charBufferNumber = 0x01

        ## Begin search at index 0
        positionStart = 0x0000
        templatesCount = self.getStorageCapacity()

        packetPayload = (
            FINGERPRINT_SEARCHTEMPLATE,
            charBufferNumber,
            utilities.rightShift(positionStart, 8),
            utilities.rightShift(positionStart, 0),
            utilities.rightShift(templatesCount, 8),
            utilities.rightShift(templatesCount, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if (receivedPacketType != FINGERPRINT_ACKPACKET):
            raise Exception('The received packet is no ack packet!')

        ## DEBUG: Found template
        if (receivedPacketPayload[0] == FINGERPRINT_OK):

            positionNumber = utilities.leftShift(receivedPacketPayload[1], 8)
            positionNumber = positionNumber | utilities.leftShift(
                receivedPacketPayload[2], 0)

            accuracyScore = utilities.leftShift(receivedPacketPayload[3], 8)
            accuracyScore = accuracyScore | utilities.leftShift(
                receivedPacketPayload[4], 0)

            return (positionNumber, accuracyScore)

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION):
            raise Exception('Communication error')

        ## DEBUG: Did not found a matching template
        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_NOTEMPLATEFOUND):
            return (-1, -1)

        else:
            raise Exception('Unknown error')
Exemplo n.º 3
0
    def loadTemplate(self, positionNumber, charBufferNumber=0x01):
        """
        Load an existing template specified by position number to specified CharBuffer.

        @param integer(2 bytes) positionNumber
        @param integer(1 byte) charBufferNumber
        @return boolean
        """

        if (positionNumber < 0x0000
                or positionNumber >= self.getStorageCapacity()):
            raise ValueError('The given position number is invalid!')

        if (charBufferNumber != 0x01 and charBufferNumber != 0x02):
            raise ValueError('The given charbuffer number is invalid!')

        packetPayload = (
            FINGERPRINT_LOADTEMPLATE,
            charBufferNumber,
            utilities.rightShift(positionNumber, 8),
            utilities.rightShift(positionNumber, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if (receivedPacketType != FINGERPRINT_ACKPACKET):
            raise Exception('The received packet is no ack packet!')

        ## DEBUG: Template loaded successful
        if (receivedPacketPayload[0] == FINGERPRINT_OK):
            return True

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION):
            raise Exception('Communication error')

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_LOADTEMPLATE):
            raise Exception('The template could not be read')

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_INVALIDPOSITION):
            raise Exception('Could not load template from that position')

        else:
            raise Exception('Unknown error')
Exemplo n.º 4
0
    def storeTemplate(self, positionNumber, charBufferNumber=0x01):
        """
        Save a template from the specified CharBuffer to the given position number.

        @param integer(2 bytes) positionNumber
        @param integer(1 byte) charBufferNumber
        @return boolean
        """

        if (positionNumber < 0x0000 or positionNumber > 0x00A3):
            raise ValueError('The given position number is invalid!')

        if (charBufferNumber != 0x01 and charBufferNumber != 0x02):
            raise ValueError('The given charbuffer number is invalid!')

        packetPayload = (
            FINGERPRINT_STORETEMPLATE,
            charBufferNumber,
            utilities.rightShift(positionNumber, 8),
            utilities.rightShift(positionNumber, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if (receivedPacketType != FINGERPRINT_ACKPACKET):
            raise Exception('The received packet is no ack packet!')

        ## DEBUG: Template stored successful
        if (receivedPacketPayload[0] == FINGERPRINT_OK):
            return True

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION):
            raise Exception('Communication error')

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_INVALIDPOSITION):
            raise Exception('Could not store template in that position')

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_FLASH):
            raise Exception('Error writing to flash')

        else:
            raise Exception('Unknown error')
Exemplo n.º 5
0
    def loadTemplate(self, positionNumber, charBufferNumber = 0x01):
        """
        Load an existing template specified by position number to specified CharBuffer.

        @param integer(2 bytes) positionNumber
        @param integer(1 byte) charBufferNumber
        @return boolean
        """

        if ( positionNumber < 0x0000 or positionNumber > 0x0400 ):
            raise ValueError('The given position number is invalid!')

        if ( charBufferNumber != 0x01 and charBufferNumber != 0x02 ):
            raise ValueError('The given charbuffer number is invalid!')

        packetPayload = (
            FINGERPRINT_LOADTEMPLATE,
            charBufferNumber,
            utilities.rightShift(positionNumber, 8),
            utilities.rightShift(positionNumber, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if ( receivedPacketType != FINGERPRINT_ACKPACKET ):
            raise Exception('The received packet is no ack packet!')

        ## DEBUG: Template loaded successful
        if ( receivedPacketPayload[0] == FINGERPRINT_OK ):
            return True

        elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION ):
            raise Exception('Communication error')

        elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_LOADTEMPLATE ):
            raise Exception('The template could not be read')

        elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_INVALIDPOSITION ):
            raise Exception('Could not load template from that position')

        else:
            raise Exception('Unknown error')
Exemplo n.º 6
0
    def deleteTemplate(self, positionNumber):
        """
        Delete one template from fingerprint database.

        @param integer(2 bytes) positionNumber
        @return boolean
        """

        if (positionNumber < 0x0000
                or positionNumber >= self.getStorageCapacity()):
            raise ValueError('The given position number is invalid!')

        ## Delete only one template
        count = 0x0001

        packetPayload = (
            FINGERPRINT_DELETETEMPLATE,
            utilities.rightShift(positionNumber, 8),
            utilities.rightShift(positionNumber, 0),
            utilities.rightShift(count, 8),
            utilities.rightShift(count, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if (receivedPacketType != FINGERPRINT_ACKPACKET):
            raise Exception('The received packet is no ack packet!')

        ## DEBUG: Template deleted successful
        if (receivedPacketPayload[0] == FINGERPRINT_OK):
            return True

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION):
            raise Exception('Communication error')

        ## DEBUG: Could not delete template
        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_DELETETEMPLATE):
            return False

        else:
            raise Exception('Unknown error')
Exemplo n.º 7
0
    def deleteTemplate(self, positionNumber):
        """
        Delete one template from fingerprint database.

        @param integer(2 bytes) positionNumber
        @return boolean
        """

        if ( positionNumber < 0x0000 or positionNumber > 0x0400 ):
            raise ValueError('The given position number is invalid!')

        ## Delete only one template
        count = 0x0001

        packetPayload = (
            FINGERPRINT_DELETETEMPLATE,
            utilities.rightShift(positionNumber, 8),
            utilities.rightShift(positionNumber, 0),
            utilities.rightShift(count, 8),
            utilities.rightShift(count, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if ( receivedPacketType != FINGERPRINT_ACKPACKET ):
            raise Exception('The received packet is no ack packet!')

        ## DEBUG: Template deleted successful
        if ( receivedPacketPayload[0] == FINGERPRINT_OK ):
            return True

        elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION ):
            raise Exception('Communication error')

        ## DEBUG: Could not delete template
        elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_DELETETEMPLATE ):
            return False

        else:
            raise Exception('Unknown error')
Exemplo n.º 8
0
    def setPassword(self, newPassword):
        """
        Set the password of the sensor.

        @param integer(4 bytes) newPassword
        @return boolean
        """

        # Validate the password (maximum 4 bytes)
        if (newPassword < 0x00000000 or newPassword > 0xFFFFFFFF):
            raise ValueError('The given password is invalid!')

        packetPayload = (
            FINGERPRINT_SETPASSWORD,
            utilities.rightShift(newPassword, 24),
            utilities.rightShift(newPassword, 16),
            utilities.rightShift(newPassword, 8),
            utilities.rightShift(newPassword, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if (receivedPacketType != FINGERPRINT_ACKPACKET):
            raise Exception('The received packet is no ack packet!')

        # DEBUG: Password set was successful
        if (receivedPacketPayload[0] == FINGERPRINT_OK):
            return True

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION):
            raise Exception('Communication error')

        else:
            raise Exception('Unknown error')
Exemplo n.º 9
0
    def setAddress(self, newAddress):
        """
        Set the module address of the sensor.

        @param integer(4 bytes) newAddress
        @return boolean
        """

        ## Validate the address (maximum 4 bytes)
        if (newAddress < 0x00000000 or newAddress > 0xFFFFFFFF):
            raise ValueError('The given address is invalid!')

        packetPayload = (
            FINGERPRINT_SETADDRESS,
            utilities.rightShift(newAddress, 24),
            utilities.rightShift(newAddress, 16),
            utilities.rightShift(newAddress, 8),
            utilities.rightShift(newAddress, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if (receivedPacketType != FINGERPRINT_ACKPACKET):
            raise Exception('The received packet is no ack packet!')

        ## DEBUG: Address set was successful
        if (receivedPacketPayload[0] == FINGERPRINT_OK):
            return True

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION):
            raise Exception('Communication error')

        else:
            raise Exception('Unknown error')
Exemplo n.º 10
0
    def setAddress(self, newAddress):
        """
        Set the module address of the sensor.

        @param integer(4 bytes) newAddress
        @return boolean
        """

        ## Validate the address (maximum 4 bytes)
        if ( newAddress < 0x00000000 or newAddress > 0xFFFFFFFF ):
            raise ValueError('The given address is invalid!')

        packetPayload = (
            FINGERPRINT_SETADDRESS,
            utilities.rightShift(newAddress, 24),
            utilities.rightShift(newAddress, 16),
            utilities.rightShift(newAddress, 8),
            utilities.rightShift(newAddress, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if ( receivedPacketType != FINGERPRINT_ACKPACKET ):
            raise Exception('The received packet is no ack packet!')

        ## DEBUG: Address set was successful
        if ( receivedPacketPayload[0] == FINGERPRINT_OK ):
            return True

        elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION ):
            raise Exception('Communication error')

        else:
            raise Exception('Unknown error')
Exemplo n.º 11
0
    def verifyPassword(self):
        """
        Verifie password of the fingerprint sensor.

        @return boolean
        """

        packetPayload = (
            FINGERPRINT_VERIFYPASSWORD,
            utilities.rightShift(self.__password, 24),
            utilities.rightShift(self.__password, 16),
            utilities.rightShift(self.__password, 8),
            utilities.rightShift(self.__password, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if (receivedPacketType != FINGERPRINT_ACKPACKET):
            raise Exception('The received packet is no ack packet!')

        ## DEBUG: Sensor password is correct
        if (receivedPacketPayload[0] == FINGERPRINT_OK):
            return True

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION):
            raise Exception('Communication error')

        ## DEBUG: Sensor password is wrong
        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_WRONGPASSWORD):
            return False

        else:
            raise Exception('Unknown error')
Exemplo n.º 12
0
    def verifyPassword(self):
        """
        Verifie password of the fingerprint sensor.

        @return boolean
        """

        packetPayload = (
            FINGERPRINT_VERIFYPASSWORD,
            utilities.rightShift(self.__password, 24),
            utilities.rightShift(self.__password, 16),
            utilities.rightShift(self.__password, 8),
            utilities.rightShift(self.__password, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if ( receivedPacketType != FINGERPRINT_ACKPACKET ):
            raise Exception('The received packet is no ack packet!')

        ## DEBUG: Sensor password is correct
        if ( receivedPacketPayload[0] == FINGERPRINT_OK ):
            return True

        elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION ):
            raise Exception('Communication error')

        ## DEBUG: Sensor password is wrong
        elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_WRONGPASSWORD ):
            return False

        else:
            raise Exception('Unknown error')
Exemplo n.º 13
0
    def __writePacket(self, packetType, packetPayload):
        """
        Send a packet to fingerprint sensor.

        @param integer(1 byte) packetType
        @param tuple packetPayload

        @return void
        """

        ## Write header (one byte at once)
        self.__serial.write(utilities.byteToString(utilities.rightShift(FINGERPRINT_STARTCODE, 8)))
        self.__serial.write(utilities.byteToString(utilities.rightShift(FINGERPRINT_STARTCODE, 0)))

        self.__serial.write(utilities.byteToString(utilities.rightShift(self.__address, 24)))
        self.__serial.write(utilities.byteToString(utilities.rightShift(self.__address, 16)))
        self.__serial.write(utilities.byteToString(utilities.rightShift(self.__address, 8)))
        self.__serial.write(utilities.byteToString(utilities.rightShift(self.__address, 0)))

        self.__serial.write(utilities.byteToString(packetType))

        ## The packet length = package payload (n bytes) + checksum (2 bytes)
        packetLength = len(packetPayload) + 2

        self.__serial.write(utilities.byteToString(utilities.rightShift(packetLength, 8)))
        self.__serial.write(utilities.byteToString(utilities.rightShift(packetLength, 0)))

        ## The packet checksum = packet type (1 byte) + packet length (2 bytes) + payload (n bytes)
        packetChecksum = packetType + utilities.rightShift(packetLength, 8) + utilities.rightShift(packetLength, 0)

        ## Write payload
        for i in range(0, len(packetPayload)):
            self.__serial.write(utilities.byteToString(packetPayload[i]))
            packetChecksum += packetPayload[i]

        ## Write checksum (2 bytes)
        self.__serial.write(utilities.byteToString(utilities.rightShift(packetChecksum, 8)))
        self.__serial.write(utilities.byteToString(utilities.rightShift(packetChecksum, 0)))
Exemplo n.º 14
0
    def __readPacket(self):
        """
        Receive a packet from fingerprint sensor.

        Return a tuple that contain the following information:
        0: integer(1 byte) The packet type.
        1: integer(n bytes) The packet payload.

        @return tuple
        """

        receivedPacketData = []
        i = 0

        while ( True ):

            ## Read one byte
            receivedFragment = self.__serial.read()

            if ( len(receivedFragment) != 0 ):
                receivedFragment = utilities.stringToByte(receivedFragment)
                ## print 'Received packet fragment = ' + hex(receivedFragment)

            ## Insert byte if packet seems valid
            receivedPacketData.insert(i, receivedFragment)
            i += 1

            ## Packet could be complete (the minimal packet size is 12 bytes)
            if ( i >= 12 ):

                ## Check the packet header
                if ( receivedPacketData[0] != utilities.rightShift(FINGERPRINT_STARTCODE, 8) or receivedPacketData[1] != utilities.rightShift(FINGERPRINT_STARTCODE, 0) ):
                    raise Exception('The received packet do not begin with a valid header!')

                ## Calculate packet payload length (combine the 2 length bytes)
                packetPayloadLength = utilities.leftShift(receivedPacketData[7], 8)
                packetPayloadLength = packetPayloadLength | utilities.leftShift(receivedPacketData[8], 0)

                ## Check if the packet is still fully received
                ## Condition: index counter < packet payload length + packet frame
                if ( i < packetPayloadLength + 9 ):
                    continue

                ## At this point the packet should be fully received

                packetType = receivedPacketData[6]

                ## Calculate checksum:
                ## checksum = packet type (1 byte) + packet length (2 bytes) + packet payload (n bytes)
                packetChecksum = packetType + receivedPacketData[7] + receivedPacketData[8]

                packetPayload = []

                ## Collect package payload (ignore the last 2 checksum bytes)
                for j in range(9, 9 + packetPayloadLength - 2):
                    packetPayload.append(receivedPacketData[j])
                    packetChecksum += receivedPacketData[j]

                ## Calculate full checksum of the 2 separate checksum bytes
                receivedChecksum = utilities.leftShift(receivedPacketData[i - 2], 8)
                receivedChecksum = receivedChecksum | utilities.leftShift(receivedPacketData[i - 1], 0)

                if ( receivedChecksum != packetChecksum ):
                    raise Exception('The received packet is corrupted (the checksum is wrong)!')

                return (packetType, packetPayload)
Exemplo n.º 15
0
    def storeTemplate(self, positionNumber=-1, charBufferNumber=0x01):
        """
        Save a template from the specified CharBuffer to the given position number.

        @param integer(2 bytes) positionNumber
        @param integer(1 byte) charBufferNumber
        @return integer
        """

        ## Find a free index
        if (positionNumber == -1):
            for page in range(0, 4):
                ## Free index found?
                if (positionNumber >= 0):
                    break

                templateIndex = self.getTemplateIndex(page)

                for i in range(0, len(templateIndex)):
                    ## Index not used?
                    if (templateIndex[i] == False):
                        positionNumber = (len(templateIndex) * page) + i
                        break

        if (positionNumber < 0x0000
                or positionNumber >= self.getStorageCapacity()):
            raise ValueError('The given position number is invalid!')

        if (charBufferNumber != 0x01 and charBufferNumber != 0x02):
            raise ValueError('The given charbuffer number is invalid!')

        packetPayload = (
            FINGERPRINT_STORETEMPLATE,
            charBufferNumber,
            utilities.rightShift(positionNumber, 8),
            utilities.rightShift(positionNumber, 0),
        )

        self.__writePacket(FINGERPRINT_COMMANDPACKET, packetPayload)
        receivedPacket = self.__readPacket()

        receivedPacketType = receivedPacket[0]
        receivedPacketPayload = receivedPacket[1]

        if (receivedPacketType != FINGERPRINT_ACKPACKET):
            raise Exception('The received packet is no ack packet!')

        ## DEBUG: Template stored successful
        if (receivedPacketPayload[0] == FINGERPRINT_OK):
            return positionNumber

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION):
            raise Exception('Communication error')

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_INVALIDPOSITION):
            raise Exception('Could not store template in that position')

        elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_FLASH):
            raise Exception('Error writing to flash')

        else:
            raise Exception('Unknown error')
Exemplo n.º 16
0
    def __readPacket(self):
        """
        Receive a packet from fingerprint sensor.

        Return a tuple that contain the following information:
        0: integer(1 byte) The packet type.
        1: integer(n bytes) The packet payload.

        @return tuple
        """

        receivedPacketData = []
        i = 0

        while (True):

            ## Read one byte
            receivedFragment = self.__serial.read()

            if (len(receivedFragment) != 0):
                receivedFragment = utilities.stringToByte(receivedFragment)
                ## print 'Received packet fragment = ' + hex(receivedFragment)

            ## Insert byte if packet seems valid
            receivedPacketData.insert(i, receivedFragment)
            i += 1

            ## Packet could be complete (the minimal packet size is 12 bytes)
            if (i >= 12):

                ## Check the packet header
                if (receivedPacketData[0] != utilities.rightShift(
                        FINGERPRINT_STARTCODE, 8)
                        or receivedPacketData[1] != utilities.rightShift(
                            FINGERPRINT_STARTCODE, 0)):
                    raise Exception(
                        'The received packet do not begin with a valid header!'
                    )

                ## Calculate packet payload length (combine the 2 length bytes)
                packetPayloadLength = utilities.leftShift(
                    receivedPacketData[7], 8)
                packetPayloadLength = packetPayloadLength | utilities.leftShift(
                    receivedPacketData[8], 0)

                ## Check if the packet is still fully received
                ## Condition: index counter < packet payload length + packet frame
                if (i < packetPayloadLength + 9):
                    continue

                ## At this point the packet should be fully received

                packetType = receivedPacketData[6]

                ## Calculate checksum:
                ## checksum = packet type (1 byte) + packet length (2 bytes) + packet payload (n bytes)
                packetChecksum = packetType + receivedPacketData[
                    7] + receivedPacketData[8]

                packetPayload = []

                ## Collect package payload (ignore the last 2 checksum bytes)
                for j in range(9, 9 + packetPayloadLength - 2):
                    packetPayload.append(receivedPacketData[j])
                    packetChecksum += receivedPacketData[j]

                ## Calculate full checksum of the 2 separate checksum bytes
                receivedChecksum = utilities.leftShift(
                    receivedPacketData[i - 2], 8)
                receivedChecksum = receivedChecksum | utilities.leftShift(
                    receivedPacketData[i - 1], 0)

                if (receivedChecksum != packetChecksum):
                    raise Exception(
                        'The received packet is corrupted (the checksum is wrong)!'
                    )

                return (packetType, packetPayload)
Exemplo n.º 17
0
    def __writePacket(self, packetType, packetPayload):
        """
        Send a packet to fingerprint sensor.

        @param integer(1 byte) packetType
        @param tuple packetPayload

        @return void
        """

        ## Write header (one byte at once)
        self.__serial.write(
            utilities.byteToString(
                utilities.rightShift(FINGERPRINT_STARTCODE, 8)))
        self.__serial.write(
            utilities.byteToString(
                utilities.rightShift(FINGERPRINT_STARTCODE, 0)))

        self.__serial.write(
            utilities.byteToString(utilities.rightShift(self.__address, 24)))
        self.__serial.write(
            utilities.byteToString(utilities.rightShift(self.__address, 16)))
        self.__serial.write(
            utilities.byteToString(utilities.rightShift(self.__address, 8)))
        self.__serial.write(
            utilities.byteToString(utilities.rightShift(self.__address, 0)))

        self.__serial.write(utilities.byteToString(packetType))

        ## The packet length = package payload (n bytes) + checksum (2 bytes)
        packetLength = len(packetPayload) + 2

        self.__serial.write(
            utilities.byteToString(utilities.rightShift(packetLength, 8)))
        self.__serial.write(
            utilities.byteToString(utilities.rightShift(packetLength, 0)))

        ## The packet checksum = packet type (1 byte) + packet length (2 bytes) + payload (n bytes)
        packetChecksum = packetType + utilities.rightShift(
            packetLength, 8) + utilities.rightShift(packetLength, 0)

        ## Write payload
        for i in range(0, len(packetPayload)):
            self.__serial.write(utilities.byteToString(packetPayload[i]))
            packetChecksum += packetPayload[i]

        ## Write checksum (2 bytes)
        self.__serial.write(
            utilities.byteToString(utilities.rightShift(packetChecksum, 8)))
        self.__serial.write(
            utilities.byteToString(utilities.rightShift(packetChecksum, 0)))