SD.remove() 不会删除 Arduino C++ 上的文件

SD.remove() is not removing a file on Arduino C++

本文关键字:C++ Arduino 文件 删除 remove SD      更新时间:2023-10-16

我有两个草图,我在Arduino Uno上运行。第一个将文件转储到串行(如果存在)。这是Arduino附带的示例之一,但是我已经对其进行了修改:

/*
    SD card file dump
    This example shows how to read a file from the SD card using the
    SD library and send it over the serial port.
    The circuit:
    * SD card attached to SPI bus as follows:
    ** MOSI - pin 11
    ** MISO - pin 12
    ** CLK - pin 13
    ** CS - pin 4
    Created  22 December 2010 by Limor Fried
    Modified 9 Apr 2012 by Tom Igoe
    This example code is in the public domain.
 */
#include <SD.h>
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;
void setup()
{
    // Open serial communications and wait for port to open:
    Serial.begin(115200);
    while (!Serial) {
        ; // Wait for serial port to connect. Needed for Leonardo only.
    }
    Serial.print("Initializing SD card...");
    // Make sure that the default chip select pin is set to
    // output, even if you don't use it:
    pinMode(10, OUTPUT);
    // See if the card is present and can be initialized:
    if (!SD.begin(chipSelect)) {
        Serial.println("Card failed, or not present");
        // Don't do anything more:
        return;
    }
    Serial.println("card initialized.");
    // Open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    File dataFile = SD.open("datalog.txt");
    // If the file is available, write to it:
    if (dataFile) {
        while (dataFile.available()) {
          Serial.write(dataFile.read());
        }
        dataFile.close();
    }
    // If the file isn't open, pop up an error:
    else {
        Serial.println("error opening datalog.txt");
    }
}
void loop()
{
}

我的另一个草图应该删除一个文件。当我运行此删除草图时,它说找不到文件。然而,我可以继续运行上面的草图并将内容转储到连续剧中。我的删除草图如下:

#include <SD.h>
const int chipSelect = 4;
void setup(){
    Serial.begin(115200);
    Serial.print("Initializing SD card...");
    // Make sure that the default chip select pin is set to
    // output, even if you don't use it:
    pinMode(10, OUTPUT);
    if(SD.exists("datalog.txt"))
    {
        SD.remove("datalog.txt");
        Serial.println("file removed");
    }
    else
    {
        Serial.println("no file to remove");
    }
}
void loop(){
}

我在这里错过了什么吗?

发布此内容后,我意识到我没有使这个删除草图容错,并在pinmode行后添加了以下代码:

  // See if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
      Serial.println("Card failed, or not present");
      // don't do anything more:
      return;
  }
  Serial.println("card initialized.");

因此,新的删除草图如下:

#include <SD.h>
const int chipSelect = 4;
void setup(){
    Serial.begin(115200);
    Serial.print("Initializing SD card...");
    // Make sure that the default chip select pin is set to
    // output, even if you don't use it:
    pinMode(10, OUTPUT);
    // See if the card is present and can be initialized:
    if (!SD.begin(chipSelect)) {
        Serial.println("Card failed, or not present");
        // Don't do anything more:
        return;
    }
    Serial.println("card initialized.");
    if(SD.exists("datalog.txt"))
    {
        SD.remove("datalog.txt");
        Serial.println("file removed");
    }
    else
    {
        Serial.println("no file to remove");
    }
}
void loop(){
}

运行该草图后,它现在会删除文件。为什么新版本有效,而旧版本无效?

添加SD.begin()不会使其容错。它初始化库。您需要在调用其他函数之前调用它。从参考资料:

begin() 初始化 SD 库和卡。这开始使用 SPI 总线(大多数 Arduino 板上的数字引脚 11、12 和 13;50、51、 和 52 在 Mega) 和片选引脚,默认为 硬件 SS 引脚(大多数 Arduino 板上的引脚 10,Mega 上的引脚 53)。