SD.remove() 不会删除 Arduino C++ 上的文件
SD.remove() is not removing a file on Arduino C++
我有两个草图,我在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)。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 人脸跟踪arduino代码的优化
- vscode下的Arduino代码出现意外编译错误
- Arduino从Txt读取整数
- Arduino C++在构造函数中用参数声明对象数组
- C++ Boost::asio串行通信与Arduino无法写入
- Arduino millis() - millis() 怎么能等于 0 以外的任何东西?
- 在Arduino中将PWM通道命名为LEDC是否有特定原因
- 'val' Arduino 错误代码之前的预期'('
- 我可以在 Arduino 的库文件夹之外安装库吗?
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 无法在 Arduino 中uint8_t数组转换为无符号长整型数组
- Arduino-C++ bool 不会从 false 变为 true
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- cmsis_compiler.h:在为 Arduino Due 编译 Arduino Tensorflow lite 库
- Arduino SocketIoClient - 没有用于调用的匹配函数
- Arduino无法编译名为SP:"Expected ')' before '(' token"的变量
- 当我尝试通过 arduino 上的引用传递布尔数组时出现问题