Hotswap SDcard

Status
Not open for further replies.

RayanR

Member
Hy,

I'm currently working on a project where i use an SD card with the T4.1. I can succesfully read it out, but now i'd like to make it hotswapable.

i tried just seeing if i can open the file and if not: wait, reopen the sd and try to open the file again. but that doesn't seem to work. Once i take out the SD card, it just grinds to a halt.
Code:
  // open the file.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  else {
    // if the file isn't open, pop up an error:
    do{
      Serial.println("error opening datalog.txt");
      error_blink(2, 2, false);
    }
    while(!SD.begin(chipSelect));
    dataFile = SD.open("datalog.txt", FILE_WRITE);
    while(!dataFile)
    {
      Serial.println("error opening datalog file.txt");
      error_blink(2, 2, false);
      dataFile = SD.open("datalog.txt", FILE_WRITE);
    }
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }

greetings,
RayanR
 
My guess: you have a file open in write-mode when the SDcard is pulled, so that the close() call hangs trying
to write back the state.

Is the SD library designed to recover from this situation? Normally pulling a card that's actively being written
will corrupt it, this isn't something you should expect to work.
 
That could be it. I'l try to close the file after each write.

I know i shouldn't be doing it. but as i'm writing software for others to use the device, i want to make it foolproof ;).

the SD card actually isn't corrupt tho.
 
Status
Not open for further replies.
Back
Top