Arduino SD Fat Library OpenNext错误

时间:2017-06-13 14:48:26

标签: c++ arduino

我正在使用Arduino SD胖库在SD卡上查找文件。我正在向函数发送指向char数组和对象SdFile的指针作为模板,我将代码基于以下示例:

const uint8_t SD_CS_PIN = 10;

SdFat sd;
SdFile file;
SdFile dirFile;

// Number of files found.
uint16_t n = 0;

// Max of ten files since files are selected with a single digit.
const uint16_t nMax = 10;

// Position of file's directory entry.
uint16_t dirIndex[nMax];

// List files in root directory.
while (file.openNext(&dirFile, O_READ)) 
{
  // Skip directories and hidden files.
  if (!file.isSubDir() && !file.isHidden())
  {
    // Save dirIndex of file in directory.
    dirIndex[n] = file.dirIndex();
    // Print the file number and name.
    Serial.print(n++);
    Serial.write(' ');
    file.printName(&Serial);
    Serial.println();
  }
  file.close();
}

下面是我写的代码,当loc.openNext(&dir, O_READ)对我的逻辑进行一些解释时,它没有返回true。

开始

功能回声用户输入

bool FindFile (SdFile dir, char *findname)
{
  Serial.println("getting file");
  const char *p;
  p = findname;
  while (*p) 
  {
    Serial.print(*p);
    p++;
  }
  Serial.println();
  Serial.println();
  bool found = false;

检查文件名是否在根目录

(这部分有效)

  if(dir.exists(findname))
  {
    ActiveDir=dir;
    found = true;
    Serial.println("found");
  }

递归搜索

如果findname不在dir中,请查找第一个子目录,检查并继续直到找到或检查所有子目录。该功能还会跳过任何隐藏的内容,因为我不希望用户能够选择隐藏文件。

  else
  {
    //debug marker
    Serial.println("else");
    SdFile loc;
    while (loc.openNext(&dir, O_READ))
    {
      //while loop is always returning false so this part is not running when it should
      //debug marker
      Serial.println("while");
      if (loc.isHidden())
      {
      //file hidden, skip
      }
      else if (loc.isSubDir())
      {
        Serial.println("SUB");
        loc.open(&dir, loc.dirIndex(), O_READ);
        if(FindFile(loc, findname))
        {
          found = true;
          break;
        }
      }
    }
    loc.close();
  }
return found;
}

对于我的生活,我无法弄清file.openNext(&dirFile, O_READ)loc.openNext(&dir, O_READ)的区别

编辑:更新代码以显示更多示例代码。

0 个答案:

没有答案