Made a program to get file dumps from SD cards onto computer

Whoops, I posted originally in the SD card forum but probably should have put it here instead. I'm making a project that grabs files off SD cards via serial connection and saves them on your computer. I'm intending to build a java GUI for it but the machine-dependent part is just a basic C program that can be used independently. I thought I would post it in case anyone finds it useful. It's Mac-only at the moment. I'll work on a Windows version later. I might also extend it to allow full control of the SD card (e.g. format, delete files). I didn't do that initially because I don't have enough room in my project for the code but that doesn't mean future projects will never have room.

I'm sharing it in case anyone finds it useful. The usage is simply:
sdreader [portname] [filename] > yourfile.extension

or sdreader [l] to get a listing of port names.

It's slow because serial communication is capped at ~240000 bps but it allows you to leave the SD card in your project and never have to pull it out. Just plug in via the normal USB cable you would use to program the Arduino.

Arduino code:

Libraries:
#include <SD.h> // should work with whatever library you choose, just match all code to it

Put in setup somewhere:
Serial.begin(230400); // test with CoolTerm if needed because Arduino IDE does not support this
Serial.setTimeout(100);

Globals (or whatever you want):
char input;
File file;
char filename[13];

Put in loop somewhere:

if (Serial.available() > 0) {
// user typed something
  
input = Serial.read();
if (input == 'D' || input == 'd') {   
  printRoot();
} else if (input == 'F' || input == 'f') {
  // asked for file
  while (!Serial.available()); // wait
  Serial.readBytesUntil('\n', filename, 13);
  
  // print it
  file = SD.open(filename);

  // if the file is available, read and print it:
  if (file) {
    while (file.available()) {
      Serial.write(file.read());
    }
    file.close();
  }
  else {
    Serial.println(F("File not found."));
  }
}

printRoot:

// modified version of the listfiles example from SD library
void printRoot() {
file = SD.open("/");
file.rewindDirectory();
if (!file) Serial.println(F("Couldn't open root."));
while(true) {
 File entry =  file.openNextFile();
 if (! entry) {
   // no more files
   break;
 }
 // Print the 8.3 name
 Serial.print(entry.name());
 Serial.print("\t\t");
 Serial.println(entry.size(), DEC);
 entry.close();
}
}

sdreader.c (11.7 KB)

Serial.begin(230400); // test with CoolTerm if needed because Arduino IDE does not support this

The IDE doesn't care what baud rate you use. The Serial Monitor application does. Big difference.

PaulS:

Serial.begin(230400); // test with CoolTerm if needed because Arduino IDE does not support this

The IDE doesn't care what baud rate you use. The Serial Monitor application does. Big difference.

Oh sorry, yes, I should have been specific.