File from PC to SD card module



I am currently working on a project to digitalize the paper receipt in retail environments using a
NodeMCU, NFC reader, OLED screen (for developer feedback) and an SD card module.

The situation would be: the cashier system “prints” the receipts on a generic/text printer with the adequate COM port. The device I am developing needs to receive a file from the cashier system and store it temporarly on the SD card. Next, the end consumer would be able to identify himself on the device with NFC (UID = Unique Identifier). Finally the file and the UID need to be sent to my Firebase database using WiFi.

I tested every indiviual module and everything works as expected.

This is the code I am now using to test the printer function:

#include <SD.h>

File myFile;
byte incomingByte = 1;

void setup()
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(10, OUTPUT);
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
  Serial.println("initialization done.");


void loop (){

void SD_receive()
  if (Serial.available() > 0) {
    myFile ="test.txt", FILE_WRITE);
     while(Serial.available() > 0)
     incomingByte =;
     Serial.print("Writing to test.txt...");
    // close the file:
  // else
    // if the file didn't open, print an error:
    //Serial.println("error opening test.jpg");
  // re-open the file for reading:
  myFile ="test.txt");
  if (myFile) {
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    // close the file:
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");


The thing I am struggling with is receiving and storing the “printed” file trough Serial.


How can I receive and store the “printed” receipt using an SD card module without getting extra characters or inconcistencies?

Big thanks in advance!



Are you sending text only?
What is the sending application?

Basically you will need to send in chunks of a number of bytes (e.g. 512) and wait for a reply from the Arduino. The Arduino will only reply when it has saved the data to the card. The last chunk might be smaller, so you need to add a length indication as well.

Alternatively, you can implement the xon/xoff protocol for text (basically same principle).

Are you sending text only?
What is the sending application?

The sending application would be a a printer task from a cashier system.
For this test I am using a Generic/Text printer to send data to my Serial port.

I attached a screenshot of the results in this reply.
There seem to be inconsistencies concerning the reception of the text file.

Update: Characters and text are being processed correctly 80% of the time.
Sometimes I get an extra character like the red arrow is pointing to in the screenshot.