Announcement

Collapse
No announcement yet.

Parameter file on SD card

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parameter file on SD card

    Hi,

    Sorry if my question seems obvious but I'm a total newbie with 4DGL, and more generally with embedded programming and low-level languages.

    I need to be able to store parameters for my program within a PARAM file in my microSD card. The parameters, stored in plain text, will likely be separated by CRLF, but without a fixed length.

    Since the file_Read() function will only allow me to retrieve the whole content of the file at once, how can I retrieve the individual parameters? In my opinion, splitting the read string in substrings cut at each CRLF would be the fanciest solution, but I can't find any documentation about a function which can do that.
    Another way would be to read every parameter in the file one by one, but how can I stop the reading when reaching a CRLF?

    Thank you for your help

  • #2
    Hi,

    Welcome to the forum!


    I made a simple project that is related to your concern. This project demonstrates how to create a PARAM file, reads/displays the contents of the file in the terminal, retrieve the parameter and it is a good start in your case.

    I used ViSi Environment.

    You could try the simple project I have attached, if you have time.
    Also, if you have questions regarding with the project, please don't hesitate to ask.



    Hopefully, this will help you.

    Best regards,
    Kayven
    Attached Files

    Comment


    • #3
      Thank you a lot for your answer, I haven't thought of reading every character one by one. I have two remaining questions:
      - Since my buffer will be an array of characters, I guess I can use the whole array as a string, like [STR]buffer0?
      - The file is never closed by the file_Close() function. Is it on purpose?

      Best regards

      Comment


      • #4
        Hello again,



        Oh! anytime, willing to help you.



        Since my buffer will be an array of characters, I guess I can use the whole array as a string, like [STR]buffer0?
        Unfortunately, it is not possible because the buffer0 is an array of characters and used to stores values by indexes.

        Unless if an array doesn't use indexes to store values as like shown below:

        Code:
        var buffer[5];
        to(buffer);
        putstr("hello ");
        to(APPEND);
         putstr("world");
        
        txt_MoveCursor(7,0);
        print([STR]buffer);
        Thus, you can use "chardata" array to convert "buffer0" array as a string as shown below:
        Code:
        var chardata[20];   // 20 character buffer for  a source string
        var ptr,i;
        
        ptr:=str_Ptr(chardata);
        
         for(i:=0; i<=varCount; i++)
         str_PutByte(ptr + i,buffer0[i]);
        next
        
        txt_MoveCursor(7,0);
        print([STR]chardata);

        The file is never closed by the file_Close() function. Is it on purpose?
        Basically, file_Close(...) function will ensure that the file is closed to avoid data corruption after the writing.


        I have attached the modified version of the demo project.

        Best regards,
        Kayven
        Attached Files
        Last edited by kayven@4d; 7th November 2018, 06:15 PM.

        Comment


        • #5
          Hi,

          Thanks your answer.

          However I still don't manage to properly get char and transform them to string. Perhaps I'm missing a file_Seek to initialize the pointer at a position 0?

          I've noticed the file_GetS function in the documentation. It apparently gets the whole line as a string until a line feed is reached. Wouldn't it be a better fit? Is CR included in the read string in that scenario?

          Best regards

          Comment


          • #6
            Hi,


            Welcome back to the forum!


            file_Seek(...) function can be set at different address.


            And Yes, you're right, file_GetS(...) function will include the "CR" character
            with the whole string.

            This function will stop reading the file when it encounters a newline character.

            Intentionally, "CR" character is an indicator especially if you have a file that contains multi double-space text.
            Thus, file_GetS(...) is not recommended to use if a file has more than newline character.



            Best regards,
            Kayven
            Last edited by kayven@4d; Yesterday, 07:05 PM.

            Comment

            Working...
            X