Using the RDM630 RFID Tag Reader and NewSoftwareSerial library

Hello,
Here is the code (pro publico bono) which takes into account the official frame format from the sensor. We are synchronizing with the frame. We do not rely on the fact that every 13 characters there should be a new card number. Enjoy. Code should compile on Arduino Mega, for other Arduinos you have to replace Serial1 with instance of SoftwareSerial class (2 places in the code below). It works both with software and hardware UART.

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);  // you can use softwareserial instead
}
unsigned char card[20];

// let's use the data frame format from RDM630 specification to avoid mismatch
// [0x02] [ 10 ASCII CHARACTERS ] [CHECKSUM] [0x03]

void loop()
{
  static unsigned char i=0;

  while(Serial1.available())
  {
    char c= Serial1.read();
    if(i==0)
    {
      //we are waiting for the first character of the next RFID tag. It has to be byte of value 2.
      //we ignore everything before we get 2.
      if(c==2) 
      {
        card[i]=c;
        i++;
      }
    }
    else
    {
      //we are already into reading the ID so we do not ignore any more characters ...
      card[i]=c;
      i++;
      if((i>18) || (c==3))  // if we get a terminating byte (value=03 according to RDM630Reader specification)...
      {                            // ...or we are nearly buffer overflow for some reason
        decode();
        i=0;
      }
    }
  }
}

void decode()
{
  unsigned char p;
  //you  can add checksum calculation here to verify if the tag is correct
  // CHECKSUM: card 10byte DATA entire do XOR operation

  for(p=0;p<19;p++) // let's print the whole buffer however the tag ID itself is from card[1]  through[11]
  {
    Serial.print(card[p],HEX);
    Serial.print(" ");
  }
  Serial.println();
}

Moderator edit: quote tags removed, code tags added.