I created a file called setup.txt on the mbed local file and I save these variable to it:
ip 192.168.3.60
name Pin
I want to retrieve these variable from the file each time the mbed restarted , that’s mean when I change anything in the file , my variable in my program will change in the next reset.
I tried reading from the local file system but it didn’t work.
Could you please provide me with an example for the same variables I need.
LocalFileSystem - The LocalFileSystem is a symbiotic file system that connects to the Mbed board’s interface chip if the interface chip has built-in storage. The features available on the LocalFileSystem is dependent on the board’s interface chip. Note: The LocalFileSystem is only available on the LPC1768 and LPC11U24.
#include "mbed.h"
LocalFileSystem local("local"); //File System
int main()
{
char ip_address[15];
char name[15];
// update config file
FILE * fp = fopen("/local/config.txt", "w"); // Open "config.txt" on the local file system for writing
FILE *tmp = fopen("/local/tmp.txt", "r"); // Open "temp.txt" on the local file system for read
if (tmp == NULL) {
error("Could not open file");
}
while (!feof(tmp)) {
int n = fscanf(tmp, "%s\t%s\n",
&ip_address, &name);
if (n != 2) {
error("Could not read 12 elements");
}
fprintf(fp, "%s\t%s\n",
ip_address, name);
}
fclose(fp);
fclose(tmp);
}
But I have issue with config.txt it showing me errors when I tried to open it.
I do not have LPC1768 so can not test on real hardware. Also I not see anything wrong in your code.
But anyway I think it is waste of time. C027 is based on LPC1768 but the LPC1768 board has a addition IC memory for that feature. So do you are sure C027 also has it?
I had a program to creat an xml and then parse the data. I edited the program to be as the following
#include "mbed.h"
#include "tinyxml.h"
#include <string>
//See http://www.grinninglizard.com/tinyxml/ for more information on how to use tinyxml.
//See TINYXML Library for license.
//
//TODO Document library and app in embed style.
DigitalOut myled(LED1);
int main() {
//Setup Tx, Rx.
Serial usb(USBTX, USBRX);
usb.baud(115200);
// Create the local filesystem under the name "local".
// DO NOT FORGET THIS CALL OR SAVING WON'T WORK!
LocalFileSystem local("local");
// Create some Xml to experiment on.
/* const char* demoStart =
"<?xml version=\"1.0\" standalone='no' >\r\n"
"<!-- Our to do list data -->\r\n"
"<ToDo>\r\n"
"<!-- Do I need a secure PDA? -->\r\n"
"<Item priority=\"1\" distance='close'> Go to the <bold>Toy store!</bold></Item>\r\n"
"<Item priority=\"2\" distance='none'> Do bills </Item>\r\n"
"<Item priority=\"2\" distance='far & back'> Look for Evil Dinosaurs! </Item>\r\n"
"</ToDo>\r\n";*/
FILE *fp = fopen("/local/demotest.xml", "r");
char buffer[128];
char* demoStart = new char[200] ;
while(fgets(buffer, 128, fp))
{
//memcpy(demoStart,buffer,sizeof(buffer));
strcat(demoStart,buffer);
}
//usb.printf("demostart,%s\n\r",demoStart);
/*"<?xml version=\"1.0\" standalone='no' >\r\n"
"<!-- Our Pinning Station Data -->\r\n"
"<Data>\r\n"
"<Item ip_address=1 > 192.168.3.80 </Item>\r\n"
"<Item Name_P =1> Pinning_001 </Item>\r\n"
"</Data>\r\n";*/
// Load Xml into a XmlDocument.
TiXmlDocument doc("/local/demotest_1.xml");
//doc.LoadFile("/local/demotest.xml");
/*usb.printf("Printing XML:\r\n");
TiXmlPrinter printer;
printer.SetLineBreak("\r\n"); //Default is "\n".
doc.Accept(&printer);
usb.printf("%s\r\n", printer.CStr());
const char* demoStart = printer.CStr();*/
// Parse Xml.
doc.Parse(demoStart);
// Get the tagname of the Root Element (ToDo).
// Note: doc.RootElement()->Value().c_str() is not compatible with printf! E153
string tag = doc.RootElement()->Value();
usb.printf("RootElement = <%s>\r\n\r\n", tag.c_str());
// Iterate Elements with for loop.
usb.printf("Enumerating Item Child Tags:\r\n");
for (TiXmlNode* child = doc.RootElement()->FirstChild(); child; child = child->NextSibling() ) {
if (child->Type()==TiXmlNode::TINYXML_ELEMENT) {
tag = child->Value();
//usb.printf(child->Value());
usb.printf(" Child = <%s>\r\n", tag.c_str());
usb.printf(" Text = '%s'\r\n\r\n", child->ToElement()->GetText());
string b= child->ToElement()->GetText();
if (strcmp(child->ToElement()->GetText(), "Pinning_001") == 0)
{
usb.printf("hello\n\r");
}
}
}
//Walk Elements by FirstChildElement/NextSiblingElement.
usb.printf("Walking Item Child Tags:\r\n");
TiXmlElement* child = doc.RootElement()->FirstChildElement("Item");
do {
tag = child->Value();
usb.printf(" Child = <%s>\r\n", tag.c_str());
usb.printf(" Text = '%s'\r\n\r\n", child->ToElement()->GetText());
child = child->NextSiblingElement("Item");
} while (child);
/* //Save to a file on the /local/ filesystem on Usb.
usb.printf("Saving with filename:\r\n");
doc.SaveFile("/local/out_1.xml");
//Save to a file on the /local/ filesystem on Usb.
usb.printf("Saving with FILE:\r\n");
FILE *fp = fopen("/local/out_2.xml", "w");
doc.SaveFile(fp);
fclose(fp);*/
if ( doc.Error() ) {
usb.printf( "Error in %s: %s\n", doc.Value(), doc.ErrorDesc() );
exit( 1 );
}
/*//Print the Parsed Xml.
usb.printf("Printing XML:\r\n");
TiXmlPrinter printer;
printer.SetLineBreak("\r\n"); //Default is "\n".
doc.Accept(&printer);
usb.printf("%s\r\n", printer.CStr());*/
while (1) {
//Wait...
}
}
I just commented the const demostart* , I wrote the const demostart* into the xml file, read the file and then copy the buffer to demostart.
When I parse the data , the result will be as the following
before commenting the demostart the result were as the following
I don’t know what the difference if I have a const char* and if I read the file and copy it to char*