简体   繁体   English

如何创建 function:使用文件名作为 function 参数从 SD 卡文件中获取单个值

[英]How to create a function: Get a single value from SD card file using the file name as function parameter

I appreciate any guidance or help I can get on this.我很感激我能得到的任何指导或帮助。 I am writing a program with values for a PID stored on an SD card so I can change them on the touchscreen without the need to hook it up to my computer.我正在编写一个程序,其中包含存储在 SD 卡上的 PID 值,因此我可以在触摸屏上更改它们,而无需将其连接到我的计算机。 I want a single function that I can call with parameters allowing me to increase or decrease the number and change the file name.我想要一个 function ,我可以使用参数调用它,允许我增加或减少数字并更改文件名。 The below function is what I have to change whether "pnum.txt" increases or decreases;下面的 function 是我必须更改“pnum.txt”是否增加或减少; however I cannot figure out how to make a File work as a parameter.但是我无法弄清楚如何使文件作为参数工作。

I have tried making " "pnum.txt" " (with quotes) as a char and as a String, and even though it prints out as it should, it doesn't work when inserted into the function.我尝试将“”pnum.txt“”(带引号)作为字符和字符串,即使它按应有的方式打印出来,当插入 function 时它也不起作用。 I have also tried passing the whole SD.open("pnum.txt", FILE_WRITE) and myFile = SD.open("pnum.txt", FILE_WRITE) as a File , but that does something odd - it will create the file when I open it, but it won't write to the file.我也尝试将整个SD.open("pnum.txt", FILE_WRITE)myFile = SD.open("pnum.txt", FILE_WRITE)作为File传递,但这会做一些奇怪的事情 - 它会在我打开它,但它不会写入文件。 I'm finding myself just trying the same things over and over, so I clearly have a lack of understanding that I'm not finding anywhere.我发现自己只是一遍又一遍地尝试同样的事情,所以我显然缺乏理解,我在任何地方都找不到。 Thank you again for any help on this!再次感谢您对此的任何帮助!

float incDecValue(float value) {

        //This is important, because the libraries are sharing pins
        pinMode(XM, OUTPUT);
        pinMode(YP, OUTPUT);

        value;
        SD.remove("pnum.txt");
        myFile = SD.open("pnum.txt", FILE_WRITE);
        myFile.print(value);
        myFile.close();
        counter = 0;
        myFile = SD.open("pnum.txt");
        if (myFile) {
          while (myFile.available()) {
            testChar[counter] = myFile.read();
            counter++;
          }
          myFile.close();
         }
         float convertedValue = atof(testChar);
         return convertedValue;
    }

And I will call it like this.我会这样称呼它。

pValue = incValue(pValue+=.5);

As not exactly knowing what you really want I did the following assumptions:由于不完全知道您真正想要什么,我做了以下假设:

  • You want to save a single float value to a file called pnum.txt您想将单个浮点值保存到名为 pnum.txt 的文件中
  • You want to do something with that value你想用那个价值做一些事情
  • You want the processed value to write back to the file pnum.txt (overwriting the content)您希望将处理后的值写回文件 pnum.txt(覆盖内容)
  • Two different functions parametrized each with fileName as input and the value for write将两个不同的函数参数化,每个函数都使用 fileName 作为输入和 write 的值

So here a complete sequence (works as posted) you could easily implement into your code.因此,这里有一个完整的序列(如发布的那样工作),您可以轻松地将其实现到您的代码中。 No String class is used and its a one line file read/write only:没有字符串 class 被使用,它的单行文件只读/写:

/* SD card pid read/write

  The circuit:
     SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4   */

#include <SPI.h>
#include <SD.h>
const uint8_t chipSelect = 4;
char dataChar[16] = {'\0'};

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed or no card present");
    return;
  }
  Serial.println("Card initialized");
}

void loop() {
  float pValue = readFileValue ("pnum.txt") + 0.5;
  writeFileValue ("pnum.txt", pValue);
}

float readFileValue (const char* fileName) {
  /* Open the file. note that only one file can be open at a time,
    so you have to close this one before opening another.*/
  File dataFile = SD.open(fileName, FILE_READ);
  // if the file is available, write to it:
  if (dataFile) {
    char c;
    uint8_t i = 0;
    while (dataFile.available()) {
      c = dataFile.read();   // Read char by char
      if (c != '\n') {      // As long no line terminator
        dataChar[i] = c;
        i++;
      }
      else {
        dataChar[i] = '\0';  // Terminate char array properly
        dataFile.close();
        break;
      }
    }
    Serial.print("Success writing content: ");
    Serial.println(dataChar);
  }
  else {   // If the file isn't open, pop up an error:
    Serial.print("Error opening requested file: ");
    Serial.println(fileName);
  }
  float fileVal = atof(dataChar);;
  return fileVal;
}

bool writeFileValue (const char* fileName, float fileVal) {
  SD.remove(fileName); // Delete the existing file if existing 
  File dataFile = SD.open(fileName, FILE_WRITE);
  // If the file opened okay, write to it
  if (dataFile) {
    // dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
    dtostrf(fileVal, 5, 2, dataChar);  // Not really needed for this simple issue, but ..
    Serial.print("Writing to file: ");
    Serial.println(fileName);
    dataFile.println(dataChar);
    // Close the file
    dataFile.close();
    Serial.println("Success saving done");
    return true;
  } else {
    // if the file didn't open, print an error:
    Serial.println("Error opening file: ");
    Serial.println(fileName);
    return false;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM