Go Down

Topic: RFID tag compair problem (Read 1 time) previous topic - next topic


ok so still no joy

here is the code as it stands, maybe im just doing it wrong i dont know

#include <SoftwareSerial.h>

int  val = 0;
char code[10];
int bytesread = 0;

int strcmp(const char *s1, const char *s2)
   unsigned char uc1, uc2;
   /* Move s1 and s2 to the first differing characters
      in each string, or the ends of the strings if they
      are identical.  */
   while (*s1 != '\0' && *s1 == *s2) {
   /* Compare the characters as unsigned char and
      return the difference.  */
   uc1 = (*(unsigned char *) s1);
   uc2 = (*(unsigned char *) s2);
   if (uc1 != uc2)
      return ((uc1 < uc2) ? -1 : (uc1 > uc2));
      return 0;

#define rxPin 8
#define txPin 9
// RFID reader SOUT pin connected to Serial RX pin at 2400bps to pin8

void setup()
 Serial.begin(9600);  // Hardware serial for Monitor 9600bps

 pinMode(2,OUTPUT);       // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
 digitalWrite(2, LOW);    // Activate the RFID reader

void loop()
 SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);

 if((val = RFID.read()) == 10)
 {   // check for header
   bytesread = 0;
   {  // read 10 digit code
     val = RFID.read();

     if((val == 10)||(val == 13))
     {  // if header or stop bytes before the 10 digit reading
       break;                       // stop reading
     code[bytesread] = val;         // add the digit        
     bytesread++;                   // ready to read next digit  

   if(bytesread == 10)
   {  // if 10 digit read is complete
     Serial.print("TAG code is: ");   // possibly a good TAG
     Serial.println(code);            // print the TAG code

// ADDED / Check for Particular tags
     if (strcmp(code, "0415E9B02D") == 0) {
       Serial.println("Round Black WORLD TAG");

   bytesread = 0;
   delay(1000);                       // wait for a second

Adrian McEwen

Okay, I think this will be the last problem...

Strings in C and C++ are NUL-terminated, which means there's a '\0' character on the end of them.  Although you can't see it, the "0415E9B02D" has a '\0' on the end, and your "code" array doesn't.  If you stick
Code: [Select]
code[bytesread] = '\0';
before the if statement then you should be good.  (Actually, you should really do that before the "Serial.println(code);" line, so that it doesn't end up printing any garbage that's after the array)


Dec 10, 2008, 08:28 pm Last Edit: Dec 10, 2008, 08:31 pm by mikalhart Reason: 1

Are you getting the message "Tag Code is xxxxxxxxxx"?  If not, then the problem is not with strcmp, because your code is never reaching there.

There is no need to provide your own strcmp.  The Arduino library already provides one that is faster and smaller.  This is just wasting space in your binary.


EDIT: Adrian has nailed it!  (But you still don't need your own version of strcmp.)
EDIT2: Make sure you increase the size of your "code" array to 11 (10+NUL byte), otherwise Ardrian's addition will overrun the buffer.


Adrian McEwen

Oops.  Thanks for the comment Mikal - I hadn't realised strcmp was provided in Arduino.  Indeed you should use that, but you'll still need to NUL-terminate the array first.  Which I've just realised also means that you'll need to make your array 11 chars rather than 10, otherwise you'll overwrite part of 'bytesRead'.


Dec 10, 2008, 08:39 pm Last Edit: Dec 10, 2008, 08:44 pm by dbunting Reason: 1
im sorry, i excluded that.

yes i am getting a tag code output.

if i use code[bytesread] = '\0' wont it overwrite the last byte read?
shouldnt i use code[bytesread+1] = '\0' ? <-- ignore that


you guys are fantastic.

now to read up on placing values into EEPROM and doing so conditionally

Go Up