def getTemplateCount(self): """ Get the number of stored templates. @return integer(2 bytes) """ packetPayload = (FINGERPRINT_TEMPLATECOUNT, ) 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: Read successfully if (receivedPacketPayload[0] == FINGERPRINT_OK): templateCount = utilities.leftShift(receivedPacketPayload[1], 8) templateCount = templateCount | utilities.leftShift( receivedPacketPayload[2], 0) return templateCount elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION): raise Exception('Communication error') else: raise Exception('Unknown error')
def compareCharacteristics(self): """ Compare the finger characteristics of CharBuffer1 with CharBuffer2 and return the accuracy score. @return integer(2 bytes) """ packetPayload = (FINGERPRINT_COMPARECHARACTERISTICS, ) 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: Comparation successful if (receivedPacketPayload[0] == FINGERPRINT_OK): accuracyScore = utilities.leftShift(receivedPacketPayload[1], 8) accuracyScore = accuracyScore | utilities.leftShift( receivedPacketPayload[2], 0) return accuracyScore elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION): raise Exception('Communication error') ## DEBUG: The characteristics do not matching elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_NOTMATCHING): return 0 else: raise Exception('Unknown error')
def generateRandomNumber(self): """ Generates a random 32-bit decimal number. @author: Philipp Meisberger <*****@*****.**> @return int The generated random number """ packetPayload = (FINGERPRINT_GENERATERANDOMNUMBER, ) 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!') if (receivedPacketPayload[0] == FINGERPRINT_OK): pass elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION): raise Exception('Communication error') else: raise Exception('Unknown error') number = 0 number = number | utilities.leftShift(receivedPacketPayload[1], 24) number = number | utilities.leftShift(receivedPacketPayload[2], 16) number = number | utilities.leftShift(receivedPacketPayload[3], 8) number = number | utilities.leftShift(receivedPacketPayload[4], 0) return number
def getTemplateCount(self): """ Get the number of stored templates. @return integer(2 bytes) """ packetPayload = ( FINGERPRINT_TEMPLATECOUNT, ) 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: Read successfully if ( receivedPacketPayload[0] == FINGERPRINT_OK ): templateCount = utilities.leftShift(receivedPacketPayload[1], 8) templateCount = templateCount | utilities.leftShift(receivedPacketPayload[2], 0) return templateCount elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION ): raise Exception('Communication error') else: raise Exception('Unknown error')
def compareCharacteristics(self): """ Compare the finger characteristics of CharBuffer1 with CharBuffer2 and return the accuracy score. @return integer(2 bytes) """ packetPayload = ( FINGERPRINT_COMPARECHARACTERISTICS, ) 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: Comparation successful if ( receivedPacketPayload[0] == FINGERPRINT_OK ): accuracyScore = utilities.leftShift(receivedPacketPayload[1], 8) accuracyScore = accuracyScore | utilities.leftShift(receivedPacketPayload[2], 0) return accuracyScore elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION ): raise Exception('Communication error') ## DEBUG: The characteristics do not matching elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_NOTMATCHING ): return 0 else: raise Exception('Unknown error')
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')
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')
def getSystemParameters(self): """ Get all available system information of the sensor. Return a tuple that contain the following information: 0: integer(2 bytes) The status register. 1: integer(2 bytes) The system id. 2: integer(2 bytes) The storage capacity. 3: integer(2 bytes) The security level. 4: integer(4 bytes) The sensor address. 5: integer(2 bytes) The packet length. 6: integer(2 bytes) The baudrate. @return tuple """ packetPayload = (FINGERPRINT_GETSYSTEMPARAMETERS, ) 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: Read successfully if (receivedPacketPayload[0] == FINGERPRINT_OK): statusRegister = utilities.leftShift( receivedPacketPayload[1], 8) | utilities.leftShift( receivedPacketPayload[2], 0) systemID = utilities.leftShift(receivedPacketPayload[3], 8) | utilities.leftShift( receivedPacketPayload[4], 0) storageCapacity = utilities.leftShift( receivedPacketPayload[5], 8) | utilities.leftShift( receivedPacketPayload[6], 0) securityLevel = utilities.leftShift( receivedPacketPayload[7], 8) | utilities.leftShift( receivedPacketPayload[8], 0) deviceAddress = ( (receivedPacketPayload[9] << 8 | receivedPacketPayload[10]) << 8 | receivedPacketPayload[11]) << 8 | receivedPacketPayload[ 12] ## TODO packetLength = utilities.leftShift( receivedPacketPayload[13], 8) | utilities.leftShift( receivedPacketPayload[14], 0) baudRate = utilities.leftShift(receivedPacketPayload[15], 8) | utilities.leftShift( receivedPacketPayload[16], 0) return (statusRegister, systemID, storageCapacity, securityLevel, deviceAddress, packetLength, baudRate) elif (receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION): raise Exception('Communication error') else: raise Exception('Unknown error')
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)
def getSystemParameters(self): """ Get all available system information of the sensor. Return a tuple that contain the following information: 0: integer(2 bytes) The status register. 1: integer(2 bytes) The system id. 2: integer(2 bytes) The storage capacity. 3: integer(2 bytes) The security level. 4: integer(4 bytes) The sensor address. 5: integer(2 bytes) The packet length. 6: integer(2 bytes) The baudrate. @return tuple """ packetPayload = ( FINGERPRINT_GETSYSTEMPARAMETERS, ) 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: Read successfully if ( receivedPacketPayload[0] == FINGERPRINT_OK ): statusRegister = utilities.leftShift(receivedPacketPayload[1], 8) | utilities.leftShift(receivedPacketPayload[2], 0) systemID = utilities.leftShift(receivedPacketPayload[3], 8) | utilities.leftShift(receivedPacketPayload[4], 0) storageCapacity = utilities.leftShift(receivedPacketPayload[5], 8) | utilities.leftShift(receivedPacketPayload[6], 0) securityLevel = utilities.leftShift(receivedPacketPayload[7], 8) | utilities.leftShift(receivedPacketPayload[8], 0) deviceAddress = ((receivedPacketPayload[9] << 8 | receivedPacketPayload[10]) << 8 | receivedPacketPayload[11]) << 8 | receivedPacketPayload[12] ## TODO packetLength = utilities.leftShift(receivedPacketPayload[13], 8) | utilities.leftShift(receivedPacketPayload[14], 0) baudRate = utilities.leftShift(receivedPacketPayload[15], 8) | utilities.leftShift(receivedPacketPayload[16], 0) return (statusRegister, systemID, storageCapacity, securityLevel, deviceAddress, packetLength, baudRate) elif ( receivedPacketPayload[0] == FINGERPRINT_ERROR_COMMUNICATION ): raise Exception('Communication error') else: raise Exception('Unknown error')
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)