Go Down

Topic: Arduino RFID Module RDM630 Code (Read 1 time) previous topic - next topic

ilovetoflyfpv

Hi Gang

I'm thinking of purchasing the following RFID Module;

http://www.ebay.com.au/itm/New-1pc-125-KHZ-EM4100-RFID-Card-Read-Module-RDM630-UART-Compatible-Arduino-/190769771688?pt=LH_DefaultDomain_15&hash=item2c6ac3b4a8&_uhb=1#ht_4786wt_978

I found the following example code;

Code: [Select]
/*------------------------------------------------------------------
This is a sample code for RDM630 RFID reader by Spekel(Spekel.se)
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
http://creativecommons.org/licenses/by-nc-sa/3.0/
-------------------------------------------------------------------*/
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
char code[20];
int val = 0;
int bytesread = 0;
//------------------------------------
//create a Serial object RFID
SoftwareSerial RFID= SoftwareSerial(rxPin, txPin);

void setup()
{
 Serial.begin(9600);
 Serial.println("Serial Ready");
 RFID.begin(9600);
 Serial.println("RFID Ready");
 pinMode(rxPin, INPUT);
 pinMode(txPin, OUTPUT);
}
void loop()
{
 val = 0;
 bytesread = 0;

 while(bytesread < 12)
 {
   // read 12 digit code
   val = RFID.read();
   if(val == 3)
   { // if header or stop bytes before the 10 digit reading
     break; // stop reading
   }

   if(val != 2)
   {
     code[bytesread] = val; // add the digit
     bytesread++; // ready to read next digit
     code[bytesread] = '\0'; // add the NULL
   }
 }

 if(bytesread >= 12)
 { // if 12 digit read is complete
   Serial.print("Tag: [");
   for(int i=0; code[i]!='\0' ; i++)
   {
     Serial.print(code[i]);
   }
   Serial.println("]"); //print the whole 13 bytes
 }
}


I was wondering if someone could explain what the following two lines do;

Code: [Select]
code[bytesread] = '\0'; // add the NULL

and

Code: [Select]
for(int i=0; code[i]!='\0' ; i++)

specifically;

Quote
'\0'


Any help would be greatly appreciated.

Cheers

Jase :)


tack

The sketch is commented. It clearly tells you that /0 is adding the NULL terminator to the end of the bytes read.

All strings should be terminated with a NULL. If you use a chat array to read in bytes then you should terminate the string with a NULL. Therefore if you want to hold 20 characters, your char array would need to be 21 to hold the extra NULL to terminate the string.

The other bits just set a loop up to read each byte and add to the array, or read valid characters from the array until it encounters the NULL (/0) signifying the end of the string.

ilovetoflyfpv

Hi tack

Thanks for the reply. I'm starting to get it. Much appreciated.

Cheers

Jase :)

Go Up