YFROBOT创客社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 8314|回复: 1
打印 上一主题 下一主题

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

[复制链接]

签到天数: 866 天

[LV.10]以坛为家III

跳转到指定楼层
楼主
发表于 2015-7-22 08:17:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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库说明截图:


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


将下面的名称测试程序代码烧写到控制板中,改变名称,测试打开文件是否成功:
[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.
}

代码运行,输出结果:

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


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


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


测试二,特殊符号文件名(不违反第一条规则情况下)
电脑上当试图创建、 保存,或重命名文件、 文件夹或快捷方式时,也有些特殊符号无法使用,如图:

这些符号跟系统符号有冲突,所以无法使用
查找资料得出:
有效的命名文件、 文件夹,或快捷方式字符包括:
       字母 (A-Z) 和数字 (0-9) 以及下列特殊字符的任意组合:      
  • ^   Accent circumflex (caret)
  • &   Ampersand
  • '   Apostrophe (single quotation mark)
  • @   At sign
  • {   Brace left
  • }   Brace right
  • [   Bracket opening
  • ]   Bracket closing
  • ,   Comma
  • $   Dollar sign
  • =   Equal sign
  • !   Exclamation point
  • -   Hyphen
  • #   Number sign
  • (   Parenthesis opening
  • )   Parenthesis closing
  • %   Percent
  • .   Period
  • +   Plus
  • ~   Tilde
  • _   Underscore

下面对其中有可能用到的符号进行测试,有兴趣可以全部测试下:
●  re ad.txt(空格)、 re_ad.txt、re-ad.txt、re#ad.txt、re%ad.txt

除了空格没有成功创建,其他4个都可以使用!其他符号在这里就不做测试了。

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

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



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|联系我们|YFROBOT ( 苏ICP备20009901号-2  

GMT+8, 2024-5-17 15:39 , Processed in 0.045915 second(s), 27 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表