简体   繁体   English

使用esp8266解析SD卡数据

[英]SD card data parsing with esp8266

I need some assistance for extracting data from a SD card I based my code from this section.我需要一些帮助来从 SD 卡中提取数据,我基于节中的代码。

The code works when I read the data from the SD card and display it into the serial port, but when I pass the data into an char* array and call a function that will loop the array, the array displays garbage (some unreadable data).当我从 SD 卡读取数据并将其显示到串行端口时,代码有效,但是当我将数据传递到 char* 数组并调用将循环数组的函数时,该数组显示垃圾(一些不可读的数据) . I am trying to make a function which I can use to call different settings stored from SD card in a text file format.我正在尝试创建一个函数,我可以使用该函数调用以文本文件格式从 SD 卡存储的不同设置。

I have a global variable named:我有一个名为的全局变量:

char* tempStoreParam[10]; 

Which will store temporary data to be process.它将存储要处理的临时数据。 The data stored in the text file is in this format文本文件中存储的数据就是这种格式

-n.command

Where: n = int number and index location of the data to be stored in the tempStoreParam[10] and command is a char* array to stored in tempStoreParam[10] .其中: n = 要存储在tempStoreParam[10]中的数据的 int 编号和索引位置,并且 command 是要存储在tempStoreParam[10]的 char* 数组。

Example:示例:

-1.readTempC

-2.readTempF

-3.setdelay:10

-4.getIpAddr

Here is the code snippet:这是代码片段:

while (sdFiles.available()) {
  char sdData[datalen + 1];
  byte byteSize = sdFiles.read(sdData, datalen);
  sdData[byteSize] = 0;
  char* mList = strtok(sdData, "-");
  while (mList != 0)
  {
    // Split the command in 2 values
    char* lsParam = strchr(mList, '.');
    if (lsParam != 0)
    {
      *lsParam = 0;
      int index = atoi(mList);
      ++lsParam;
      tempStoreParam[index] = lsParam;
      Serial.println(index);
      Serial.println(tempStoreParam[index]);
    }
    mList = strtok(0, "-");
  }
} 

I am trying to get the following result:我试图得到以下结果:

char* tempStoreParam[10] = {"readTempC","readTempF","setdelay:10","getIpAddr"};

Your code has a few problems - In order:您的代码有一些问题 - 按顺序:

The return value of read in this instance is a 32 bit integer - You are truncating it to a byte value, meaning if the file contents are ever over 255 bytes, you will incorrectly terminate your string, and fail to read the contents correctly:在此实例中 read 的返回值是一个 32 位整数 - 您将其截断为一个字节值,这意味着如果文件内容超过 255 个字节,您将错误地终止您的字符串,并且无法正确读取内容:

byte byteSize = sdFiles.read(sdData, datalen);

Secondly, you are storing the address of a stack variable into your tempStoreParam array with this line:其次,您使用以下行将堆栈变量的地址存储到tempStoreParam数组中:

tempStoreParam[index] = lsParam;

Now, this will work, but only for how long sdData remains in scope.现在,这将起作用,但仅限于sdData保留在范围内的时间。 After that, sdData is no longer valid to use, most likely leading to the garbage you experience.之后, sdData不再有效使用,很可能会导致您遇到垃圾。 What you most likely are trying to do is take a copy of the data to place it into tempStoreParam .您最有可能尝试做的是获取数据的副本并将其放入tempStoreParam To do that, you should use something like this:要做到这一点,你应该使用这样的东西:

// The amount of memory we need is the length of the string, plus one 
// for the null byte
int length = strlen(lsParam)+1

// Allocate storage space for the length of lsParam in tempStoreParam
tempStoreParam[index] = new char[length];

// Make sure the allocation succeeded 
if (tempStoreParam[index] != nullptr) {
   // Copy the string into our new container
   strncpy(tempStoreParam[index], lsParam, length);
}

At this point, you should be able to pass around that string outside the function successfully.此时,您应该能够成功地在函数外部传递该字符串。 As a note, you will need to delete the array created once you are done with it / before reading the file again.请注意,您需要在完成后delete创建的数组/在再次读取文件之前。

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

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