RFID board - don't understand output

I've have one of these RFID boards...

It returns a 30 byte message, starting with 0x02, and end with 0x03.

I've written this code to read from the board.


#include <SoftwareSerial.h>

SoftwareSerial RFID(7, 8);

char ch;
char buffer[30];
int  idx;
boolean started = false;

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

void loop()
{
  while (RFID.available())
  {
    ch = RFID.read();

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

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

      if (ch == 0x03) // End byte
      {
        // Display the 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 (" ");
        }
        started = false;
      }
    }
  }
}

It seems to work fine... the message is as expected.. except for the 2nd to last byte (i.e. just before the last 0x03 byte).

The above code returns the following...

02 33 39 38 44 38 35 42 30 30 30 36 44 33 30 30 31 30 30 30 30 30 30 30 30 30 30 79 0FFFFFF86 03 

Can anyone explain why I get this odd output?

Thanks

Try:

      if (ch == 0x03) // End byte
      {
        // Display the 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 (" ");
        }
        buffer[idx-1] = 0 ;  // null terminate
        Serial.print (buffer+1) ; // print as string.
        started = false;
      }

Your message (between the 02 and 86 03) is ASCII and reads
398D85B0006D30010000000000y
The last section might be binary (zeros and then an 0x86 byte).

Because 'char' is a signed byte. When the top bit is 1 that means a negative number. Since the negative number is less than 0x10 you print a leading 0. Inside .print() the (char, HEX) gets passed to .print(long, HEX). The 8-bit 'char' is sign-extended to a 32-bit 'long' and printed in hex.

You can fix that by making buffer[] 'byte' instead of 'char'.

I suspect that the '0x86' byte is a checksum to make sure your data was received correctly.

Awesome.. thanks @johnwasser. That fixed the issue.