Pages: [1]   Go Down
Author Topic: Arduino RFID Module RDM630 Code  (Read 1157 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*------------------------------------------------------------------
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:
code[bytesread] = '\0'; // add the NULL

and

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

specifically;

Quote
'\0'

Any help would be greatly appreciated.

Cheers

Jase smiley

Logged

UK
Offline Offline
God Member
*****
Karma: 17
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi tack

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

Cheers

Jase smiley
Logged

Pages: [1]   Go Up
Jump to: