c++ - SD.remove() 没有删除 Arduino C++ 上的文件

标签 c++ arduino

我有两个草图在 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(){

}

我是不是漏掉了什么?

在发布这个之后我意识到我没有让这个 remove sketch 容错并且在 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.");

于是新的remove sketch如下:

#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() 不会使其容错。它初始化库。您需要在调用其他函数之前调用它。来自Reference :

begin() Initializes the SD library and card. This begins use of the SPI bus (digital pins 11, 12, and 13 on most Arduino boards; 50, 51, and 52 on the Mega) and the chip select pin, which defaults to the hardware SS pin (pin 10 on most Arduino boards, 53 on the Mega).

关于c++ - SD.remove() 没有删除 Arduino C++ 上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13549118/

相关文章:

c++ - 从 arduino 读取到 c++ 程序时保持接收 NULL

c++ - 在新变量中存储类实例会导致奇怪的行为(Arduino Serial)

c++ - 带有数组的 Arduino C++ 类

c++ - 从我自己的库调用时,伺服器没有 move 。阿杜诺

python - 如何使用派生的 Python swigged 对象访问 C++ 基类函数?

c++ - 如何应用 DOP 并保持良好的用户界面?

C++:是否有包含各种函数的特征类来操作零终止的 char * 和 wchar_t * 字符串?

c++ - 如何从函数数组中调用特定函数?

c++ - c中的内存泄漏

c++ - 如何重载 << 运算符