Reading variables from a local file

Dear Jan,

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.

Regards
Nada

Hello,

I do not know what exactly did you tried but

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.

Do you still using MbedOS 5.15?

Try to look to the KVStore - APIs | Mbed OS 5 Documentation instead.

BR, Jan

Hello Jan,

I’m trying this

#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.

Yes I’m still using mbed_os 5

in the remp.txt I wrote
192.168.3.60 Pinning

Regards
Nada

And your board is?

BR, Jan

Lpc1768 later I will try it in the UBLOx c027

Thank you Jan,

It’s working now.

Regards
Nada

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?

BR, Jan

Actually I’m not sure.

I thought as UBLOX is based on the LPC1768 so it will work.

I will try and see.

Thank you so much
Regards
Nada

For Ublox do no forgot to add to mbed_app.json content below

{
    "target_overrides": {
        "*":{
            "target.device_has_add": ["LOCALFILESYSTEM"]
        }
    }
}

BR, Jan

Thank you Jan,

Should I create this file manually?

I need also to ask you if I can assign a value to the MAC default value in the platforms- sources - mbed_interface.c from a file or xml file.?

Please, your advise

Regards
Nada

Hello,

If you have access to the memory via USB, then probably yes.

I do not know it depends when the function for setting a MAC is called.

BR, Jan

Thank you Jan.

I will try and see.

Regards
Nada

Hello Jan how are you

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 &amp; 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
image
before commenting the demostart the result were as the following
image

I don’t know what the difference if I have a const char* and if I read the file and copy it to char*

Please your advise

Regards
Nada

Hello Jan, what do u mean if I have the access to the memory from the USB ???

Regards
Nada

Hello,

I do not understand what you mean with the variable.
You asked about create the file manually. How did you want to create the file manually ?

I do not know how many informations do you want to hold but from my point of view the xml file seems to be like overkill solution for few strings.

BR, Jan

Hello Jan,

I meant where I can find the mbed_app.json? if it’s not in my program path, should I create a new file and name it mbed_app.jason ?

Regards
Nada

Yes, if you not already have it in the project then you must to create new one.

BR, Jan

Thank you just I wanted to be sure.

Regards
Nada

Hello Jan,

I hope you are doing well.

I added mbed_app.json to the my project psth on the UBLOx c_027 as te picture below:
image
and I wrote inside it

{
    "target_overrides": {
        "*":{
            "target.device_has_add": ["LOCALFILESYSTEM"]
        }
    }
}

and I got the following error
Error: Use of undeclared identifier ‘semihost_open’ in “mbed-os/platform/source/LocalFileSystem.cpp”, Line: 107, Col: 21

when I checked the error, all the responses mentioned that the localfile is not supported in the CMSIS-DAP even after I added the json file.

Also when I wrote in the mbed_app.json the following

{
    "target_overrides": {
        "*":{
            "target.device_has_add": ["LOCALFILESYSTEM"],
              "target.device_has_add": ["SEMIHOST"]
        }
    }
}

I got also an error in the mbed_interface.cpp

Please your support.

Regards
Nada

Hello,

and arror is?

BR, Jan