• And here is the code I have so far.
    Please note it was late, not necessarly optimized and the way the onData is done might change later :)
    Also I will add an 'err' as first parameter for each callback in case,
    and adapt some part of code.
    Once Enroll is done, I will start transforming and prepare for module

    var C = {
      FINGERPRINT_OK                 : 0x00,
      FINGERPRINT_PACKETRECIEVEERR   : 0x01,
      FINGERPRINT_NOFINGER           : 0x02,
      FINGERPRINT_IMAGEFAIL          : 0x03,
      FINGERPRINT_IMAGEMESS          : 0x06,
      FINGERPRINT_FEATUREFAIL        : 0x07,
      FINGERPRINT_NOMATCH            : 0x08,
      FINGERPRINT_NOTFOUND           : 0x09,
      FINGERPRINT_ENROLLMISMATCH     : 0x0A,
      FINGERPRINT_BADLOCATION        : 0x0B,
      FINGERPRINT_DBRANGEFAIL        : 0x0C,
      FINGERPRINT_UPLOADFEATUREFAIL  : 0x0D,
      FINGERPRINT_PACKETRESPONSEFAIL : 0x0E,
      FINGERPRINT_UPLOADFAIL         : 0x0F,
      FINGERPRINT_DELETEFAIL         : 0x10,
      FINGERPRINT_DBCLEARFAIL        : 0x11,
      FINGERPRINT_PASSFAIL           : 0x13,
      FINGERPRINT_INVALIDIMAGE       : 0x15,
      FINGERPRINT_FLASHERR           : 0x18,
      FINGERPRINT_INVALIDREG         : 0x1A,
      FINGERPRINT_ADDRCODE           : 0x20,
      FINGERPRINT_PASSVERIFY         : 0x21,
    
      FINGERPRINT_STARTCODE          : 0xEF01,
    
      FINGERPRINT_COMMANDPACKET      : 0x1,
      FINGERPRINT_DATAPACKET         : 0x2,
      FINGERPRINT_ACKPACKET          : 0x7,
      FINGERPRINT_ENDDATAPACKET      : 0x8,
    
      FINGERPRINT_TIMEOUT            : 0xFF,
      FINGERPRINT_BADPACKET          : 0xFE,
    
      FINGERPRINT_GETIMAGE           : 0x01,
      FINGERPRINT_IMAGE2TZ           : 0x02,
      FINGERPRINT_REGMODEL           : 0x05,
      FINGERPRINT_STORE              : 0x06,
      FINGERPRINT_EMPTY              : 0x0D,
      FINGERPRINT_VERIFYPASSWORD     : 0x13,
      FINGERPRINT_HISPEEDSEARCH      : 0x1B,
      FINGERPRINT_TEMPLATECOUNT      : 0x1D,
    
      DEFAULTTIMEOUT : 5000 // milliseconds
    };
    
    Serial4.setup(57600);
    
    var thePassword = 0;
    var theAddress = 0xFFFFFFFF;
    
    function writePacket(addr, packetType, packet) {
      var len = packet.length+2;
      Serial4.write([
        (C.FINGERPRINT_STARTCODE >> 8) & 0xFF,
        C.FINGERPRINT_STARTCODE & 0xFF,
        (addr >> 24) & 0xFF,
        (addr >> 16) & 0xFF,
        (addr >> 8) & 0xFF,
        addr & 0xFF,
        packetType,
        (len >> 8) & 0xFF,
        len &0xFF
      ]);
    
      var sum = (len>>8) + (len&0xFF) + packetType;
      Serial4.write(packet);
      for (var i in packet) sum += packet[i];
      Serial4.write([(sum>>8)&0xFF, sum&0xFF]);
    }
    
    function getReply(packet, callback) {
    
      var receivedData = [];// new Uint8Array(20);
    
      Serial4.onData(function (e) {
        receivedData.push(e.data.charCodeAt(0));
    
        if ( (idx === 0) && (receivedData[0] != ( (C.FINGERPRINT_STARTCODE >> 8) & 0xFF) ))
          return;
    
        if (receivedData.length >= 9) {
    
          if (( receivedData[0] != (C.FINGERPRINT_STARTCODE >> 8)) ||
              ( receivedData[1] != (C.FINGERPRINT_STARTCODE & 0xFF)))
            console.log('bad packet');
    
          var packettype = receivedData[6];
          var len = receivedData[7];
    
          len = len << 8 & 0xFF;
          len = len | (receivedData[8] & 0xFF);
          len -= 2;
    
          if (receivedData.length <= (len+10))
            return;
    
          packet[0] = packettype;
    
          for (var i=0; i<len; i++) {
            packet[1+i] = receivedData[9+i];
          }
    
          console.log('end');  
          callback(len, packet);
        }
      });
    }
    
    function verifyPassword(callback) {
      var packet = [
        C.FINGERPRINT_VERIFYPASSWORD,
        (thePassword >> 24) & 0xFF,
        (thePassword >> 16) & 0xFF,
        (thePassword >> 8) & 0xFF,
        thePassword & 0xFF
      ];
    
      writePacket(theAddress, C.FINGERPRINT_COMMANDPACKET, packet);
    
      getReply(packet, function(len, packet) {
        if ((len == 1) && (packet[0] == C.FINGERPRINT_ACKPACKET) && (packet[1] == C.FINGERPRINT_OK))
          callback(true);
        else
          callback(false);
      });
    }
    
    function getImage(callback) {
      var packet = [C.FINGERPRINT_GETIMAGE];
    
      writePacket(theAddress, C.FINGERPRINT_COMMANDPACKET, packet);
    
      getReply(packet, function(len, packet) {
        if ((len != 1) && (packet[0] != C.FINGERPRINT_ACKPACKET))
          callback(-1);
        else
          callback(packet[1]);
      });
    }
    
    function image2Tz(slot, callback) {
      var packet = [C.FINGERPRINT_IMAGE2TZ, (slot & 0xFF)];
    
      writePacket(theAddress, C.FINGERPRINT_COMMANDPACKET, packet);
    
      getReply(packet, function(len, packet) {
        if ((len != 1) && (packet[0] != C.FINGERPRINT_ACKPACKET))
          callback(-1);
        else
          callback(packet[1]);
      });
    }
    
    function fingerFastSearch(callback) {
      var fingerID = 0xFFFF;
      var confidence = 0xFFFF;
      var packet = [C.FINGERPRINT_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x00, 0xA3];
    
      writePacket(theAddress, C.FINGERPRINT_COMMANDPACKET, packet);
    
      getReply(packet, function(len, packet) {
    
        if ((len != 1) && (packet[0] != C.FINGERPRINT_ACKPACKET)) {
          callback(-1);
          return;
        }
    
        fingerID = packet[2];
        fingerID <<= 8;
        fingerID |= packet[3];
    
        confidence = packet[4];
        confidence <<= 8;
        confidence |= packet[5];
    
        callback(packet[1], fingerID, confidence);
      });
    }
    
    function getFingerprintIDez(callback) {
      getImage(function(f) {
        if (f != C.FINGERPRINT_OK) {
          callback(-1);
          return;
        }
        image2Tz(null, function(g) {
          if (g != C.FINGERPRINT_OK) {
            callback(-1);
            return;
          }
          fingerFastSearch(function(res, fingerID, confidence) {
            if (res != C.FINGERPRINT_OK) {
              callback(-1);
              return;
            }
    
            callback(fingerID);
    
          });
        });
      });
    }
    
    
    //setup
    verifyPassword(function(e) {
      if(e) {
        getFingerprintIDez(function(e) {
          console.log(e);
        });
      }
    });
    
About

Avatar for ChrisB @ChrisB started