关于官方SD库,打开一个文件后,再索引目录会从打开的文件后面开始索引,前面的没有了
本帖最后由 yifabin023 于 2017-7-29 19:15 编辑关于官方SD库,打开一个文件后,再索引目录会从打开的文件后面开始索引,前面的没有了,不知道哪里没弄对,弄不出来了。大家帮忙看看。
程序大概流程就是:读取目录并且编号,通过串口显示出来,然后等待串口输入目录号,打开文件并读取。读完后再索引目录。
比如我有20个文件在卡里,我读取了第15号文件,再出来的目录就只有15号后面的文件了,前面的15个没有了。
/*
Listfiles
This example shows how print out the files in a
directory on a SD card
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
modified 2 Feb 2014
by Scott Fitzgerald
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
//const uint8_t CS_IO = 10;
#define CS_IO10 //cs_io
File root;
String mun_sd;
String mun_sd_size ;
int SetPoint = 0;
const char *c_neme ;
String c_neme2 ;
char b;
//------------------------------------------------------------------
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(CS_IO)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
////////////----------------------------------------------------------
void loop()
{
b = 1;
root = SD.open("/");
printDirectory(root, 0 );//读sd文件目录
// root.rewindDirectory();
// root.close();
Serial.println("done!");
while (1)
{
chuan();
if (SetPoint) break;
}
b = 0;
root = SD.open("/");
printDirectory(root, 0);
Serial.println(SetPoint);
Serial.println(c_neme);
Serial.println("\r");
SetPoint = 0;
delay(1000);
c_neme = c_neme2.c_str();
open_sd(c_neme);
root.close();
delay(1000);
}
int xuhao = 0;
void printDirectory(File dir, int numTabs)
{
char ki = 30;
while (ki)
{
ki--;
File entry =dir.openNextFile();
if (!entry) {//读完所有文件名,跳出
//entry.rewindDirectory();
//break;
}
if (b == 0)
{
if (SetPoint == xuhao)
{
c_neme2 = mun_sd;
}
}
xuhao++;//文件序号
Serial.print(xuhao);
Serial.print(".");
mun_sd = entry.name();//读取文件名
Serial.print(mun_sd);
mun_sd_size = entry.size();
Serial.print("\t\t");
Serial.println(mun_sd_size);
entry.close();
//root.close();
}
root.close();
xuhao = 0;
}
void open_sd(const char *neme )
{
//打开文件。 请注意,一次只能打开一个文件,因此在打开另一个文件之前必须先关闭此文件。
File dataFile = SD.open(neme);
if (dataFile) { // 如果文件可用,请写入:
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
dataFile.rewindDirectory();
return;//退出
}
else {//如果文件未打开,弹出错误:
dataFile.close();
Serial.println("error opening ");
}
}
void chuan()// 串口转变量
{
char *comdata;
String c = "";
boolean mark = 0;
while (Serial.available() > 0)
{
c += char(Serial.read());
delay(2);
mark = 1;
}
if (mark == 1)
{
comdata = &c;
SetPoint = atof(comdata);
mark = 0;
c = String("");
// return (atof(comdata));
}
}
同问 ??????????????????? 没毛病啊,不就应该从新文件里索引吗
页:
[1]