2017-07-26 102 views
0

就像標題所說,我試圖從我的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卡。

任何想法的人?

+0

你有沒有格式化SD?在哪種類型? – George

+0

我做了,它是FAT32。 – BourbonCreams

+0

我也已經用它來更新Arduino的軟件,並且在ssh進入Arduino之後,我可以看到目錄/ mnt/sda1 /仍然包含該文件。我甚至可以使用nano創建從終端創建文件,因此SD卡一定能夠很好地工作。 – BourbonCreams

回答

0

正如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"); 

和它的工作。

相關問題