从外部源将指纹数据加载到R305指纹

时间:2019-04-07 16:18:25

标签: arduino

我正在尝试从外部源指纹传感器R305加载指纹模板以进行检查。我按照它的数据表在库中生成一些功能,以将字符数组下载到char-buffer-2并使用它们检查指纹。但是,它只能识别从外部源(在我的情况下为SD卡)分配的最后一个指纹。

首先,我尝试将SD卡中的模板加载到传感器的char-buffer-2并按照ID的格式存储它,该代码遵循Adafruits提供的原始代码。但是,它只能最后分配最后一个指纹。之后,我设计了加载到char-buffer-2并直接检查它。不幸的是,我遇到了与先前方法相同的错误。

我只是在下面添加一些其他功能。请将它们添加到Adafruit的标准库中 Adafruit_Fingerprint.cpp

var quotes = JsonConvert.DeserializeObject(json, new QuoteListConverter());

Adafruit_Fingerprint.h

uint8_t Adafruit_Fingerprint::setModel(void) {
    uint8_t packet[] = {FINGERPRINT_DOWNLOAD, 0x02};
    writePacket(theAddress, FINGERPRINT_COMMANDPACKET, sizeof(packet)+2, packet);
    uint8_t len = getReply(packet);

    if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
        return -1;
    return packet[1];
}

void Adafruit_Fingerprint::sendTemplate(uint8_t bytePacket){
  #if ARDUINO >= 100
    mySerial->write(bytePacket);
  #else
    mySerial->print(bytePacket, BYTE);
  #endif
}

uint8_t Adafruit_Fingerprint::getMatch(void) {
    uint8_t packet[] = {FINGERPRINT_MATCH, 0x01};
    writePacket(theAddress, FINGERPRINT_COMMANDPACKET, sizeof(packet)+2, packet);
    uint8_t len = getReply(packet);

    if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
        return -1;


    confidence = 0xFFFF;
    confidence = packet[2];
    confidence <<= 8;
    confidence |= packet[3];

    return packet[1];
}

void Adafruit_Fingerprint::writePacket(uint32_t addr, uint8_t packettype, 
        uint16_t len, uint8_t *packet) {
#ifdef FINGERPRINT_DEBUG
    Serial.print("---> 0x");
    Serial.print((uint8_t)(FINGERPRINT_STARTCODE >> 8), HEX);
    Serial.print(" 0x");
    Serial.print((uint8_t)FINGERPRINT_STARTCODE, HEX);
    Serial.print(" 0x");
    Serial.print((uint8_t)(addr >> 24), HEX);
    Serial.print(" 0x");
    Serial.print((uint8_t)(addr >> 16), HEX);
    Serial.print(" 0x");
    Serial.print((uint8_t)(addr >> 8), HEX);
    Serial.print(" 0x");
    Serial.print((uint8_t)(addr), HEX);
    Serial.print(" 0x");
    Serial.print((uint8_t)packettype, HEX);
    Serial.print(" 0x");
    Serial.print((uint8_t)(len >> 8), HEX);
    Serial.print(" 0x");
    Serial.print((uint8_t)(len), HEX);
#endif

#if ARDUINO >= 100
    mySerial->write((uint8_t)(FINGERPRINT_STARTCODE >> 8));
    mySerial->write((uint8_t)FINGERPRINT_STARTCODE);
    mySerial->write((uint8_t)(addr >> 24));
    mySerial->write((uint8_t)(addr >> 16));
    mySerial->write((uint8_t)(addr >> 8));
    mySerial->write((uint8_t)(addr));
    mySerial->write((uint8_t)packettype);
    mySerial->write((uint8_t)(len >> 8));
    mySerial->write((uint8_t)(len));
#else
    mySerial->print((uint8_t)(FINGERPRINT_STARTCODE >> 8), BYTE);
    mySerial->print((uint8_t)FINGERPRINT_STARTCODE, BYTE);
    mySerial->print((uint8_t)(addr >> 24), BYTE);
    mySerial->print((uint8_t)(addr >> 16), BYTE);
    mySerial->print((uint8_t)(addr >> 8), BYTE);
    mySerial->print((uint8_t)(addr), BYTE);
    mySerial->print((uint8_t)packettype, BYTE);
    mySerial->print((uint8_t)(len >> 8), BYTE);
    mySerial->print((uint8_t)(len), BYTE);
#endif

    uint16_t sum = (len>>8) + (len&0xFF) + packettype;
    for (uint8_t i=0; i< len-2; i++) {
#if ARDUINO >= 100
        mySerial->write((uint8_t)(packet[i]));
#else
        mySerial->print((uint8_t)(packet[i]), BYTE);
#endif
#ifdef FINGERPRINT_DEBUG
        Serial.print(" 0x"); Serial.print(packet[i], HEX);
#endif
        sum += packet[i];
    }
#ifdef FINGERPRINT_DEBUG
    //Serial.print("Checksum = 0x"); Serial.println(sum);
    Serial.print(" 0x"); Serial.print((uint8_t)(sum>>8), HEX);
    Serial.print(" 0x"); Serial.println((uint8_t)(sum), HEX);
#endif
#if ARDUINO >= 100
    mySerial->write((uint8_t)(sum>>8));
    mySerial->write((uint8_t)sum);
#else
    mySerial->print((uint8_t)(sum>>8), BYTE);
    mySerial->print((uint8_t)sum, BYTE);
#endif
}

void Adafruit_Fingerprint::sendFormattedTemplatePackages(uint8_t bytePacket) {
#if ARDUINO >= 100
      mySerial->write(bytePacket);
#else
   mySerial->print(packet[i], BYTE);
#endif
}



uint8_t Adafruit_Fingerprint::getReply(uint8_t packet[], uint16_t timeout) {
    uint8_t reply[20], idx;
    uint16_t timer=0;

    idx = 0;
#ifdef FINGERPRINT_DEBUG
    Serial.print("<--- ");
#endif
    while (true) {
        while (!mySerial->available()) {
            delay(1);
            timer++;
            if (timer >= timeout) return FINGERPRINT_TIMEOUT;
        }
        // something to read!
        reply[idx] = mySerial->read();
#ifdef FINGERPRINT_DEBUG
        Serial.print(" 0x"); Serial.print(reply[idx], HEX);
#endif
        if ((idx == 0) && (reply[0] != (FINGERPRINT_STARTCODE >> 8)))
            continue;
        idx++;

        // check packet!
        if (idx >= 9) {
            if ((reply[0] != (FINGERPRINT_STARTCODE >> 8)) ||
                    (reply[1] != (FINGERPRINT_STARTCODE & 0xFF)))
                return FINGERPRINT_BADPACKET;
            uint8_t packettype = reply[6];
            //Serial.print("Packet type"); Serial.println(packettype);
            uint16_t len = reply[7];
            len <<= 8;
            len |= reply[8];
            len -= 2;
            //Serial.print("Packet len"); Serial.println(len);
            if (idx <= (len+10)) continue;
            packet[0] = packettype;
            for (uint8_t i=0; i<len; i++) {
                packet[1+i] = reply[9+i];
            }
#ifdef FINGERPRINT_DEBUG
            Serial.println();
#endif
            return len;
        }
    }
}

enrolling.c

#ifndef ADAFRUIT_FINGERPRINT_H
#define ADAFRUIT_FINGERPRINT_H



#include "Arduino.h"
#if defined(__AVR__) || defined(ESP8266)
  #include <SoftwareSerial.h>
#elif defined(FREEDOM_E300_HIFIVE1)
  #include <SoftwareSerial32.h>
  #define SoftwareSerial SoftwareSerial32
#endif

#define FINGERPRINT_OK 0x00
#define FINGERPRINT_PACKETRECIEVEERR 0x01
#define FINGERPRINT_NOFINGER 0x02
#define FINGERPRINT_IMAGEFAIL 0x03
#define FINGERPRINT_IMAGEMESS 0x06
#define FINGERPRINT_FEATUREFAIL 0x07
#define FINGERPRINT_NOMATCH 0x08
#define FINGERPRINT_NOTFOUND 0x09
#define FINGERPRINT_ENROLLMISMATCH 0x0A
#define FINGERPRINT_BADLOCATION 0x0B
#define FINGERPRINT_DBRANGEFAIL 0x0C
#define FINGERPRINT_UPLOADFEATUREFAIL 0x0D
#define FINGERPRINT_PACKETRESPONSEFAIL 0x0E
#define FINGERPRINT_UPLOADFAIL 0x0F
#define FINGERPRINT_DELETEFAIL 0x10
#define FINGERPRINT_DBCLEARFAIL 0x11
#define FINGERPRINT_PASSFAIL 0x13
#define FINGERPRINT_INVALIDIMAGE 0x15
#define FINGERPRINT_FLASHERR 0x18
#define FINGERPRINT_INVALIDREG 0x1A
#define FINGERPRINT_ADDRCODE 0x20
#define FINGERPRINT_PASSVERIFY 0x21
#define FINGERPRINT_MATCH 0x03

#define FINGERPRINT_STARTCODE 0xEF01

#define FINGERPRINT_COMMANDPACKET 0x1
#define FINGERPRINT_DATAPACKET 0x2
#define FINGERPRINT_ACKPACKET 0x7
#define FINGERPRINT_ENDDATAPACKET 0x8

#define FINGERPRINT_TIMEOUT 0xFF
#define FINGERPRINT_BADPACKET 0xFE

#define FINGERPRINT_GETIMAGE 0x01
#define FINGERPRINT_IMAGE2TZ 0x02
#define FINGERPRINT_REGMODEL 0x05
#define FINGERPRINT_STORE 0x06
#define FINGERPRINT_LOAD 0x07
#define FINGERPRINT_UPLOAD 0x08
#define FINGERPRINT_DELETE 0x0C
#define FINGERPRINT_EMPTY 0x0D
#define FINGERPRINT_SETPASSWORD 0x12
#define FINGERPRINT_VERIFYPASSWORD 0x13
#define FINGERPRINT_HISPEEDSEARCH 0x1B
#define FINGERPRINT_TEMPLATECOUNT 0x1D
#define FINGERPRINT_DOWNLOAD 0x09

//#define FINGERPRINT_DEBUG

#define DEFAULTTIMEOUT 1000  ///< UART reading timeout in milliseconds

///! Helper class to craft UART packets
struct Adafruit_Fingerprint_Packet {



  Adafruit_Fingerprint_Packet(uint8_t type, uint16_t length, uint8_t * data) {
    this->start_code = FINGERPRINT_STARTCODE;
    this->type = type;
    this->length = length;
    address[0] = 0xFF; address[1] = 0xFF;
    address[2] = 0xFF; address[3] = 0xFF;
    if(length<64)
      memcpy(this->data, data, length);
    else
      memcpy(this->data, data, 64);
  }
  uint16_t start_code;      ///< "Wakeup" code for packet detection
  uint8_t address[4];       ///< 32-bit Fingerprint sensor address
  uint8_t type;             ///< Type of packet
  uint16_t length;          ///< Length of packet
  uint8_t data[64];         ///< The raw buffer for packet payload
};

///! Helper class to communicate with and keep state for fingerprint sensors
class Adafruit_Fingerprint {
 public:
#if defined(__AVR__) || defined(ESP8266) || defined(FREEDOM_E300_HIFIVE1)
  Adafruit_Fingerprint(SoftwareSerial *ss, uint32_t password = 0x0);
#endif
  Adafruit_Fingerprint(HardwareSerial *hs, uint32_t password = 0x0);

  void begin(uint32_t baud);

  boolean verifyPassword(void);
  uint8_t getImage(void);
  uint8_t image2Tz(uint8_t slot = 1);
  uint8_t createModel(void);
  uint8_t setModel(void);
  uint8_t getMatch(void);
  uint8_t emptyDatabase(void);
  uint8_t storeModel(uint16_t id);
  uint8_t loadModel(uint16_t id);
  uint8_t getModel(void);
  uint8_t deleteModel(uint16_t id);
  uint8_t fingerFastSearch(void);
  uint8_t getTemplateCount(void);
  uint8_t setPassword(uint32_t password);
  void writeStructuredPacket(const Adafruit_Fingerprint_Packet & p);
  void sendTemplate(uint8_t bytePacket);
  uint8_t getStructuredPacket(Adafruit_Fingerprint_Packet * p, uint16_t timeout=DEFAULTTIMEOUT);
  void writePacket(uint32_t addr, uint8_t packettype, uint16_t len, uint8_t *packet);
  void Adafruit_Fingerprint::sendFormattedTemplatePackages(uint8_t bytePacket);
  uint8_t getReply(uint8_t packet[], uint16_t timeout=DEFAULTTIMEOUT);

  /// The matching location that is set by fingerFastSearch()
  uint16_t fingerID;
  /// The confidence of the fingerFastSearch() match, higher numbers are more confidents
  uint16_t confidence;
  /// The number of stored templates in the sensor, set by getTemplateCount()
  uint16_t templateCount;

 private:
  uint8_t checkPassword(void);
  uint32_t thePassword;
  uint32_t theAddress;
    uint8_t recvPacket[20];

  Stream *mySerial;
#if defined(__AVR__) || defined(ESP8266) || defined(FREEDOM_E300_HIFIVE1)
  SoftwareSerial *swSerial;
#endif
  HardwareSerial *hwSerial;
};

#endif

Checking.cpp

#include <Adafruit_Fingerprint.h>
#include <SD.h>
#include <SPI.h>
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)

File dataFile;

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial1);

void readfile();
void initial_setup();
void printHex(int num, int precision);
uint8_t downloadFingerprintTemplate(uint16_t id);
uint8_t readnum();
uint8_t getFingerprintEnroll();
boolean SaveTemplate(Adafruit_Fingerprint &fingerSesor, uint16_t id);

uint8_t id = 1;
char esc;
uint8_t templateBuffer[534];

void setup()  
{
  initial_setup();
  esc = 'n';
}
void initial_setup(){
  while(!Serial);
  Serial.begin(9600);
  Serial.println("Scan the fingerprint and save to external source :)))");

  finger.begin(57600);

  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1){
      delay(1);
    }
  }
  again:
  Serial.println("Initialization SD card");

  if (!(SD.begin(4))){
    Serial.println("initialization failed");
    goto again;
  }
  Serial.println("initialization done");
  if (SD.exists("data.txt")) {
    SD.remove("data.txt");
  }
  else
  {
    dataFile = SD.open("data.txt",FILE_WRITE);
  }

}


void loop()
{
  Serial.println("Ready to enroll a fingerprint!");
  Serial.print("USER_ID: ");Serial.println(id);

  Serial.print("Enrolling ID: ");Serial.println(id);

  while (!getFingerprintEnroll() );
  id++;
  while(1){
    Serial.println("Do you want to continue enrolling [y/n]?");
    while(!Serial.available());
    esc = Serial.read();
    if (esc == 'y') {
      break;
    }else if (esc == 'n') {
      Serial.println("Stop enrolling");
      dataFile.close();
      readfile();
      while(1);
    }else{
      Serial.println("invalid option");
    }
  }
}

void readfile(){
  dataFile = SD.open("data.txt");
  if (dataFile) {
    Serial.println("Read datafile content:");

    // read from the file until there's nothing else in it:
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    // close the file:
    dataFile.close();
  }else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

uint8_t readnum(){
  uint8_t num = 0;
  while(num == 0){
    while(!Serial.available());
      num =  Serial.parseInt();
      Serial.println(num);
  }
  return num;
}


uint8_t getFingerprintEnroll() {

  int p = -1;
  Serial.print("Waiting for valid finger to enroll as #"); Serial.println(id);
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println(".");
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      break;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      break;
    default:
      Serial.println("Unknown error");
      break;
    }
  }

  // OK success!

  p = finger.image2Tz(1);
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
      return p;
    default:
      Serial.println("Unknown error");
      //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
      return p;
  }

  Serial.println("Remove finger");
  delay(2000);
  p = 0;
  while (p != FINGERPRINT_NOFINGER) {
    p = finger.getImage();
  }
  Serial.print("ID "); Serial.println(id);
  p = -1;
  Serial.println("Place same finger again");
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.print(".");
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      break;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      break;
    default:
      Serial.println("Unknown error");
      break;
    }
  }

  // OK success!

  p = finger.image2Tz(2);
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
      return p;
    default:
      Serial.println("Unknown error");
      //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
      return p;
  }

  // OK converted!
  Serial.print("Creating model for #");  Serial.println(id);

  p = finger.createModel();
  if (p == FINGERPRINT_OK) {
    Serial.println("Prints matched!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
    return p;
  } else if (p == FINGERPRINT_ENROLLMISMATCH) {
    Serial.println("Fingerprints did not match");
    //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
    return p;
  } else {
    Serial.println("Unknown error");
    //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
    return p;
  }   


  Serial.print("ID "); Serial.println(id);
  p = finger.storeModel(id);
  if (p == FINGERPRINT_OK) {
    Serial.println("Stored!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
    return p;
  } else if (p == FINGERPRINT_BADLOCATION) {
    Serial.println("Could not store in that location");
    //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
      return p;
  } else if (p == FINGERPRINT_FLASHERR) {
    Serial.println("Error writing to flash");
    //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
    return p;
  } else {
    Serial.println("Unknown error");
    //Additional feature
      Serial.print("Repeat id #");Serial.println(id);
      id--;
    return p;
  }   

  SaveTemplate(finger,id);


}

boolean SaveTemplate(Adafruit_Fingerprint &fingerSesor, uint16_t id)
{
   uint8_t p = fingerSesor.loadModel(id);
   switch (p)
   {
   case FINGERPRINT_OK:
      Serial.print("template "); Serial.print(id); Serial.println(" loaded");
      //delay(1000);
      break;
   case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communicatio  n error");
      return false;
   default:
      Serial.print("Unknown error "); Serial.println(p);
      return false;
   }

   // OK success!

   p = fingerSesor.getModel();
   switch (p) {
   case FINGERPRINT_OK:
      Serial.print("template "); Serial.print(id); Serial.println(" transferring");
      break;
   default:
      Serial.print("Unknown error "); Serial.println(p);
      return false;
   }


   memset(templateBuffer, 0xff, 534);  //zero out template buffer
   uint16_t index=0;
   uint32_t starttime = millis();
   uint8_t fingerTemplate[512];
   int uindx = 9, index1 = 0;
   Serial.print("Template for id: ");
    Serial.println(id);
    //dataFile.print('#');
   while ((index < 534) && ((millis() - starttime) < 1000))
   {
      if (Serial1.available())
      {
         templateBuffer[index++] = Serial1.read();
      }
   }
   while (index < 520) {
      while (index < uindx) ++index;
      uindx += 256;
      while (index < uindx) {
          fingerTemplate[index++] = templateBuffer[index];
      }
      uindx += 2;
      while (index < uindx) ++index;
      uindx = index + 9;
    }
    for (int i = 0; i < 512; ++i) {
      dataFile.print(fingerTemplate[i],HEX);
    }
    dataFile.print('\n');
}

我正在使用Mega2560,以太网屏蔽W5500和指纹R205。您应该注意的一些其他功能是“ getMatch,setModel,getReply,sendFormattedTemplatePackages”。以下大多数代码基于Adafruit的示例,即注册,show_fingerprint_template,指纹和https://forums.adafruit.com/viewtopic.php?t=33229&start=195。 谢谢!!!

0 个答案:

没有答案