Go Down

Topic: TX command code & RX respond code of module via serial com (Read 17077 times) previous topic - next topic

mr_hacker90

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.

Code: [Select]

#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() {
}

PaulS

How many characters of the reply are you reading? How long are you waiting for the reply?
The art of getting good answers lies in asking good questions.

quertyn

#2
Jan 15, 2013, 11:54 am Last Edit: Jan 15, 2013, 11:58 am by quertyn Reason: 1
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:

Code: [Select]
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);
}

PaulS

Code: [Select]
  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.
The art of getting good answers lies in asking good questions.

mr_hacker90

Code: [Select]

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.

Code: [Select]

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

PaulS

Quote
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.

Code: [Select]
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
The art of getting good answers lies in asking good questions.

PaulS

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

You've removed the fingerprint reader?

Quote
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.

The art of getting good answers lies in asking good questions.

mr_hacker90

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.

mr_hacker90

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.

Code: [Select]

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");
        }

PaulS

Hello? Are you listening?
Code: [Select]
for(byte n=0; n<7; n++)
will iterate SEVEN times, not 6.

Code: [Select]
    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,
Code: [Select]
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.
The art of getting good answers lies in asking good questions.

mr_hacker90

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.

Code: [Select]

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.

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

mr_hacker90


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()

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

mr_hacker90


Quote
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.

Go Up