【问题标题】:How to create variable filename and store variable on sd card with Arduino如何使用 Arduino 创建变量文件名并将变量存储在 sd 卡上
【发布时间】:2017-06-29 21:50:58
【问题描述】:

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 个十六进制的变量“数据”,它在串行监视器中的样子。

【问题讨论】:

    标签: arduino android-sdcard


    【解决方案1】:

    您可以使用以下代码在 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();
    
    }
    

    【讨论】:

    • 错误:Sd2Card 卡未命名类型/SdVolume 卷未命名类型
    • 能否分享一下 sd 卡模块与 arduino uno 的连接,以便我更好地为您提供帮助。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 2023-03-16
      • 2021-05-01
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 2011-08-23
      相关资源
      最近更新 更多