RFID tag reader problems

Hi folks,
This is my first post here, relatively new to c++ and arduino, Over the past few months I've been building and designing a device to monitor the health of a population of red squirrels I look after.
The ultimate aim is to have a unit that measures and records (read datalogger!) the time, date, weight, temperature, humidity, microchip number - then logged onto an SD card and onto a 20x4 lcd screen.
So far I've managed to achieve all of the above, with the exception of reading the microchip number.

For simplicity we'll ignore all but the microchip problem!

Arduino uno R4
WL-134 RFID reader

connected to pin 0 (Rx) 5v and gnd,

I have tried several permutations of code (most from on here - Red_Car I believe), and whilst I do recieve some data in the serial monitor, it is not correct or correctly interperitable (as I understand it.)

#include <SoftwareSerial.h>


SoftwareSerial RFID(0, 1);

byte b;
byte buffer[30];

uint8_t idx;
boolean started = false;

byte XOR;
byte inverted;

uint64_t value;

void setup()
{
Serial.begin (9600);
RFID.begin (9600);
}

void loop()
{

String content = "";
while (RFID.available())
{
b = RFID.read();

if (b == 0x02) // Start byte
{
  idx = 0;
  started = true;
}



if (started) // Ignore anything received until we get a start byte.
{
  buffer[idx++] = b;

  if (b == 0x03) // End byte
  {
    started = false;
  
    
    
    // Display the received data.
    Serial.print("Received data: ");
    for (int x = 0; x < idx; x++)
    {
      if (buffer[x] < 0x10)
      {
        Serial.print("0"); // Pad with leading 0
      }
      Serial.print (buffer[x], HEX);
      Serial.print (" ");
    }
    Serial.println("");


    // Check we received the message ok.  XOR checksum on bytes 01-26 should match byte 27.
    XOR = buffer[1];
    for (int x = 2; x <= 26; x++)
    {
      XOR ^= buffer[x];  
    }
    Serial.print("Calculated checksum: ");
    Serial.print(XOR, HEX);

    if (XOR == buffer[27])
      Serial.println(" (Correct)");
    else
      Serial.println(" (Error)");


    // Check the inverted XOR checksum
    inverted = ~XOR;
    Serial.print("Inverted checksum: ");
    Serial.print(inverted, HEX);
    if (inverted == buffer[28])
      Serial.println(" (Correct)");
    else
      Serial.println(" (Error)");

    
    // Extract the card number from bytes 01 (LSB) - 10 (MSB).
    value = 0;
    for (int x = 10; x >= 1; x--)
    {
      if(buffer[x] <= '9')
        value = (value<<4) + buffer[x] - '0';  
        
      else
        value = (value<<4) + buffer[x] - '7';  
    
    }
    Serial.print("Card number: ");
    Serial.println(value);
    
    

    // Extract the country number from bytes 11 (LSB) - 14 (MSB).
    value = 0;
    for (int x = 14; x >= 11; x--)
    {
      if(buffer[x] <= '9')
        value = (value<<4) + buffer[x] - '0';
      else
        value = (value<<4) + buffer[x] - '7';  
    }
    Serial.print("Country number: ");
    Serial.println(value);


    // Extract the Data Block from byte 15.
    Serial.print("Data block: ");
    Serial.println(buffer[15] - '0');

    
    // Extract the Animal Flag from byte 16.
    Serial.print("Animal flag: ");
    Serial.println(buffer[16] - '0');


    Serial.println("\r");
  }
}}}

In the serial plotter I recieve

"Received data: 02 FD 03

Calculated checksum: 8B (Error)

Inverted checksum: 74 (Error)

Card number: 158002015478

Country number: 900

Data block: 1

Animal flag: 1"

The microchip should read as - 900158002016106

I have read a lot of the posts on here about RFID readers, and I'm loathed to ask for help but I've only succeeded in confusing myself!

Any help greatly appreciated.

I hope I've got things formatted correctly!

Ben

Have you tried the Arduino library for the WL-134 RFID reader ?

1 Like

@treespanner
How do you expect a correct checksum, if you read only 3 bytes, but calculate a checksum for 26 ? Where this 26 is coming from? Is your RFID tag length 26 bytes?

Then, why do you think that every message ends with 0x03 byte? The last byte of the message is checksum byte and it MUST varied for variable data to sure that checksum is adequate.

Your code is a mess. Where you found it?
Try to start from the examples.

1 Like

Hi,

After reinstalling the library and example, yes that is now working.

I am not sure what changed during my initial effort, but it's now going!

My next challenge is to display the data on an lcd display, and add the data to an existing data string.

I will report back either way,

Thanks
Ben

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.