file.flush() does not work with PlateformIO

gplante

Member
Hi,

I'm using a Teensy 4.1 board with its built in sd card drive. I'm using PlateformIO as the IDE. I'm having hard time to keep saved data after a power outage. To make a long story short, I'm constantly logging data to a file on the sd card. Periodically, I'm using file.flush() to be sure that at least last data written before the last file.flush() will be saved on the sd card. Unfortunately, it does not work. It looks like file.flush() do nothing at all. The only way to get it working is to open() and close() the file periodically instead of using flush().

After several unsuccessful search in forums, I decided to do a very small test program to try to isolate the problem and tried it with the Arduino IDE v1.8.19 instead of my PlateformIO IDE. For an unknown reason, it works just fine with the Arduino IDE. All data written before the last file.flush() are correctly saved on the sd card even after a power outage. I tried back this test program with PlateformIO without changing anything and it does not work with PlateformIO. Data are not saved to the file. I tried with sd cards exFat as well as Fat32 formats. See the test program below.

Code:
#include <SD.h>

File myFile;

void setup()
{
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect.
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(BUILTIN_SDCARD)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  myFile = SD.open("test.txt", FILE_WRITE);
  Serial.println(myFile.size());
  
  if (myFile) {
    Serial.print("Writing to test.txt...");
    for (int i = 0; i < 4096; i++) {
     myFile.print("X");
    }	
    Serial.println(myFile.size());
    myFile.flush();
    //myFile.close();
    
    Serial.println("Power off Teensy 4.1 board now to do the test.");
    while (true);
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }    
}

void loop()
{
	// nothing happens after setup
}

So, did I miss something?

Thank you for your time.

G. Plante
 
Are you sure you're using the same <SD.h> and <SdFat.h> on PlatformIO as on Arduino? Teensyduino provides its own versions of SD and SdFat libraries. Perhaps you use the Arduino ones with PlatformIO instead?
 
Hi,

Thank you for your answer.

When I click SD.h or SdFat.h to access the source from PlateformIO, files are located in following folders:
C:\.platformio\packages\framework-arduinoteensy\libraries\SD\src
C:\.platformio\packages\framework-arduinoteensy\libraries\SdFat\src

I do not think that Arduino IDE are using these files. I have made a search and for Arduino IDE, it seams to be located in folder:
C:\Users\user1\Documents\Arduino\libraries\SD

Usually, I let Arduino IDE and PlateformIO updating their libraries...

Should I do some updates manually?
 
For an unknown reason, today, my test program above started to work on PlateformIO as well. So I was able to find and fix the problem in my real program.

I will never know why my test program did not work yesterday and now works fine today since I did not updated anything in PlateformIO...
Sorry about this and thank you for your time.
 
Could be some kind of caching issue, I dunno.

Sometimes, I admit, I forget to Save my source code before I compile/build and install it. And then scratch my head in puzzlement...
 
Back
Top