YFROBOT创客社区

标题: Arduino SD库使用 - 文件打开失败,无法读写,原来是名称问题! [打印本页]

作者: AllBlue    时间: 2015-7-22 08:17
标题: Arduino SD库使用 - 文件打开失败,无法读写,原来是名称问题!
本帖最后由 AllBlue 于 2015-7-24 08:13 编辑

Arduino SD库使用 - 文件打开失败,无法读写,原来是名称问题!

SD库使用语法参考地址:http://www.yfrobot.com/wiki/index.php?title=Arduino%E5%BA%93

最近在使用SD库读写SD卡,但发现有些文件总是读写不成功(文件打开失败),短名称的文件可以打开,打不开的文件名都长;在网上也有人遇到这种情况:
#读写文件的名称不能超过8个字符,后缀名不超过3个字符(例如:12345678.txt可以读写,123456789.txt文件就打不开了)。
#文件不分大小写,创建的文件一律大写。
测试IDE版本:1.0.5 。

开始以为是BUG,仔细看官网上关于SD库的说明才发现,有明确指出使用8.3格式的短文件名。下图为SD库说明截图:
[attach]1034[/attach]

不过还是参考官方例程,改写个程序做个名称测试!
[attach]1020[/attach] -- 名称测试程序
[attach]1021[/attach] -- 查看储存卡内文件列表程序,通过该程序可以输出储存卡所有文件名及大小:
首先准备一张TF内存卡,烧写listfiles程序,查看卡内文件(我准备的卡中没有文件,所以无输出),如图:
[attach]1022[/attach]

将下面的名称测试程序代码烧写到控制板中,改变名称,测试打开文件是否成功:
[C] 纯文本查看 复制代码
/*
  SD card basic file example

This example shows how to create and destroy an SD card file         
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created   Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.
         
*/
#include <SD.h>

File myFile;

String FileName = "read.txt";                  //此处更改文件名称
char* fileName = (char*)(FileName.c_str());

String name;

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...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  if (SD.exists(fileName)) {
    name = FileName +" exists.";
    Serial.println(name);
  }
  else {
    name = FileName +" doesn't exist.";
    Serial.println(name);
  }

  // open a new file and immediately close it:
  name = "Creating " + FileName;
  Serial.println(name);
  myFile = SD.open(fileName, FILE_WRITE);
  myFile.close();
  
  // Check to see if the file exists:
  if (SD.exists(fileName)) {
    String name = FileName +" create succeed.";
    Serial.println(name);
  }
  else {
    String name = FileName +" create failure.";
    Serial.println(name);
  }
  
  // open the file for read and immediately close it:
  name = "Opening " + FileName;
  Serial.println(name);
  myFile = SD.open(fileName, FILE_READ);

  // Check to see if the file opened:
  if (myFile) {
    String name = FileName +" open succeed.";
    Serial.println(name);
  }
  else {
    String name = FileName +" open failure.";
    Serial.println(name);
  }
  myFile.close();


  // delete the file:
  name = "Removing " + FileName;
  Serial.println(name);
  SD.remove(fileName);

  if (SD.exists(fileName)){
    String name = FileName +" exist.";
    Serial.println(name);
  }
  else {
    String name = FileName +" doesn't exist.";
    Serial.println(name);
  }
}

void loop()
{
  // nothing happens after setup finishes.
}

代码运行,输出结果:
[attach]1023[/attach]
为了方便结果展示,我将 删除文件代码 注释掉(一定要注释掉才有下面的结果),然后一个个名称测试;再用上面的查看列表程序输出结果:
测试一,名称及后缀字符数量
● 文件名称: read.txt、readfile.txt 、readthefile.txt、12345678.txt、123456789.txt

[attach]1024[/attach]
从输出的文件列表中可以看出:readthefile.txt、和 123456789.txt 文件都创建失败!
● 后缀名称: read.c、read.dat、read.exe、picture.png、picture.psd、picture.jpeg、picture.ILBM(位图图形文件)
[attach]1025[/attach]

从输出的文件列表中可以看出:picture.jpeg、和 picture.ILBM 文件创建失败!
用读卡器查看卡内文件:
[attach]1026[/attach]

测试二,特殊符号文件名(不违反第一条规则情况下)
电脑上当试图创建、 保存,或重命名文件、 文件夹或快捷方式时,也有些特殊符号无法使用,如图:
[attach]1019[/attach]
这些符号跟系统符号有冲突,所以无法使用
查找资料得出:
有效的命名文件、 文件夹,或快捷方式字符包括:
       字母 (A-Z) 和数字 (0-9) 以及下列特殊字符的任意组合:      

下面对其中有可能用到的符号进行测试,有兴趣可以全部测试下:
●  re ad.txt(空格)、 re_ad.txt、re-ad.txt、re#ad.txt、re%ad.txt
[attach]1029[/attach]
除了空格没有成功创建,其他4个都可以使用!其他符号在这里就不做测试了。

---------------------------------------------------------------------------------------------------------------------------------

ps:网上还看到说SD库貌似读取大文件不稳定,文件大小最好保持在300kb左右!没有实际测试,遇到的注意下就可以了。








欢迎光临 YFROBOT创客社区 (http://www.yfrobot.com.cn/) Powered by Discuz! X3.1