SD.open() 返回 true 但不创建文件 [英] SD.open() returns true but does not create a file

查看:18
本文介绍了SD.open() 返回 true 但不创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 SD 卡和 Arduino 的特定问题.我想创建一个函数,在不同的日子在 SD 卡上创建一个新文件.现在,我正在用整数模拟日历.我知道这个问题已经讨论过了,但我似乎找不到类似讨论的问题.

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.

而且,当我使用 printDateTitle(dateTitle, year, month, day);void loop() 中定义一个函数时,我在输出含义 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.

作为参考,我使用的是 Arduino Uno 和

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆