【问题标题】:How can I create a text file on an SD card mounted on an Arduino Yun?如何在安装在 Arduino Yun 上的 SD 卡上创建文本文件?
【发布时间】:2018-01-02 03:46:54
【问题描述】:

正如标题所说,我正在尝试从我的 Arduino 草图创建一个文本文件。我的 Arduino 通过以太网电缆连接到我的计算机,但 SD 卡安装在 Arduino 上,所以我假设与计算机的连接(USB 或以太网)无关紧要。我在 official Arduino documentation 上找到的内容似乎很简单,但我无法创建文件。

这是我的代码:

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

File myFile;

void setup() {
  // Start using the Bridge.
  Bridge.begin();
  // To output on the Serial Monitor
  Console.begin();
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  if (!SD.begin(4)) {
    Console.println("initialization failed!");
    //return;
  }
  Console.println("initialization done.");
  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    Console.print("Writing to test.txt...\n");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Console.print("done.\n");
  } else {
    // if the file didn't open, print an error:
    Console.print("error opening test.txt\n");
  }
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Console.print("test.txt: ");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Console.print(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Console.print("error opening test.txt\n");
  }
}

void loop() {
  Console.print("Loop\n");
  delay(1000);
}

这是输出:

initialization failed!
initialization done.
error opening test.txterror opening test.txt
Loop
[...]
Loop

与我上面提供的链接相反,我使用Console 而不是Serial 打印语句。其余的都是一样的。而且我显然已经安装了 SD 卡。

有人知道吗?

【问题讨论】:

  • 你格式化SD了吗?属于哪一种?
  • 我做了,它是 FAT32。
  • 我也已经用它来更新 Arduino 的软件,在 ssh 进入 Arduino 之后,我可以看到目录 /mnt/sda1/ 仍然包含该文件。我什至可以使用 nano 从终端创建文件,因此 SD 卡肯定运行良好。
  • 尝试在 SD 中创建一个文件夹以及文件夹/test.txt 的路径。也许有一些具有root权限的东西,因为您已经将它与更新文件一起使用,idk

标签: arduino text-files sd-card arduino-yun


【解决方案1】:

正如 gre_gor 在 cmets 中所建议的那样,我专门针对 Arduino Yun 遵循了本教程:https://www.arduino.cc/en/Tutorial/YunDatalogger。我必须更改 SD 卡上文件的路径(我的是 '/mnt/sda1/myFile.txt'),我必须在设置函数中删除这两行:

// Delete these two lines:
while (!SerialUSB); // wait for Serial port to connect.
SerialUSB.println("Filesystem datalogger\n");

它奏效了。

【讨论】:

    猜你喜欢
    • 2018-05-18
    • 1970-01-01
    • 2016-04-18
    • 2023-03-13
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多