Understanding variables that store buffer values

Hi,

I'm working with an Adafruit RFID reader (PN532) and I'm using the example code they provided to help me read the ID numbers for each RFID tag. I wanted to use the UID Value for other purposes but I can't seem to figure out which variable is storing the ID number. I thought it was uid but since it's a buffer, it's not letting me use it as a variable. I basically want to serial print that UID value later on in the code.

I have attached initial part of the code for your reference. Any help is much appreciated! Thanks!

[code]
void loop(void) 
{
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  
//Wait for an ISO14443A type cards (Mifare, etc.)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  
  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");

[/code]

It looks like your code is closely related to this: https://github.com/adafruit/Adafruit-PN532/blob/master/examples/readMifare/readMifare.pde

What about this code isn’t working properly? It looks to me like it will print the UID value to Serial just fine.

You may want to read the Arduino reference on arrays.

The uid is storing the ID "number" but that number is an array of bytes. The line

   nfc.PrintHex(uid, uidLength);

is printing that array to the serial interface.

I guess what I’m asking is how to retrieve the ID number stored in the uid array to be able to use it later. The UID value shows up in the serial monitor similar to this: “0xF4 0xB3 0x67 0x22” and I wanted to be able to refer to this entire value later. When I try to use just uid an error shows up saying “call of overloaded ‘print(uint8_t[7])’ is ambiguous”. Sorry I’m such a beginner at this!

Do I have to write something like: int x = uid ?

You already have it stored in an array and that’s likely the best you’re going to do. What you have to figure out is how to do whatever it is that you want to do with this number using the array. You haven’t yet been very clear about what exactly you want to do with it.

If you want to just print it out:

for (int i = 0; i<7; i++){
    Serial.print(uid[i], HEX);    //// BUG HERE SEE REPLY #7
}

If you want to compare it to another array of 7 bytes:

if(memcmp(uid, otherArrayName) == 0){
     // they're the same
}

When you say you want to be able to refer to this value later on that is useless. It depends on what you want to do with it. How do you want to use it. You have it in an array called uid and you can refer to that array. The only thing is you have to learn about working with arrays in C++. Grab any good C++ tutorial for that.

Ohh I see. I wanted to log this value into Excel using a data logging shield. This is the code that I used to log the value. I wasn’t sure what to put in the bracket with the “UNSURE” to be able to log the UID value.

[code]
logfile.print(UNSURE); // Write id number to the SD card
logfile.print(",");  
#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.println(UNSURE; 
  Serial.println("");
#endif //ECHO_TO_SERIAL

[/code]

You won’t print it with one single statement like that. You’ll need a for loop to print out the individual elements one at a time as in reply #4.

for (int i = 0; i<7; i++){
    if(uid[i]<0x10) Serial.print('0');
    Serial.print(uid[i], HEX);
}

MorganS:

for (int i = 0; i<7; i++){

if(uid[i]<0x10) Serial.print(‘0’);
    Serial.print(uid[i], HEX);
}

Very good point. Noted in reply #4.

sjacob:
I guess what I'm asking is how to retrieve the ID number stored in the uid array to be able to use it later. The UID value shows up in the serial monitor similar to this: "0xF4 0xB3 0x67 0x22" and I wanted to be able to refer to this entire value later.

Are you thinking to accommodate your array contents as a numerical value in a single variable? If so, you can use the union data structure:

void setup()
{
  Serial.begin(9600);
  union
  {
    uint8_t uid[4] = {0xF4, 0xB3, 0x67, 0x22};
    uint32_t x;
  } myData;
  Serial.print(myData.x, HEX);  //Prints: 2267B3F4; take care of endianness of your data
}

void loop() 
{
  
}

GolamMostafa:
Are you thinking to accommodate your array contents as a numerical value in a single variable? If so, you can use the union data structure:

void setup()

{
  Serial.begin(9600);
  union
  {
    uint8_t uid[4] = {0xF4, 0xB3, 0x67, 0x22};
    uint32_t x;
  } myData;
  Serial.print(myData.x, HEX);  //Prints: 2267B3F4; take care of endianness of your data
}

void loop()
{
 
}

Except that in the OP’s case he has 7 bytes and not 4. So he’d still need an array of 2 uint32_t to accomodate.

Except that in the OP's case he has 7 bytes and not 4. So he'd still need an array of 2 uint32_t to accomodate.

Absolutely right!

void setup()
{
  Serial.begin(9600);
  union
  {
    uint8_t uid[8] = {0x12, 0x34, 0x56, 0x78, 0xF4, 0xB3, 0x67, 0x22};
    uint64_t x;
    uint32_t uidX[2];
   
  } myData;
  Serial.print(myData.uidX[1], HEX);  //take care of endianness of your data
  Serial.println(myData.uidX[0], HEX); //2267B3F478563412
}

void loop() 
{
  
}

//take care of endianness of your data

I'm glad you know. The only way I've ever been able to deal with endianess was trial and error. If it comes out backwards then I must have the wrong endian.

Hi everyone,

Sorry for the late reply. Thanks for all the replies that you’ve given! I don’t think that I’ve been explaining my situation properly. I’m really new to this so I didn’t know the terminology that I should use to explain what’s happening. I’ve attached my full code here so that it’s easier to understand what I’m trying to do: [code]#include <SD.h> //load the SD library#include <SPI.h> //load the SPI - Pastebin.com

I have a RFID shield stacked on top of a data logging shield and an Arduino. Every time I detect an RFID tag with the PN532 RFID reader, I want it to log the ID number (looks something like this 0xB4 0x7F 0x66 0x43) into the SD card. I’ve been trying to use this (Adafruit-PN532/Adafruit_PN532.cpp at master · adafruit/Adafruit-PN532 · GitHub) to figure out what function to use in order to retrieve that ID number. I’ve been using nfc.PrintHex(uid, uidLength) to print the ID number to the serial monitor. But I’m not sure what to use to print that same number onto the SD card. The code I’ve pasted down below is what I tried to do to print the ID number based on what you guys were saying before, but it only prints the first section (for example, 0xB4, instead of 0xB4 0x7F 0x66 0x43). I’m not sure why that’s happening either. So could anyone help me figure out how to print the ID number onto the SD card?Thanks for all your help! I really appreciate it!

for (uint8_t i=0; i < uidLength; i++)
{
    if(uid[i]<0x10);               
}
   
logfile.print(uid[i], HEX);  // Write id number to the SD card
logfile.print(",");
 
#if ECHO_TO_SERIAL
  Serial.print(", ");
  Serial.print(F(" 0x")); Serial.print(uid[i], HEX);
  //nfc.PrintHex(uid, uidLength);
  Serial.println("");
#endif //ECHO_TO_SERIAL
for (uint8_t i=0; i < uidLength; i++)
{
    if(uid[i]<0x10);               
}

If uid[ i ] is less than 0x10, do nothing. Otherwise, do nothing.

Just what does THAT accomplish?

logfile.print(uid[i], HEX);  // Write id number to the SD card

By the time the for loop ends, i is equal to uidLength. Printing the first element beyond the end of the array seems pointless.

Convert your 64-bit (8-byte) ID (which is in this array uint8_t uid[8] = {0x12, 0x34, 0x56, 0x78, 0xF4, 0xB3, 0x67, 0x22}:wink: into character array and then write this character array into the SD card using print() method.

Where did you get this array from: {0x12, 0x34, 0x56, 0x78, 0xF4, 0xB3, 0x67, 0x22}?

Just an arbitrary array based on your declared array (uint8_t uid = { 0, 0, 0, 0, 0, 0, 0 }; ) in your original post which is a 7-byte array.