I've formatted a MiFare Classic 1K with a NDEF message of 10 characters, and I'm trying to write a sketch that reads the contents of the payload and writes it to a text file on the SD card. I've already familiarized myself well in the ways of writing and reading to an SD card, but I'm having trouble getting the payload from the NFC card to write to the SD card.
To the best of my understanding (I'm a Mechanical Engineer by education) from looking at the Adafruit Library, the function PrintHexChar (PHC) is what prints the payload in plain text to the Serial port.
PrintHexChar Function:
Code: Select all
void Adafruit_NFCShield_I2C::PrintHexChar(const byte * data, const uint32_t numBytes)
{
uint32_t szPos;
for (szPos=0; szPos < numBytes; szPos++)
{
// Append leading 0 for small values
if (data[szPos] <= 0xF)
Serial.print("0");
Serial.print(data[szPos], HEX);
if ((numBytes > 1) && (szPos != numBytes - 1))
{
Serial.print(" ");
}
}
Serial.print(" ");
for (szPos=0; szPos < numBytes; szPos++)
{
if (data[szPos] <= 0x1F)
Serial.print(".");
else
Serial.print((char)data[szPos]);
}
Serial.println("");
}
Attempt 1: Change the Function Type
Basically I tried to change the function to a char and concatenate each byte to a string in a similar manner, like so: (Note I also changed the function definition in the .h file to char)
Code: Select all
char Adafruit_NFCShield_I2C::PrintHexChar(const byte * data, const uint32_t numBytes)
{
uint32_t szPos;
for (szPos=0; szPos < numBytes; szPos++)
{
// Append leading 0 for small values
if (data[szPos] <= 0xF)
Serial.print("0");
Serial.print(data[szPos], HEX);
if ((numBytes > 1) && (szPos != numBytes - 1))
{
Serial.print(" ");
}
}
char = payload[];
Serial.print(" ");
for (szPos=0; szPos < numBytes; szPos++)
{
if (data[szPos] <= 0x1F)
Serial.print(".");
else
Serial.print((char)data[szPos]);
payload += (char)data[szPos]);
}
Serial.println("");
return payload;
}
Attempt 2: Write Directly to SD within PHC
My next idea was to copy the way that PHC prints the text in what appears to be a string even though it's just printing the individual bytes next to one another. This attempt went a little something like this.
Code: Select all
void Adafruit_NFCShield_I2C::PrintHexChar(const byte * data, const uint32_t numBytes)
{
uint32_t szPos;
for (szPos=0; szPos < numBytes; szPos++)
{
// Append leading 0 for small values
if (data[szPos] <= 0xF)
Serial.print("0");
Serial.print(data[szPos], HEX);
if ((numBytes > 1) && (szPos != numBytes - 1))
{
Serial.print(" ");
}
}
File dataFile;
dataFile=SD.open("log.txt", FILE_WRITE);
Serial.print(" ");
for (szPos=0; szPos < numBytes; szPos++)
{
if (data[szPos] <= 0x1F)
Serial.print(".");
else
Serial.print((char)data[szPos]);
dataFile.print((char)data[szPos]);
}
Serial.println("");
dataFile.println("").
SD.close();
}
So I'm looking for some alternative way to take the text on my card and write it to a text file. Any ideas? Also please correct me on any incorrect assumptions I made. I live to learn. But be gentle.