SD.open()返回true,但不创建文件 [英] SD.open() returns true but does not create a file
问题描述
我正面临SD卡和Arduino的特定问题.我想创建一个功能,以在另一天的SD卡上创建一个新文件.现在,我正在用int模拟日历.我知道这个问题已经讨论过了,但是我似乎找不到类似讨论的问题.
I'm facing a specific problem with SD card and Arduino. I want to create a function that creates a new file on the SD card for a different day. For now, I'm simulating calendar with ints. I know that problem was already discussed but I can't seem to find a similarly discussed problem.
代码:
#include <stdlib.h> // included for floatToString
#include <math.h>
#include <SPI.h>
#include <SD.h>
int year = 2014;
int month = 11;
int day = 4;
char dateTitle[20]; //= "0000000000.txt";
void printDateTitle(char* dateTitle, int Y, int M, int D){
//char dateTitle[20];
sprintf(dateTitle, "%4d-%02d-%02d.txt", Y, M, D);
return;
}
const int chipSelect = 4;
void setup() {
//printDateTitle(dateTitle, year, month, day);
Serial.begin(9600);
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("card initialized.");
}
void loop() {
Serial.print(dateTitle);
delay(1000);
File dataFile = SD.open(dateTitle, FILE_WRITE);
if (dataFile){
dataFile.println("something");
dataFile.close();
Serial.print(day);
Serial.println("something");
delay(1000);
} else
Serial.println("Error");
}
在代码中,我有一个函数 void printDateTitle
,该函数将日历中的输入格式化为要用作文件标题的字符串.
In the code, I have a function void printDateTitle
that formats the inputs from the calendar to string which I want to use as a title for the file.
而且,当我在 void loop()
中用 printDateTitle(dateTitle,year,month,day)定义函数时,
输出含义SD.open = false.
And also, when I define a function in the void loop()
with printDateTitle(dateTitle, year, month, day);
I get an "Error" in the output meaning SD.open = false.
问题在于,即使SD.open返回true,它也不会在SD卡上创建文件..txt包含在char数组中.我也用大写的.TXT.
The problem is that even though SD.open returns true it doesn't create a file on the SD card. .txt is included in the char array. I have also used capital .TXT.
对于有关该问题的所有建议,我将不胜感激.
I would be grateful for all the bits of advice regarding the problem.
For the reference, I'm using Arduino Uno and Micro SD card Adapter with Arduino IDE.
推荐答案
Arduino SD库文档指出它使用文件的短8.3名称".因此,只有名称为8个字符且文件扩展名为3个字符的文件有效.例如:12345678.txt有效,123456789.txt无效.
The Arduino SD Library documentation states that it uses the "short 8.3 names for files". Therefore only files with 8 characters as name and 3 for file extension are valid. For example: 12345678.txt is valid, 123456789.txt would be invalid.
您的日期字符串(2014-11-04.txt)太长,因为它有10个字符,而不是8个字符.
Your date string (2014-11-04.txt) is to long because it has 10 characters instead of only 8.
还要确保已将SD卡格式化为FAT16或FAT32文件系统.
Also make sure you've formatted the SD card to a FAT16 or FAT32 file system.
这篇关于SD.open()返回true,但不创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!