storing char array on SD gives garbled content

Hi all,

I have a sketch that reads the UID of an NFC tags and is supposed to connect this with a directory name. Basically what I want to achieve is a file that shall look like this, when everything works out:

43322634231761291:larsrent

The first part is the UID of the NFC tag, the second part after the : is the directory name.

The function that is supposed to do write this key:value pair to a file on the SD card, is this:

boolean createAlbumDbEntry() {
  boolean success = false;
  byte bytesWritten;
  memset(trackDbEntry, 0, sizeof(trackDbEntry));
  
    Serial.print(F("writing nfc <-> album connection to disk: "));
    for (byte i = 0; i <= uidLength ; i++) Serial.print(uid[i]);
    Serial.print(F(":"));
    Serial.print(plrCurrentFolder);
    Serial.println(F("\n"));
  
  memcpy(trackDbEntry, charUid, sizeof(charUid));
  strcat(trackDbEntry, ":");
  strcat(trackDbEntry, plrCurrentFolder);
  strcat(trackDbEntry, "\n");
  
  File file = SD.open(trackDbFile, FILE_WRITE);
  bytesWritten = file.write(trackDbEntry, sizeof(trackDbEntry));
  file.flush();
  if (bytesWritten == sizeof(trackDbEntry)) success = true; else success = false;
  
  Serial.print(F("written "));Serial.print(bytesWritten);Serial.print(F(" bytes to disk"));
  
  return (success);
}

I’m using these 2 global variables to convert the UID as returned from the NFC library to a char array which I want to write to the file:

byte uid[]               = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
char charUid[18];                                         // char representation of the UID

I think I need to do this, because I need the UID as a consecutive string as opposed to the comma separated representation that is returned by the call to read the NFC tag:

nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

My problem is that the entry in the file looks like this:

!‚"ÁLÅ:larsrent

I can even track that it doesn’t work, as I get the following serial output from my Serial.print() statements:

SD-Card ready
NFC TAG READER: 
Found chip PN532
Firmware ver. 1.6

Scan your NFC tag
___________________________________________________

43322634231761290
Send a character to connect !⸮"⸮L⸮
 with directory / album larsrent
writing nfc <-> album connection to disk: 43322634231761290:larsrent

written 27 bytes to disk433

Whereas I would have expected the following output:

SD-Card ready
NFC TAG READER: 
Found chip PN532
Firmware ver. 1.6

Scan your NFC tag
___________________________________________________

43322634231761290
Send a character to connect 43322634231761290 with directory / album larsrent
writing nfc <-> album connection to disk: 43322634231761290:larsrent

written 27 bytes to disk

The complete sketch is attached.

Can anyone tell me what I’m doing wrong here???

Kind regards,

Chris

getNfcTagUid_SM.ino (11.7 KB)

Hi,

It looks like the uid is 7 binary values.
When you loop to print: Serial.print(uid[ i ]) it is printing 7 decimal numbers which is why "43322634231761290" has more than 7 characters in it.

You only memcpy the binary 'uid' into 'charUid', you should be printing the decimal character representation of 'uid' into 'charUid' to make it look like the earlier "43322634231761290".

Note that each byte of 'uid' could translate into a 3-digit number, so 7 bytes could translate into 21 characters which will not fit in char charUid[18]; (needs to be at least 22)

Yours,
TonyWilk

Ah great,

Thanks for the explanation. I reckon that means I should do a

sprintf(charUid, “%02d“, uid);

Correct?

siliconchris:
I reckon that means I should do a sprintf(charUid, “%02d“, uid);

Almost… I don’t know what values uid[0]…uid[6] might have.
Since you printed “43322634231761290”, then some must have 3 digits
(just do Serial.print(uid[ i ]);Serial.print(" "); to see the individual values)

If you need the uid as a long number in that form then:

char tmpbuf[4]; // temp buffer for one number

 if (success) {
   memset(charUid, 0 ,sizeof(charUid));
   for (byte i = 0; i <= uidLength ; i++) {
     sprintf( tmpbuf, "%d", uid[i] );   // print one byte as decimal
     strcat( charUid, tmpbuf );         // add to output string

     Serial.print(uid[i]);  // print for debug
   }
   Serial.print(" = "); Serial.println( charUid );  // print result

Yours,
TonyWilk