SdFat library supported characters

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.

Thanks for the info! Then I have to be fully aware of what I write/print in the log file, I guess. :slight_smile:

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.

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.

That is a good idea, but I think it is over my competence at the moment. :slight_smile:

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.

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");
  }