Pages: [1]   Go Down
Author Topic: Easy code question for an RFID reader.....  (Read 738 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have my arduino working fine and outputing a 5 byte code through the serial monitor using the code from...

http://www.arduino.cc/playground/Code/ID12

I would like to write an if statement, that will output for a particular card. Right now upon a card scanned through the com I'm getting "5-byte code: 21 00 72 B7 3F" as I should.

However, I'm looking to add to this code to actually identify cards with a series of if statements. For example, I would like to be able to code something like the following.

if "5-byte code: 21 00 72 B7 3F is read"
 Serial.print("Card 1")
if "5-byte code: 21 00 34 J3 3G is read"
 Searial.print("Card 2")

This way I'm identify cards in the serial monitor instead of getting a string.  I'm sure you get what I'm trying to get at. If anyone can think of a nice way to do this I appreciate any help.

Thanks in advance...
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26621
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sounds like you need to look at something like "memcmp"
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 94
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The string library at:

http://arduino.cc/en/Tutorial/TextString

may also be of use to you.
« Last Edit: March 08, 2010, 04:41:00 pm by programmer » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the prompt responses...

After looking around the forum for a while I think using the strcmp command function should work just fine. I have limited programming knowledge.

Could someone enlighten me on the correct way to go about doing this or why I am getting error: invalid conversion from 'byte*' to 'const char*'

I have only added the define tag and string comparison code.

Code:
char tag1[] = "210072B73F"; //define tag 1

void setup() {
  Serial.begin(9600);                                 // connect to the serial port
}

void loop () {
  byte i = 0;
  byte val = 0;
  byte code[6];
  byte checksum = 0;
  byte bytesread = 0;
  byte tempbyte = 0;

  if(Serial.available() > 0) {
    if((val = Serial.read()) == 2) {                  // check for header
      bytesread = 0;
      while (bytesread < 12) {                        // read 10 digit code + 2 digit checksum
        if( Serial.available() > 0) {
          val = Serial.read();
          if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
            break;                                    // stop reading
          }

          // Do Ascii/Hex conversion:
          if ((val >= '0') && (val <= '9')) {
            val = val - '0';
          } else if ((val >= 'A') && (val <= 'F')) {
            val = 10 + val - 'A';
          }

          // Every two hex-digits, add byte to code:
          if (bytesread & 1 == 1) {
            // make some space for this hex-digit by
            // shifting the previous hex-digit with 4 bits to the left:
            code[bytesread >> 1] = (val | (tempbyte << 4));

            if (bytesread >> 1 != 5) {                // If we're at the checksum byte,
              checksum ^= code[bytesread >> 1];       // Calculate the checksum... (XOR)
            };
          } else {
            tempbyte = val;                           // Store the first hex digit first...
          };

          bytesread++;                                // ready to read next digit
        }
      }

      // Output to Serial:

      
      if(bytesread == 12) {         // if 12 digit read is complete
        if(strcmp(code, tag1) == 0) //compare tag1 to the tag read
        {
          Serial.print("Tag 1");
        }
      
     // if (bytesread == 12) {                          // if 12 digit read is complete
     //   Serial.print("5-byte code: ");
     //   for (i=0; i<5; i++) {
     //      if (code[i] < 16) Serial.print("0");
     //      Serial.print(code[i], HEX);
     //      Serial.print(" ");
        }
        Serial.println();

        Serial.print("Checksum: ");
        Serial.print(code[5], HEX);
        Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
        Serial.println();
      }

      bytesread = 0;
    }
  }
}

Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13072
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
After looking around the forum for a while I think using the strcmp command function should work just fine. I have limited programming knowledge.
Nope.  Won't work.  There are "embedded nulls" in the ID.  strcmp will stop comparing when it reaches them.

AWOL's advice is sound.  memcmp is what you need to use.
Logged

Pages: [1]   Go Up
Jump to: