Modified Class

Fork of GT511C3 by Eugene Gonzalez

Revision:
2:8fa6daf39d91
Parent:
1:aa0bd9bd1c56
Child:
3:a3500c97e7d5
diff -r aa0bd9bd1c56 -r 8fa6daf39d91 GT511C3.cpp
--- a/GT511C3.cpp	Tue Dec 01 18:13:20 2015 +0000
+++ b/GT511C3.cpp	Tue Dec 01 20:48:44 2015 +0000
@@ -134,24 +134,27 @@
 int GT511C3::RecvData(unsigned char *data,unsigned long size)
 {
     const unsigned char fixedbuf[4] = { 0x5A,0xA5,0x01,0x00 };
-    unsigned short sum = 0;
+    unsigned short sum = 0x5A + 0xA5 + 0x01 + 0x00;
     int i;
 
+    
+    for(i = 0;i < 4;i++){
+        while(!readable());
+        if (getc() != fixedbuf[i])
+            return -1;
+    }    
+    
     for(i = 0;i < size;i++){
         while(!readable());
-        *(data + i) = getc();
-        if(i < (size-2)){
-            sum += *(data + i);
-        }
-        if(i < 4){
-            if(*(data + i) != fixedbuf[i]){
-                return -1;
-            }
-        }
+        data[i] = getc();
+        sum += data[i];
     }
-    if(*(data + size - 2) != (sum & 0xff))
+    
+    while(!readable());
+    if(getc() != (sum & 0xff))
         return -2;
-    if(*(data + size - 1) != ((sum >> 8) & 0xff))
+    while(!readable());
+    if(getc() != ((sum >> 8) & 0xff))
         return -2;
     return 0;
 }
@@ -184,7 +187,7 @@
 {
     unsigned long Parameter = 1;
     unsigned short Response = 0;
-    unsigned char buf[4+sizeof(FirmwareVersion)+sizeof(IsoAreaMaxSize)+sizeof(DeviceSerialNumber)+2];
+    unsigned char buf[sizeof(FirmwareVersion)+sizeof(IsoAreaMaxSize)+sizeof(DeviceSerialNumber)];
     int sts = 0;
 
     if((sts = Init()) != 0)
@@ -195,9 +198,9 @@
         return -1;
     }
     if((sts = RecvData(buf,sizeof(buf))) == 0){
-        memcpy(&FirmwareVersion,&buf[4+0],sizeof(FirmwareVersion));
-        memcpy(&IsoAreaMaxSize,&buf[4+sizeof(FirmwareVersion)],sizeof(IsoAreaMaxSize));
-        memcpy(DeviceSerialNumber,&buf[4+sizeof(FirmwareVersion)+sizeof(IsoAreaMaxSize)],sizeof(DeviceSerialNumber));
+        memcpy(&FirmwareVersion,&buf[0],sizeof(FirmwareVersion));
+        memcpy(&IsoAreaMaxSize,&buf[sizeof(FirmwareVersion)],sizeof(IsoAreaMaxSize));
+        memcpy(DeviceSerialNumber,&buf[sizeof(FirmwareVersion)+sizeof(IsoAreaMaxSize)],sizeof(DeviceSerialNumber));
     }
     return sts;
 }
@@ -339,7 +342,7 @@
             return -9999;
         if(Enroll_N(3) != 0)
             continue;
-        WaitPress(0);
+        //WaitPress(0); // This was eating a byte from the DataPacket if you set the ID=-1
 
         if((sts = (*progress)(100,"Enroll OK\n")) != 0)
             return -9999;
@@ -363,11 +366,11 @@
 
 int GT511C3::SetTemplate(int ID, unsigned char *data, unsigned long size)
 {
-    unsigned long Parameter = 0xFFFF0000 | ID;
+    unsigned long Parameter = ID;
     unsigned short Response = 0;
     int sts = 0;
 
-    sts = SendRecv(CMD_DeleteID,&Parameter,&Response);
+    sts = SendRecv(CMD_SetTemplate,&Parameter,&Response);
     
     if ((sts != 0) || (Response != CMD_Ack))
         return -1;
@@ -375,12 +378,12 @@
     sts = SendData(data, size);
     
     if (sts != 0)
-        return -1;
+        return -2;
         
     sts = RecvResponse(&Parameter, &Response);
     
     if ((sts != 0) || (Response != CMD_Ack))
-        return -1;
+        return -3;
     
     return 0;
 }