如何在安装在Arduino Yun上的SD卡上创建文本文件?
问题描述:
就像标题所说,我试图从我的Arduino草图创建一个文本文件。我的Arduino通过以太网电缆连接到我的电脑,但SD卡安装在Arduino上,所以我假设连接到计算机(USB或以太网)并不重要。我在official Arduino documentation上找到的东西看起来非常简单,但我无法创建文件。如何在安装在Arduino Yun上的SD卡上创建文本文件?
这里是我的代码:
#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卡。
任何想法的人?
答
正如gre_gor在评论中所建议的,我遵循本教程专门针对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");
和它的工作。
你有没有格式化SD?在哪种类型? – George
我做了,它是FAT32。 – BourbonCreams
我也已经用它来更新Arduino的软件,并且在ssh进入Arduino之后,我可以看到目录/ mnt/sda1 /仍然包含该文件。我甚至可以使用nano创建从终端创建文件,因此SD卡一定能够很好地工作。 – BourbonCreams