0

I am using the RFID module to read mifare NFC chips and the library has only methods to send the data to the serial monitor. I need them to be saved to an SD card file. I have been adding custom methods to the library files to return strings that I then write to the SD card but this is a pain; there must be a quicker way to simply pipe all the data automatically but I do not know how to do this in cpp.

Here is an example of the function to dump to serial, that needs to be piped to the file on the SD card.

void MFRC522::PICC_DumpDetailsToSerial(Uid *uid) {
    Serial.print(F("Card UID:"));
    for (byte i = 0; i < uid->size; i++) {
        if(uid->uidByte[i] < 0x10)
            Serial.print(F(" 0"));
        else
            Serial.print(F(" "));
        Serial.print(uid->uidByte[i], HEX);
    } 
    Serial.println();

    // SAK
    Serial.print(F("Card SAK: "));
    if(uid->sak < 0x10)
        Serial.print(F("0"));
    Serial.println(uid->sak, HEX);

    // (suggested) PICC type
    PICC_Type piccType = PICC_GetType(uid->sak);
    Serial.print(F("PICC type: "));
    Serial.println(PICC_GetTypeName(piccType));
} 

Thanks!

  • the arduino does not have an operating system, so there is no "pipe" as is in linux ....... maybe there is a way to redefine the Serial object – jsotola Mar 21 at 23:57
2

Have the MFRC522 objects remember where you want them to send the output by adding a data member to the class like this:

private:
    Print &output;

This is a reference to a Print object, i.e. any object where you can print() or println() to. This reference should be initialized in the constructor, like this

MFRC522:MFRC522(...other arguments..., Print &my_output = Serial)
: ...other initializations..., output(my_output)
{
    ...constructor body...
}

Notice that, by default, the output is initialiazed as a reference to Serial. Thus, unless you explicitly ask for the output to go elsewhere, the class will behave as before.

Last step is to change, in the implementation of the class, every reference to Serial by output. This is just a global find and replace.

At this point, the MFRC522 class can be used exactly as before, and it will behave identically. This means that every user of the library should be able to use your modified version with no issues. I thus encourage you to submit the changes to the original author, so that they have a chance to be incorporated into the canonical version of the library.

Now, if you want this debugging output to be sent elsewhere, just provide the destination as the last argument of the constructor:

MFRC522 my_RFID_module(...other arguments..., my_SD_card_file);

As long as the destination is an instance of the Print virtual class (basically anything you can print() to should be OK), it should work as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.