1

我尝试使用Auduino Uno按行ID从/向存储在SD卡中的txt文件读取/写入。但我不成功。请帮我。谢谢

4

4 回答 4

4

您可以使用

if (myFile) {
 while (myFile.available()) {
  String line = myFile.readStringUntil('\n');
 }
}
于 2015-09-11T05:11:40.533 回答
1

不确定你是否仍然需要帮助,但这里有。

第一个建议 - 更改 login.txt

尝试在您的 ID 之间插入“\r\n”,而不是使用 br 标记。例如,如果您通过 PHP+SQL 数据库查询创建 login.txt,并将结果写入文件,请将代码中的 br 替换为“\r\n”。或者,如果您能够打开和编辑 login.txt,请执行此操作,并将每个 br 替换为新行。

然后,您所要做的就是一一读取文件中的每一行。

#include <SD.h>
#include <SPI.h>

int CS_PIN = 10;
char logfile[] = {"login.txt"};

File file;

void setup() {

  Serial.begin(9600);
  initializeSD();

  openFile(logfile);
  while(file.available()) {
    // print the lines to serial monitor
    Serial.println(readLine());
  }
  closeFile();
}
void loop() {
}

void initializeSD() {
  Serial.println("Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);

  if (SD.begin()) {
    Serial.println("SD card is ready to use.");
  } else {
    Serial.println("SD card initialization failed");
    return;
  }
}

void closeFile() {
  if (file) {
    file.close();
    Serial.println("File closed");
  }
}

int openFile(char filename[]) {
  file = SD.open(filename);
  if (file) {
    Serial.println("File opened with success!");
    return 1;
  } else {
    Serial.println("Error opening file...");
    return 0;
  }
}

String readLine() {
  String received = "";
  char ch;
  while (file.available()) {
    ch = file.read();
    if (ch == '\n') {
      return String(received);
    } else {
      received += ch;
    }
  }
  return "";
}

这是此YouTube 链接上发布的代码的缩短、未经测试且稍加修改的版本(请参阅视频说明以获取完整代码的链接)。此外,请务必仔细检查接线,如视频所示。

第二个建议 - 改变读取传入字符的方式

如果您无法更改 login.txt 文件的结构,并且只能从 SD 卡中读取,请尝试使用以下代码从文件中读取(第一个建议中的修改版本):

String readLine() {
  String received = "";
  char ch;
  while (file.available()) {
    ch = file.read();
    if (ch == '<') {            // Is the tag starting?
      return String(received);  // Return what you have so far (ID)
    } else if(ch == '>') {      // Is the tag ending?
      received = "";            // Empty the string, we don't need br
    } else {                    // Everything's OK, append to the string
      received += ch;
    }
  }
  return "";
}   

请注意,这两个建议都不会将行存储到单独的变量中。我建议您在阅读文件时对文件中的每一行执行您想做的事情(打印到串行、闪烁 LED 等)。

于 2016-11-18T10:49:08.990 回答
0

请从Arduino > Examples > SD > ReadWrite开始:

/*
  SD card read/write

 This example shows how to read and write data to and from 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 <SPI.h>
#include <SD.h>

File myFile;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  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.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

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

代码应该可以工作。确保您的引脚连接正确。

于 2014-09-15T11:21:51.450 回答
0

Tavinson 的 Answare 是正确的

if (myFile) {
 while (myFile.available()) {
  String line = myFile.readStringUntil('\n');
 }
}
于 2019-03-02T07:04:11.903 回答