Writing NFC Payload to a Datafile

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
paularcoleo
 
Posts: 6
Joined: Mon Feb 03, 2014 9:45 pm

Writing NFC Payload to a Datafile

Post by paularcoleo »

Hi all, I'm semi-new to Arduino projects. I'm using the Adafruit NFC shield as well as the Seeed Studios SD card shield.

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("");
}
Translating the 2nd for loop to my best abilities, the loop runs until the end of the block, printing a "." if the hex value is 1F and otherwise printing the ASCII (char) conversion of the hex data at the current byte. Based on this translation I attempted to modify the code in a couple of ways in order to try and get what I wanted, but so far none have worked.

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;
}
And then in my sketch, the value of payload would be written to the SD file. My motivation for this approach was based on my understanding of variable scope. If I defined and concatenated a string within PHC I wouldn't be able to access it because nothing outside of the function sees that variable. The idea was to forward declare it in the sketch and then pass it back through the function where it could be dealt with. Result: the compiler yelled at me for changing the function type from void to char.


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();
}
Turns out that I can't use Arduino commands in C++. The compiler yelled at me a lot.

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.

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Writing NFC Payload to a Datafile

Post by adafruit_support_mike »

Personally, I'd steer away from the String class for this particular operation. It's convenient for sending information back to the computer through the serial connection, but kind of gets in the way if you want to use plain old strings.

I'd do:

Code: Select all

    char buffer[5];  // or however large you want it to be
    sprintf( buffer, "%04X", data );
If you want to roll your own (which might be legitimate since the 'sprintf()' code is fairly large), use the same basic idea: create a char buffer in the calling function, then pass that to the function with fills the buffer. That keeps the scope issues under control.

User avatar
paularcoleo
 
Posts: 6
Joined: Mon Feb 03, 2014 9:45 pm

Re: Writing NFC Payload to a Datafile

Post by paularcoleo »

Sorry for my ignorance, but I'm not familiar with sprintf. What does that do?

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Writing NFC Payload to a Datafile

Post by adafruit_support_mike »

'printf()' is the standard C function for printing formatted output. 'sprintf()' works the same way, but stores the result in a string rather than sending it to standard output.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”