TX command code & RX respond code of module via serial com

hi everyone. i have a question here. i need to sent 8 bytes command code to a fingerprint reader. and then, read the 6 bytes respond code of the reader. sending & reading the code is via serial (RX & TX). i already made the code. but, it is not working. i just want to ask your opinion what's wrong with my code here. seems that the TX & RX LED even not light up when i start the program. thank you.

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,4,5,6,7);

byte addFingerPrint[] = {0x4D, 0x58, 0x10, 0x03, 0x40, 0x00, 0x00, 0xF8};
byte respond = 0;

void setup() {
  // initialize serial:
  Serial.begin(57600);
  
  // set up the LCD's number of columns and rows: 
  lcd.begin(8, 2);

  displayLCD();
  
  addFinger();
  
  correctRespond();
}

void displayLCD(){
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 0);
  // Print a message to the LCD.
  lcd.print("Reader");
}

void addFinger(){
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("Add");

  // print the string when a newline arrives:
  Serial.write(addFingerPrint, sizeof(addFingerPrint));
  
  delay(1000);
}

void correctRespond() {
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("Cor");
  
  if (Serial.available()) {
  // read the most recent byte
  respond = Serial.read();
  }
  
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // print the correct respond code of the reader (0x4D, 0x58, 0x30, 0x01, 0x01, 0xD7)
  lcd.print("Cor=");
  lcd.print(respond, HEX);
  
  delay(1000);
}

void loop() {
}

How many characters of the reply are you reading? How long are you waiting for the reply?

Hi,

First thing in your code, in correctRespond function you should poll until the fingerprint reader sent back a response, just in case correctRespond is called too early, before the fingerprint reader had enough time to respond. You can do something like this:

void correctRespond() {
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("Cor");
  
  while(Serial.available() == 0){
    delay(10);
  }

  // read the most recent byte
  respond = Serial.read();

  
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // print the correct respond code of the reader (0x4D, 0x58, 0x30, 0x01, 0x01, 0xD7)
  lcd.print("Cor=");
  lcd.print(respond, HEX);
  
  delay(1000);
}
  while(Serial.available() == 0){
    delay(10);
  }

Can you describe the exact purpose for the delay()? Does it matter whether the while loop iterates 10 times in 100 milliseconds, or 10,000,000?

If you know (or think/hope) that the response is going to be 6 bytes, wait for 6,

Then, of course, you still need to read all 6 of them.

respond = Serial.read();

this command i believe just read a byte / char. if i want to put a loop, which read 6 bytes. how i'm gonna make it. of course it need a for loop. but, how i gonna integrate the counter with the serial read.

for (int n=0; n<7; n++) {
{

this command i believe just read a byte / char. if i want to put a loop, which read 6 bytes. how i'm gonna make it. of course it need a for loop. but, how i gonna integrate the counter with the serial read.

That for loop will iterate while n = 0, 1, 2, 3, 4, 5, and 6. That's 7 iterations, not 6.

byte reply[6];
for(byte n=0; n<6; n++)
{
   while(Serial.available() == 0) {}; // Wait for a byte
   reply[n] = Serial.read();
}
// Now, reply contains all 6 bytes

understand. now i'm using x-ctu serial terminal.

You've removed the fingerprint reader?

but the x-ctu hyperterminal shows FF

Also known as -1 (no data).

X-CTU is fine for talking to XBees. Other than that, I find it useless.

nono.. it's correct. i'm mistaken configure the baud. is shows all the 8 bytes. correctly. i just want to check whether it's write the 8 bytes. this tips is being told by my lecturer. to check the bytes of the TX pin.

the uno transmit 8 bytes. if it managed to receive respond code, how i'm gonna compare it either right or wrong? can i just make it like this? correct respond is 6 bytes while wrong respond is 7 bytes.

for(byte n=0; n<7; n++)
  {
    while(Serial.available() == 0) {}; // Wait for a byte
    respond[n] = Serial.read();
  }
  
  // Now, reply contains all 6 bytes
  
    if (respond== "0x4D + 0x58 + 0x30 + 0x01 + 0x01 + 0xD7"){
        // set the cursor to column 0, line 1
        lcd.setCursor(0, 1);
        // print the correct respond
        lcd.print("Correct Respond");
        }
      
    else if(respond== "0x4D + 0x58 + 0x30 + 0x02 + 0x40 + 0x35 + 0x4C"){
        // set the cursor to column 0, line 1
        lcd.setCursor(0, 1);
        // print the wrong respond
        lcd.print("Wrong Resp");
        }

Hello? Are you listening?

for(byte n=0; n<7; n++)

will iterate SEVEN times, not 6.

    if (respond== "0x4D + 0x58 + 0x30 + 0x01 + 0x01 + 0xD7"){

The address of an array of bytes will NEVER equal the address of a literal string.

You can have another array,

byte correct[] = {0x4D, 0x58, 0x30, 0x01, 0x01, 0xD7};

Then, you can use memcmp() to compare the arrays. But, you can't just make up stuff.

memcmp()? by the way, it is quite confusing. because the correct command respond is 6 bytes but wrong command respond is 7 bytes. that is the reason why i put 7 sir.

byte correctRespond[] = {0x4D, 0x58, 0x30, 0x01, 0x01, 0xD7};
byte wrongrespond[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x35, 0x4C);

the fingerprint reader respond correct or wrong not in same byte length.

by the way, it is quite confusing. because the correct command respond is 6 bytes but wrong command respond is 7 bytes.

Perhaps you need to post a link to the fingerprint reader. It does not make sense to return two different length arrays when the finger print is valid and is not valid.

PaulS:
Perhaps you need to post a link to the fingerprint reader. It does not make sense to return two different length arrays when the finger print is valid and is not valid.

if u think that way, i also think the same. nevermind. but how i'm gonna post the link to the reader. i need to compare lots of byte array. memcmp()

but how i'm gonna post the link to the reader.

Use the 3rd icon on the bottom row. Paste the URL after clicking the icon.

PaulS:

but how i'm gonna post the link to the reader.

Use the 3rd icon on the bottom row. Paste the URL after clicking the icon.

i'm sorry. i don't understand sir.

Where did you get the fingerprint reader you are trying to talk to? We have to know something about it in order to help you.

my fingerprint? this is my fingerprint that i bought. at first i juz thought simple RX & TX. i never thought that it quite difficult to a beginner like me. SN-FPR-SM630. thank you mr pauls.

i've studied a little bit. thank you to u sir. finally understand it a little bit. but, too bad. i don't bring the reader to my house. i left it at my hostel. at least i know the basic function. but, i'm not sure either this is the correct way to compare it. but, i know the basic operation by the way. i just not sure about the

m = memcmp(operationSuccess, operation, sizeof(operationSuccess));

by the way, thank you so much sir. at least from zero. i got half of it on what you told me. i wish i could got all of it. but i know, i need to study more. thank you coz taught me.

byte operationSuccess[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x31, 0x48};
byte operation[7],m;

void operationSuccesful() {
  // Now, reply contains all 7 bytes
  for(byte n=0; n<7; n++)
  {
    while(Serial.available() == 0) {}; // Wait for a byte
    operation[n] = Serial.read();
    
    m = memcmp(operationSuccess, operation, sizeof(operationSuccess));
    
    if (m>0) {
      // set the cursor to column 0, line 1
      lcd.setCursor(0, 0);
      // print the number of seconds since reset:
      lcd.print("Operation Failed");
      }
      
    else if (m<0) {
      // set the cursor to column 0, line 1
      lcd.setCursor(0, 0);
      // print the number of seconds since reset:
      lcd.print("Operation Failed");
      }

    
    else {
      // set the cursor to column 0, line 1
      lcd.setCursor(0, 0);
      // print the number of seconds since reset:
      lcd.print("Operation Success");
      }
  }
}

hello everyone. i currently make try to make a simple project. uno with fingerprint reader. as startup i try to add fingerprint.

at first. i try run the fingerprint reader + uno with X-CTU only. i use the terminal of the X-CTU. i sent the command code of the reader. and the reader reply with correct respond code & success operation code.
as u can see in the picture, the first 8 bytes {0x4D, 0x58, 0x10, 0x03, 0x40, 0x00, 0x00, 0xF8} is the command code for add fingerprint. then the reader reply 6 bytes correct respond code, {0x4D, 0x58, 0x30, 0x01, 0x01, 0xD7}. later the reader reply 7 bytes success operation code, {0x4D, 0x58, 0x30, 0x02, 0x40, 0x31, 0x48}.

then, i try make the program. so that it follow exactly what the reader respond as i tested above. i manage to make the code. by using X-CTU. i key the respond code, the arduino program able to detect the 6 bytes of the correct respond code & 7 bytes of success operation.

however, when i try to combine them both, it doesn't being to be happen. the arduino uno transmit the command code. but, the reader doesn't respond to that code. i believe there is a problem with my code. but, i don't have any idea where's the problem. with all, regards.

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,4,5,6,7);

byte addFingerPrint[] = {0x4D, 0x58, 0x10, 0x03, 0x40, 0x00, 0x00, 0xF8}; // 8 bytes
byte respondCorrect[] = {0x4D, 0x58, 0x30, 0x01, 0x01, 0xD7}; // 6 bytes

byte operationSuccess[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x31, 0x48}; // 7 bytes
byte timeOut[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x33, 0x4A}; // 7 bytes
byte processFailure[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x34, 0x4B}; // 7 bytes
byte parameterError [] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x35, 0x4C}; // 7 bytes
byte respond[6],operation[7],a,b,c,d;

void setup() {
  // initialize serial:
  Serial.begin(57600);
  
  // set up the LCD's number of columns and rows: 
  lcd.begin(8, 2);

  displayLCD();
  
  addFinger();
  
  correctRespond();
  
  operationSuccesful();
}

void displayLCD(){
  // set the cursor to column 0, line 0
  lcd.setCursor(0, 0);
  // Print a message to the LCD.
  lcd.print("Fingerprint Reader");
}

void addFinger(){
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // Print a message to the LCD.
  lcd.print("Add Fingerprint");

  // write the 8bytes array of the command code
  Serial.write(addFingerPrint, sizeof(addFingerPrint));
  //delay(2000);
}

void correctRespond() {
  // Now, reply contains all 6 bytes
  for(byte n=0; n<6; n++)
  {
    while(Serial.available() == 0) {}; // Wait for a byte
    respond[n] = Serial.read();
    
    a = memcmp(respondCorrect, respond, sizeof(respondCorrect));

    if (a>0) {
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);
      // Print a message to the LCD.
      lcd.print("Wrong   ");
      // set the cursor to column 0, line 1
      lcd.setCursor(0, 1);
      // Print a message to the LCD.
      lcd.print("Respond ");
      }
      
    else if (a<0) {
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);
      // Print a message to the LCD.
      lcd.print("Wrong   ");
      // set the cursor to column 0, line 1
      lcd.setCursor(0, 1);
      // Print a message to the LCD.
      lcd.print("Respond ");
      }
    
    else {
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);
      // Print a message to the LCD.
      lcd.print("Correct ");
      // set the cursor to column 0, line 1
      lcd.setCursor(0, 1);
      // Print a message to the LCD.
      lcd.print("Respond ");
      }
  }
  delay(2000);
}

void operationSuccesful() {
  // Now, reply contains all 7 bytes
  for(byte n=0; n<7; n++)
  {
    while(Serial.available() == 0) {}; // Wait for a byte
    operation[n] = Serial.read();
    
    b = memcmp(operationSuccess, operation, sizeof(operationSuccess));
    
    if (b==0) {
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);
      // Print a message to the LCD.
      lcd.print("Operation");
      // set the cursor to column 0, line 1
      lcd.setCursor(0, 1);
      // Print a message to the LCD.
      lcd.print("Success ");
      }

    else {
      c = memcmp(processFailure, operation, sizeof(processFailure));
      
      if (c==0) {
        // set the cursor to column 0, line 0
        lcd.setCursor(0, 0);
        // Print a message to the LCD.
        lcd.print("Process ");
        // set the cursor to column 0, line 1
        lcd.setCursor(0, 1);
        // Print a message to the LCD.
        lcd.print("Failure ");
        }
      
      else  {
        d = memcmp(parameterError, operation, sizeof(parameterError));
        
        if (d==0) {
          // set the cursor to column 0, line 0
          lcd.setCursor(0, 0);
          // Print a message to the LCD.
          lcd.print("Parameter");
          // set the cursor to column 0, line 1
          lcd.setCursor(0, 1);
          // Print a message to the LCD.
          lcd.print("Error   ");
          }
        
        else {
          // set the cursor to column 0, line 0
          lcd.setCursor(0, 0);
          // Print a message to the LCD.
          lcd.print("Time    ");
          // set the cursor to column 0, line 1
          lcd.setCursor(0, 1);
          // Print a message to the LCD.
          lcd.print("Out     ");
        }
      }
    }
  } 
  delay(2000);
}

void loop() {
}

Why are you starting a new thread on the same topic?