Why can't Serial.readString() see serial input from nfc.PrintHexChar?

I am an ultranoob when it comes Arduino in general. My friends know a lot more about it than I do. However, this problem seems to have even them stumped.

We are using Adafruit's NFC library and shield to read NTAG 215 NFC tags. Our goal is as follows:

  • Have nfc.PrintHexChar dump the data from page 6 of one of our NFC tags into the serial
  • Use Serial.readString to get the dumped data as a string, then concatenate it to two other strings to make a command string for a 3rd party program that's listening in on the serial
  • Print that command string to the serial so the 3rd party program can get it.

This is the code that should be doing all that:

if (success) // A bool that is 1 if an nfc tag is successfully read
        {
          // Dump the page data
          
        nfc.PrintHexChar(data, 4);
                  
        }
        else
        {
            Serial.println("Unable to read the requested page!");
        }

        if(Serial.available() > 0) {
  
        Serial.println("#S|LOGBALS|[" + Serial.readString() + "]#");
        
        }

I opened the serial monitor and scanned our tag. It printed the data line fine, but did not print anything else. It didn't seem to think that there was available data in the serial.

It was only when I manually typed words into the input box and clicked "Send" that something else happened: It printed our command string pieces concatenated to whatever thing I had typed into the box, with the first letter missing.

What's happening here? How can we access our nfc page data?

(First post. Sorry if there's problems, again, I barely know anything here.)

Please post a complete sketch rather than just a snippet of one so that it can be seen in context

It sounds like you have 2 devices sharing the same Serial connection. Is that correct ?

Hello,

I have an Arduino Uno hooked up to serial port COM4. When this problem is resolved, I will have another program running on my computer that will listen in on that serial connection. But first, I need to know why I can't get this command string together.

The sketch I have is the example code given by Adafruit for use with their NFC shield, we modified it to fit our purposes but wanted to preserve everything, which is why there's so many comments. Apologies.

/**************************************************************************/
/*!
    @file     readntag203.pde
    @author   KTOWN (Adafruit Industries)
    @license  BSD (see license.txt)

    This example will wait for any NTAG203 or NTAG213 card or tag,
    and will attempt to read from it.

    This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
    This library works with the Adafruit NFC breakout
      ----> https://www.adafruit.com/products/364

    Check out the links above for our tutorials and wiring diagrams
    These chips use SPI or I2C to communicate.

    Adafruit invests time and resources providing this open source code,
    please support Adafruit and open-source hardware by purchasing
    products from Adafruit!
*/
/**************************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK  (2)
#define PN532_MOSI (3)
#define PN532_SS   (4)
#define PN532_MISO (5)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ   (2)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield

// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a software SPI connection (recommended):
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// Use this line for a breakout with a hardware SPI connection.  Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

void setup(void) {
  Serial.begin(115200);
  while (!Serial) delay(10); // for Leonardo/Micro/Zero

  // Serial.println("Hello!");

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
  //    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
 // Got ok data, print it out!
//  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
//  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
//  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);

  // configure board to read RFID tags
  nfc.SAMConfig();

 // Serial.println("Waiting for an ISO14443A Card ...");
}

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 NTAG203 card.  When one is found 'uid' will be populated with
  // the UID, and uidLength will indicate the size of the UUID (normally 7)
  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("");

    if (uidLength == 7)
    {
      uint8_t data[32];

      // We probably have an NTAG2xx card (though it could be Ultralight as well)
      //   Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");

      // NTAG2x3 cards have 39*4 bytes of user pages (156 user bytes),
      // starting at page 4 ... larger cards just add pages to the end of
      // this range:

      // See: http://www.nxp.com/documents/short_data_sheet/NTAG203_SDS.pdf

      // TAG Type       PAGES   USER START    USER STOP
      // --------       -----   ----------    ---------
      // NTAG 203       42      4             39
      // NTAG 213       45      4             39
      // NTAG 215       135     4             129
      // NTAG 216       231     4             225

      for (uint8_t i = 6; i < 7; i++)
      {
        success = nfc.ntag2xx_ReadPage(i, data);

        // Display the current page number
       //  Serial.print("PAGE ");
        if (i < 10)
        {
         //   Serial.print("0");
          //   Serial.print(i);
        }
        else
        {
        //     Serial.print(i);
        }
      //    Serial.print(": ");

        // Display the results, depending on 'success'
        if (success)
        {
          // Dump the page data
        nfc.PrintHexChar(data, 1);
        nfc.PrintHexChar(data, 2);  
        nfc.PrintHexChar(data, 3);
        nfc.PrintHexChar(data, 4);

        Serial.println("#S|LOGBALS|[" + Serial.readString() + "]#");
                  
        }
        else
        {
            Serial.println("Unable to read the requested page!");
        }

        char *string = *data;
      //  Serial.print(string);
      }
      // handle input here.
      // reminder: uint32 data[32];

      char text[] = "";
    }
    else
    {
        Serial.println("This doesn't seem to be an NTAG203 tag (UUID length != 7 bytes)!");
    }

    // Wait a bit before trying again
    // Serial.println("\n\nSend a character to scan another tag!");
      Serial.flush();
    while (!Serial.available());
    while (Serial.available()) {
        Serial.read();
    }
     Serial.flush();
  }
}

The Arduino can't read the characters it sent to Serial. It can only read the characters the whoever is at the other end of the serial connection has sent.

If you want to send those same characters again, just call nfc.PrintHexChar(data, 4); again.

  Serial.print("#S|LOGBALS|[");
  nfc.PrintHexChar(data, 4);
  Serial.println("]#");

Thanks for your suggestion. I think I'll use this reply to update everyone on my situation.

I learned yesterday that what I was trying to do can be done much, much easier by using a terminal viewer (Tera Term). It logs the nfc data to a CSV file in my Google Drive.

However, I'm now facing a somewhat less serious, but still bothersome problem, which is that Tera Term can't seem to overwrite data in CSV files. Every time I start the logging process, and I select the CSV file that's already there, it just replaces the one I selected with a new one with the same name. This is a problem because I want to mess around with that data in Google Apps Script, and for that, there needs to be one URL to one file that the data always goes to.

Does anyone happen to know a different terminal viewer that I can use to overwrite instead of replace?

It sounds to me as though it is overwriting the data in the CSV file, but I suspect that is not what you mean

What exactly do you want it to do ?

Take a look at Coolterm as an alternative

You're right. After further testing, it seems that it does overwrite the csv file. For some reason, my computer gives me a "Do you want to replace this file" prompt whenever I select the csv file as the file to log to, which is a little odd, but it does keep the same Drive ID.

What I meant by "overwriting" was, I wanted to keep the file's Drive ID the same, but have the terminal overwrite the data within the file. Which, apparently, it was already doing. Thanks for the coolterm suggestion by the way, I might look into it.

Hopefully last question, can someone explain why every time I log new data to the CSV, the very first entry always starts with two weird g's?
image

I can get around this by simply not using index 0 when parsing in Google Apps Script, but it would still be nice to know if it can be changed.

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