我一直在尝试将Arduino文件转储和目录列表示例一起运行并撞墙。
使用下面的草图,如果我尝试转储文件(即使文件不存在于SD卡上),然后执行SD的目录列表,草图将返回" **否更多文件**"。我认为这必定是由于某些事情尚未公开但几天未能找到。任何帮助将不胜感激。
草图:
#include <SD.h>
Sd2Card card;
SdVolume volume;
File root;
String inputString = "";
char FName[12];
const int chipSelect = 10;
void setup(){
Serial.begin(9600);
Serial.print("\nInitializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed.");
} else { Serial.println("SD Card OK"); }
/* if (!volume.init(card)) {
Serial.println("No Partition Format SD"); return; }*/
}
void loop(void) {
getSerialString();
if (inputString == "id") {
Serial.println("SD Card Test");
} else {
if (inputString == "fr") {
Serial.println("File to Read");
getSerialString();
inputString.toCharArray(FName, inputString.length()+1);
if (!dumpFile(FName)) {
Serial.print("error opening ");
Serial.println(FName);
}
} else {
if (inputString == "dir" ) {
File root = SD.open("/");
if (root ) {
printDirectory(root, 0);
root.close();
}
}
}
}
inputString = "";
}
boolean dumpFile(char fileNm[]) {
File dataFile = SD.open(fileNm);
if (dataFile) { // if the file is available, read from it:
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
} else {dataFile.close(); return false; }
}
void printDirectory(File dir, int numTabs) {
while(true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
Serial.println("**no more files**");
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
entry.close();
}
}
}
void getSerialString(void) {
char inChar = ' '; inputString = "";
// Serial.println("Waiting for User");
while (inChar != 10) {
while (Serial.available()) {
inChar = Serial.read(); // get the new byte:
if (inChar != 10) { // add it to the inputString:
inputString += inChar;
}
}
}
}