File transfer from SD for data logging device

I'm working on a device that is used to log frequencies from a metal detector. The purpose of my code is to detect when a USB connection has been established and transfer all files on the card to a PC. I am not able to test the code currently, so would like a bit of feedback on whether this will function as intended.

I am communicating with a python script through the serial to determine connection. The code should determine the number of files on the card, store the file names in an array and write each file to serial.

The code to check for USB connection:

  //check for usb connection
  if (Serial.available() > 0) {
    File root;
    root = SD.open("/");
    printDirectory(root, 0);
    while (true);
  }

printDerectory function:

void printDirectory(File dir, int numFiles)
{
  numFiles = 0;

  while (true)
  {
    while (!Serial);
    {}  //wait for serial to open

    if (!SD.begin(chipSelect))
    {
      serial.write("2"); //error message to pyserial
      while (true);
    }

    File entry = dir.openNextFile();
    if (!entry)
    {
      dir.rewindDirectory();
      break;
    }

    else
    {
      numFiles++;
    }
    entry.close();
  }
  fileList = new String[numFiles];

  for (int i = 0; i < numFiles; i++)
  {
    File entry = dir.openNextFile();
    fileList[i] = entry.name();
    entry.close();

    File dataFile = SD.open(fileList[i]); //need testing to see if this works
    while (dataFile.available())
    {
      Serial.write(dataFile.read());   //^^^
    }
    dataFile.close();
  }
}

You might use Wokwi to test your sketch before you use it with real hardware

https://docs.wokwi.com/parts/wokwi-microsd-card

Just open one of the examples and modify them to your needs. To open the built-in Serial command line print something in setup().

Hope that helps you!

The requirements of the software on the PC would very likley affect how the software on the Arduino runs.

So before writing the code for sending the files from the Arduino you probably need to work out how to receive the file and filename data on the PC so the PC can save the files to disk.

And whilst a serial transfer should be reasonably reliable, if the data your transferring is important or critical you need to have some method of checking the file received by the PC has all the correct data.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.