I have been having difficulty with a datalogger-style project; all of the code for the failing component is in the attachment. This component receives messages from remote components via XBee transceivers, and then it (for now) just stores the incoming data
The problem is all in the tFileBuffer.SaveContents() routine in FileBuffer.inc.
Control comes here long after the SD library routine has been successfully initialized (in the main program's setup), and all that tFileBuffer.SaveContents() has to do is to append the characters of TheBuffer to a file whose name is the value of FileName. Filename can be other things, but here it's always 1.txt.
It doesn't matter whether I try to open 1.txt with SD.open(FileName) or SD.open(FileName, FILE_WRITE). Either way, the open fails.
There's probably an silly error in my use of the SD package. What is it?
The problem is all in the tFileBuffer.SaveContents() routine in FileBuffer.inc.
Code:
void tFileBuffer::SaveContents()
{ if (NChars > 0)
{ // flush it
// File SaveFile = SD.open(FileName); // these fail identically
File SaveFile = SD.open(FileName, FILE_WRITE); // ..
if (SaveFile)
{ // nondegenerate
SaveFile.seek(SaveFile.size());
// SaveFile.write(TheBuffer, NChars); // this should work;
for (int I = 1; I <= NChars; I++) // this put in for absolute
{ SaveFile.print(TheBuffer[I-1]); } // simplicity in debugging.
Serial.printf(" saved %d chars in %s, file size now %d\n",
NChars, FileName, SaveFile.size());
SaveFile.close();
} // nondegenerate
else
{ Serial.printf("couldn't open %s\n", FileName); } // this is what always happens
NChars = 0;
digitalWrite(pinuSD, HIGH);
} // flush it
} // tFileBuffer::SaveContents()
It doesn't matter whether I try to open 1.txt with SD.open(FileName) or SD.open(FileName, FILE_WRITE). Either way, the open fails.
There's probably an silly error in my use of the SD package. What is it?