Store in and retrieve variables from file on local mbed Microcontroller USB disk drive

30 Oct 2010

I like to store the values of different variables persistently in a file on the local mbed Microcontroller USB disk drive and retrieve these values after a reset.

To clarify:

- I have a file on the local mbed Microntroller disk drive with the name 'Variables.txt'.
- In this file I like to store some variables that are calculated in my program (based on sensor data). For example: VARIABLE_A, VARIABLE_B, VARIABLE_C
- After a reset I like to retrieve the values corresponding to these variables so that I can access them again in my program.
- I also want to update the values in the file.

Can anyone tell me how this can be accomplished?

Thanks!

 

 

31 Oct 2010 . Edited: 31 Oct 2010

Take a look at LocalFileSystem. For one sensor you can do something like this, didin't test it so check syntax.

 

AnalogIn sensor(p20);

int main() {
var = sensor.read();
    FILE *fp = fopen("/local/Variables.txt", "w");  // Open "out.txt" on the local file system for writing

//store ten readings
for(int i=0;i < 10;i++){    
fprintf(fp, "Sensor 1\n");
fprintf(fp,"%f",var1);
}
 fclose(fp);
}
31 Oct 2010

Thank your for your reply!

I am already familiair with working with the LocalFileSystem and storing and retrieving the value of a single variable in a file isn't the problem.

What I want to do is store the values of multiple variables in a single file and retrieve these values after performing a reset.

 

31 Oct 2010

I don't see any big difference.For storing multiple variables you can create multiple AnalogIn objects and write something like:

fprintf(fp,"%f\t%f\t%f",var1,var2,var3);

I am not sure if it's posible to find the file after a reset. I assume that it gets overwritten every time the program runs. In that case your best solution is to use an SD card.

31 Oct 2010 . Edited: 31 Oct 2010

The storage of the values of the variables is for me absolutely useless if I can't retrieve the values after a reset.

Why wouldn't it be possible to find the file after a reset?

What I try to accomplish is:

1. Retrieve the values of three different variables from a file on the local mbed Microcontroller USB disk drive.
2. Next, use these variabeles in my program. And when neccesary update the values of the variabeles in the file on the local mbed Microcontroller USB disk drive. (so persistenly store the data)
3. Power-down the LPC1768.
4. Power-up the LPC1768 and retrieve the values of the three variabeles (just like step 1) from the file.

I know how to simply write values to a file, but I don't know how to parse a file to retrieve the values of the variables.

 

 

 

31 Oct 2010

Well in that case just add an if statement to see if the file exists in the local FS. If it exists then use a switch statement to open it , read it and process it , otherwise go to the default case of opening a new file I guess. Use the fscanf() function to parse the file, it very similar to fprintf(). Also you may need a buffer class or array. I can't say anything more without looking at your code. Hope that helps.

31 Oct 2010

I think fscanf() is what I am looking for, thanks! I'm going to read some information about using it.

03 Nov 2010

I believe fopen has the options you are looking for. See here.

If you use the "a" option, fopen opens the file and anything you write to the file gets appended to it, (i.e. is written at the end of the existing file). If the file doesn't exist, it is created.

To check if the file contains data, you can fopen and read the file for EOF and proceed from there. You can also use fseek, rewind, and fsetpos to manipulate the current position (i.e. cursor) within the file.

Hope that helps!