极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13159|回复: 2

关于官方SD库,打开一个文件后,再索引目录会从打开的文件后面开始索引,前面的没有了

[复制链接]
发表于 2017-7-29 18:58:21 | 显示全部楼层 |阅读模式
本帖最后由 yifabin023 于 2017-7-29 19:15 编辑

关于官方SD库,打开一个文件后,再索引目录会从打开的文件后面开始索引,前面的没有了,不知道哪里没弄对,弄不出来了。大家帮忙看看。
程序大概流程就是:读取目录并且编号,通过串口显示出来,然后等待串口输入目录号,打开文件并读取。读完后再索引目录。
比如我有20个文件在卡里,我读取了第15号文件,再出来的目录就只有15号后面的文件了,前面的15个没有了。

  1. /*
  2.   Listfiles

  3.   This example shows how print out the files in a
  4.   directory on a SD card

  5.   The circuit:
  6.    SD card attached to SPI bus as follows:
  7. ** MOSI - pin 11
  8. ** MISO - pin 12
  9. ** CLK - pin 13
  10. ** CS - pin 10

  11.   created   Nov 2010
  12.   by David A. Mellis
  13.   modified 9 Apr 2012
  14.   by Tom Igoe
  15.   modified 2 Feb 2014
  16.   by Scott Fitzgerald

  17.   This example code is in the public domain.

  18. */


  19. #include <SPI.h>
  20. #include <SD.h>
  21. //const uint8_t CS_IO = 10;
  22. #define CS_IO  10    //cs_io
  23. File root;

  24. String mun_sd;
  25. String mun_sd_size ;


  26. int SetPoint = 0;
  27. const char *c_neme ;
  28. String c_neme2 ;

  29. char b;

  30. //------------------------------------------------------------------
  31. void setup() {
  32.   // Open serial communications and wait for port to open:
  33.   Serial.begin(115200);
  34.   while (!Serial) {
  35.     ; // wait for serial port to connect. Needed for native USB port only
  36.   }

  37.   Serial.print("Initializing SD card...");

  38.   if (!SD.begin(CS_IO)) {
  39.     Serial.println("initialization failed!");
  40.     return;
  41.   }
  42.   Serial.println("initialization done.");
  43. }

  44. ////////////----------------------------------------------------------
  45. void loop()
  46. {
  47.   b = 1;
  48.   root = SD.open("/");
  49.   printDirectory(root, 0 );//读sd文件目录
  50.   // root.rewindDirectory();
  51.   // root.close();
  52.   Serial.println("done!");
  53.   while (1)
  54.   {
  55.     chuan();
  56.     if (SetPoint) break;
  57.   }
  58.   b = 0;

  59.   root = SD.open("/");
  60.   printDirectory(root, 0);
  61.   Serial.println(SetPoint);
  62.   Serial.println(c_neme);
  63.   Serial.println("\r");
  64.   SetPoint = 0;
  65.   delay(1000);
  66.   c_neme = c_neme2.c_str();
  67.   open_sd(c_neme);
  68.   root.close();
  69.   delay(1000);
  70. }




  71. int xuhao = 0;
  72. void printDirectory(File dir, int numTabs)
  73. {
  74.   char ki = 30;
  75.   while (ki)
  76.   {
  77.     ki--;
  78.     File entry =  dir.openNextFile();
  79.     if (!entry) {  //读完所有文件名,跳出
  80.       //entry.rewindDirectory();
  81.       //break;
  82.     }
  83.     if (b == 0)
  84.     {
  85.       if (SetPoint == xuhao)
  86.       {
  87.         c_neme2 = mun_sd;
  88.       }
  89.     }
  90.     xuhao++;//文件序号
  91.     Serial.print(xuhao);
  92.     Serial.print(".  ");
  93.     mun_sd = entry.name();//读取文件名
  94.     Serial.print(mun_sd);
  95.     mun_sd_size = entry.size();
  96.     Serial.print("\t\t");
  97.     Serial.println(mun_sd_size);
  98.     entry.close();
  99.     //root.close();
  100.   }
  101.   root.close();
  102.   xuhao = 0;
  103. }

  104. void open_sd(const char *neme )
  105. {
  106.   //打开文件。 请注意,一次只能打开一个文件,因此在打开另一个文件之前必须先关闭此文件。
  107.   File dataFile = SD.open(neme);
  108.   if (dataFile) {   // 如果文件可用,请写入:
  109.     while (dataFile.available()) {
  110.       Serial.write(dataFile.read());
  111.     }
  112.     dataFile.close();
  113.     dataFile.rewindDirectory();
  114.     return;//退出
  115.   }
  116.   else {  //如果文件未打开,弹出错误:
  117.     dataFile.close();
  118.     Serial.println("error opening ");
  119.   }
  120. }



  121. void chuan()// 串口转变量
  122. {
  123.   char *comdata;
  124.   String c = "";
  125.   boolean mark = 0;
  126.   while (Serial.available() > 0)
  127.   {
  128.     c += char(Serial.read());
  129.     delay(2);
  130.     mark = 1;
  131.   }
  132.   if (mark == 1)
  133.   {
  134.     comdata = &c[0];
  135.     SetPoint = atof(comdata);
  136.     mark = 0;
  137.     c = String("");
  138.     // return (atof(comdata));
  139.   }
  140. }


复制代码


本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2017-7-31 10:35:49 | 显示全部楼层
没毛病啊,不就应该从新文件里索引吗
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-30 18:52 , Processed in 0.047924 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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