I'd like to comment on this to confirm my understanding - which is:
- a clean file starts with 0x03 (ETX) as the 1st byte.
- there are 0x0A (LF) at the end of each line (of spaces) and at the end of the file (as normal)
- as text is written, it is inserted before the ETX
- but SD.println also appends an ETX to the line
- so after 20 SD.println's, I see 21 ETXs in the file
- I think the SDuFAT lib only cares about the one ETX and maybe the last LF
That's what I see when all goes well, but when using the "hola" example, it has been easy to loose the ETX, and then you're out in the ozone.
I've experienced the same problem. I think it happens when data
written to the file fill exactly a 512 byte sector. Here is a test
sketch:
#include <mmc.h>
#include <SDuFAT.h>
#include <microfat.h>
char myBuffer[257];
void setup (void) {
Serial.begin (9600);
Serial.println ("In order to run this test, hola.txt must be empty.");
Serial.println ("You can use the hola.txt file included in SDuFAT.zip\n");
Serial.println ("Send a character to write first block");
while (Serial.available() == 0);
Serial.read();
for (int i=0; i<=256; i++) {
myBuffer[i] = 'A';
}
myBuffer[256] = 0;
int result = SD.print ("hola.txt", myBuffer);
Serial.print ("First block written, result = ");
Serial.println (result, DEC);
Serial.println ("Send a character to write second block");
while (Serial.available() == 0);
Serial.read();
for (int i=0; i<=256; i++) {
myBuffer[i] = 'B';
}
myBuffer[256] = 0;
result = SD.print ("hola.txt", myBuffer);
Serial.print ("Second block written, result = ");
Serial.println (result, DEC);
Serial.println ("Send a character to write the final string");
while (Serial.available() == 0);
result = SD.print ("hola.txt", "ciao");
Serial.print ("Final result = ");
Serial.println (result, DEC);
}
void loop () {}
After the first print, in the file there are 256 'A' followed by 256
ETX, because SDuFAT fills the empty space of the sector with it.
After the second print, in the file there are 256 'A', followed by 256
'B', followed by spaces and a few LF. No ETX: the library "forgets" to
add it in the subsequent sector of the file.
The string "ciao" is never written to the file, because SDuFAT can't
find ETX in it.