I'm trying to log binary on an SD card using a Teensy 4.1 via SdFat.h, but I'm having issues when writing low-value bytes. There is at least one bye between 0x0 to 0x4 that, when I write it to the card, it closes the file and opens a new file. Very bizarre.
Pseudo code that basically does what I'm doing in my code (my code is very complex and I want to keep it private, so snippets will have to suffice):
My answer is very complex, but I want to keep it private...
Seriously, though, you're making it hard for people who want to help, which reduces the likelihood dramatically. You could take a very short break and work up a simple example that illustrates the problem; that would probably make our lives simpler than having to read your 'very complex' code anyway.
Given your example, exactly what does the file contain?
It seems odd that you open the file, write data to the file, close the file, then open it again at the end. It would also be good practice to check that the file is successfully opened before writing the data and closing the file.
If you are opening a pre-existing file with FILE_WRITE, intending to add data to the end, you may need to go to EOF before writing additional data.
Not when write() is given a uint8_t, it just writes the single byte it is given. write() will recognize a null '\0' as end of string when given a char*.
I don't think there is such a thing. I've never had to specify the data type when opening a file.
Do you mean it closes the file, and opens a completely different file, with a different file name? If that is the case, I would suspect the 'filepath' variable is changing, either intentionally or by being overwritten accidentally by other code.
Can't you tell from reading back the file what the last value written into the file was?
Another thought, you may have a memory issue with lack of ram, and the code is crashing and restarting.
Just tested the writing with below code on a Pro Micro (don't have Teensies).
It happily writes the binary data
#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"
// SD card chip select pin. Adjusted for ProMicro setup with CS on A0
const uint8_t chipSelect = A0;
// File system object.
SdFat sd;
// Use for file creation in folders.
SdFile file;
char filename[13] = "test15.txt";
void setup()
{
Serial.begin(115200);
while (!Serial)
;
Serial.println("SD timing test");
if (!sd.begin(chipSelect, SD_SCK_MHZ(50)))
{
Serial.println("Could not begin");
for (;;)
;
}
//if (!file.open(filename, O_WRONLY | O_CREAT))
if (!file.open(filename, FILE_WRITE))
{
Serial.println("Could not open file");
for (;;)
;
}
//file.open(filename, FILE_WRITE);
file.write((uint8_t)0x0);
file.write((uint8_t)0x1);
file.write((uint8_t)0x2);
file.write((uint8_t)0x3);
file.write((uint8_t)0x4);
file.write((uint8_t)0x0);
file.write((uint8_t)0x1);
file.write((uint8_t)0x2);
file.write((uint8_t)0x3);
file.write((uint8_t)0x4);
file.close();
Serial.println("Complete");
}
void loop()
{
// put your main code here, to run repeatedly:
}
So FILE_WRITE should work. The other option shown also works.
Note:
Changing SdFile to FsFile and the serial monitor tells me that it can't open the file. I'm probably missing something but you did not give a complete example to test so I don't know.