amazed
June 12, 2021, 7:42pm
1
Hello,
Regarding SdFat library, I would like to ask what is the supported character range which can be printed to a file (only with file.print command) ?
I'd like to test during file read, that everything is okay.
For example a basic check like this for standard ASCII range:
while ( logFile.available() )
{
uint8_t rb = logFile.read();
if (rb > 0x7F)
{
readError += 1;
if (readError >= 10)
{
sdError = true;
break;
}
}
else
{
Serial.write(rb);
}
}
Thanks in advance.
.write() and .read() work with any 8-bit value: 0 to 255.
.print should support unicode characters, so exceeding 0x7F is not a problem. The only limitation would be the inability to print a NUL.
amazed
June 13, 2021, 9:04am
4
Thanks for the info! Then I have to be fully aware of what I write/print in the log file, I guess.
Jokes aside, I asked this because I'd like to make data logging in my current project as errorproof as I can. So if I make sure that only standard characters are used, the simple check in #1 could work and if it signals error, eeprom could be used further on (with limited logging capability).
By the way, I checked if I remove the SD card or the whole SD module during reading (sort of a fault injection), the library will return 255.
A simple timeout is also useful.
The modified code snippet:
...
uint8_t readError = 0;
unsigned long dataReadStarted = millis();
logFile = SD.open(fileName);
if (logFile)
{
while ( logFile.available() && ((millis() - dataReadStarted) < 20000ul) )
{
uint8_t rb = logFile.read();
if (rb > 0x7F)
{
readError += 1;
if (readError >= 20)
{
sdError = true;
break;
}
}
else
{
Serial.write(rb);
}
}
}
...
Most(if not all) read functions return a signed interger where -1 indicates that there is nothing more to read. -1 equals 0xFFFF which is truncated by your code to a byte (0xFF) which is 255.
amazed
June 13, 2021, 9:30am
6
I see, thanks for the explanation!
You could add a hash or other type of error check code to each entry in the log if data integrity is that important.
amazed
June 14, 2021, 1:26pm
8
That is a good idea, but I think it is over my competence at the moment.
By the way, could somebody tell me, what type logFile is if I do this with SdFat library (@fat16lib ) ?
//...not working code snippet
File logFile;
logFile = SD.open(FILE_NAME);
I saw a lot of examples to check if the file exists or not with this simple method:
//not working code snippet
if (logFile)
{
//do something with the file
}
Is logFile a bool type? Is this a foolproof method or is there a better way? How can I check if file is not available?
Thanks in advance.
I don't have much experience with SD cards.
logfile is a file object. You can use it to determine if a file exists but it's not the correct way.
Not behind a PC at the moment, but there seems to be a sd.exist() method; I suggest that you do a little research.
amazed
June 15, 2021, 6:21am
10
I checked the library examples. You were right, there is an exists function.
// Remove any existing file.
if (sd.exists("ReadCsvDemo.csv")) {
sd.remove("ReadCsvDemo.csv");
}