如何使用Arduino在SD卡上创建变量文件名和存储变量

时间:2017-06-29 21:50:58

标签: arduino android-sdcard

Arduino IDE中的示例“SD_Test”完美运行。现在我想扩展它。

首先:我想使用变量文件名。我在互联网上找到了一些例子,也在stackoverlow中找到了这个例子,但没有任何作用(仍在寻找一个最小的例子)

writeFile(SD, "/hello.txt", "Hello ");

我想要

writeFile(SD, filename, "Hello ");

其中filename是一个处理“file.txt”

之类的变量

第二次:我想对内容做同样的事情,我希望将其保存在此文件中。而不是

writeFile(SD, "/hello.txt", "Hello ");

我想要

writeFile(SD, "/hello.txt", datas);

举个例子:我可以打印出这个

        printf("%04x", datas);

现在我想在文件中保存带有4个十六进制的变量'datas',它在串行监视器中的确切显示方式。

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码在SD卡上书写。 您需要下载两个库文件,这些名称在代码中提到。 请记住先将SD卡格式化为FAT32 希望这会对你有所帮助。

Manifest-Version: 1.0
Built-By: user
Build-Jdk: 1.7.0_80
Class-Path: lib/json-20170516.jar lib/mysql-connector-java-5.1.42.jar 
 lib/log4j-1.2.17.jar lib/slf4j-api-1.7.21.jar lib/opencsv-3.9.jar lib
 /commons-lang3-3.5.jar lib/commons-beanutils-1.9.3.jar lib/commons-lo
 gging-1.2.jar lib/commons-collections-3.2.2.jar
Created-By: Apache Maven 3.0.5
Main-Class: com.myapp.App

答案 1 :(得分:0)

好的。所以我要做的是循环创建文件。

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

File myFile;

int chipSelectNumber = 10;
String prefix = "test";
String extension = ".txt";
String filename;
int fileNumber=0;
void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }  
  initSDCard();
}
void loop() {
  createFileAndEnterData();  

}

//This Function will create file and Enter modbus data in the file
void createFileAndEnterData(){

 String temp;
 temp = prefix;
 temp.concat(fileNumber);
 temp.concat(extension);
 filename = temp;
 Serial.println(filename);
 fileNumber++;
 boolean statusFileCreate = createFile(filename);
 if(statusFileCreate){
  Serial.println("File Creation Successful");
 }
 else{
  Serial.println("File Creation Unsuccessful");
 }
 delay(1000);
}

boolean createFile(String fileName){
  myFile = SD.open(fileName, FILE_WRITE);
  if(myFile){
    Serial.print("Creating file : ");
    Serial.print(fileName);
    Serial.print(" ");
    myFile.println("");
    myFile.close();
    return true;
  }
  else{
    return false;
  }
}


void initSDCard(){

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelectNumber)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done."); 
}

void openFile(String fileName){ 
  myFile = SD.open(fileName, FILE_WRITE);
  if (myFile) {
    Serial.println("Opened File");
  } else {
    Serial.println("error opening test.txt");
  } 
}
void deleteFile(String filename){

}
void writeFile(String fileName){
    // re-open the file for reading:
  myFile = SD.open(fileName);
  if (myFile) {
    Serial.println(fileName);

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());

    }
    myFile.close();

  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

}

有一些不必要的功能,请避免这种情况。 以下功能将创建文件:-

void createFileAndEnterData(){

 String temp;
 temp = prefix;
 temp.concat(fileNumber);
 temp.concat(extension);
 filename = temp;
 Serial.println(filename);
 fileNumber++;
 boolean statusFileCreate = createFile(filename);
 if(statusFileCreate){
  Serial.println("File Creation Successful");
 }
 else{
  Serial.println("File Creation Unsuccessful");
 }
 delay(1000);
}

希望这可以解决您的问题。 谢谢。

相关问题