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



Arduino IDE中的示例" sd_test"效果很好。现在我想扩展它。

first :我想使用一个变量文件名。我在Internet中找到了一些示例,也在Stackoverlow中找到了一些示例,但没有任何效果(仍在寻找最小的示例(

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

我想要

writeFile(SD, filename, "Hello ");

fileName是一个变量,可以处理诸如file.txt"

之类的变量。

second :我想为内容做同样的事情,我想保存在此文件中。所以而不是

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

我想要

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

作为一个例子:我可以打印出此

        printf("%04x", datas);

现在,我想在文件中使用4个十六进制保存此变量"数据",正是在串行监视器中的外观。

您可以使用以下代码在SD卡上编写。您需要在代码中提及此名称的两个库文件。记住要格式化您的SD卡首先为FAT32希望这对您有帮助。

/*  Author: Ashish Kumar
    Org: INVOOTECH                            */
#include <SPI.h>
#include <SD.h>
Sd2Card card;
SdVolume volume;
File myFile;
 char *b="hellow.txt";
 char p;
const int chipSelect = 4;
void setup() {
Serial.begin(9600);
 if (!card.init(SPI_HALF_SPEED, chipSelect))   //error statement
  {
    Serial.println("initialization failed. Things to check:");
    return;
  }
  if (!volume.init(card))           //error statement
  {
    Serial.println("Could not find FAT16/FAT32 partition.nMake sure you've formatted the card");
    return;
  }
    if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
}
void loop() {
  myFile = SD.open(b);
if (myFile) {
      p=myFile.read();
      Serial.write(p);
    }
    // close the file:
    myFile.close();
}

好吧。所以我做的是在循环中创建一个文件。

#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);
}

希望这能解决您的问题。谢谢。

最新更新