RFID PN532 Variable Type

Hey all,

I'm using the PN532 RFID Library. I'm trying this snippet of code in the Arduino IDE to read a value off of the card. I can see it in the Serial Monitor, but I can't work out how to store it in a variable, I've tried different variations of int, char, uint8_t,uint16_t and they all resolve to 0.

nfc.PrintChar(data, 4);

This is the function being referenced in the C++ Library:

/**************************************************************************/
Adafruit_PN532::PrintChar(const byte *data, const uint32_t numBytes) {
  uint32_t szPos;
   for (szPos = 0; szPos < numBytes; szPos++) {
    if (data[szPos] <= 0x1F)
      PN532DEBUGPRINT.print(F("."));
    else
      PN532DEBUGPRINT.print((char)data[szPos]);
  }
  PN532DEBUGPRINT.println();
}

so data is a pointer to a byte buffer
the PrintChar() method receives that pointer and a number of byte to print as ASCII characters. Not all values of the ASCII table have a visual representation that's why the code checks if the byte is less or equal than 0x1F because as you can see in the table those are control codes.


(you could argue that >= 07F it's also not always printable)

so you would get a dot for those otherwise you get the ASCII representation.

so the question now is what do you want to get? a c-string equivalent to what would be printed out, a String, a shorter byte buffer? what do you want to do with it? does it hold an int or something you need to parse?

Thanks for your detailed reply, that helps to clear up how the function works.

I have stored a 4 digit number onto the tags. I have three tags : 1001,1002,1003.

The aim is to read which number it is, and then run it through a switch statement to then do different actions, based on what tag has been read.

Thanks!

so when you call nfc.PrintChar(data, 4); you see 1001 for example and you would want to get that number

You could create a small function that gets you that number, something like

long readTagID(const byte *data, const uint32_t numBytes) {
  char tempBuffer[numBytes + 1];       // create a temporary buffer enough bytes including  for a trailing null char
  memcpy(tempBuffer, data, numBytes);  // copy the bytes
  tempBuffer[numBytes] = '\0';         // add the trailing null char
  return atol(tempBuffer);             // parse the content as a long number and return it
}

and you would use it like this

long theTagID = readTagID(testData, 4); // testData being the payload you got from the tag

example here

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