Announcement

Collapse
No announcement yet.

How to save variables(integers) in SDcard in .txt file?

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

  • How to save variables(integers) in SDcard in .txt file?

    I am using uLCD-70DT, Diablo 16, Visi, for my project.

    I want the text file to contain:
    Time: 1
    Time: 2
    Time: 3
    Time: 4
    The number increases every time the screen is touched.

    These numbers will be replaced with time after connecting a RTC module.
    But for now, I want to know how to write variables to .txt file. The variable here is the number of times the screen is touched (denoted by "x" in the project file).
    Should I be converting "x" to string before writing it? If so, then how?

    Here is my code: writing_deletable.zip

    I am using:
    Code:
     hndl1 := file_Open("qwe.txt", 'a');
                res := file_PutS("Time: ", hndl1);
                res := file_Write(x, 1, hndl1);
                res := file_PutC("\n", hndl1);
                res := file_Close(hndl1);
    But the .txt file contains random things. Eg.
    楔敭›ሀ楔敭›ኁ楔敭›ሽ楔敭›ሀ楔敭›ሀ
    I have read through pages 302 of Diablo internal 4DGL function manual.
    P.S: I do not understand buffers and pointers, if it is required, I will read about it.
    Also, is it possible to read content of the .txt file from the display?
    Attached Files

  • #2
    Hi,

    This function may work for you.

    var string[10];
    var buffer;

    hndl1 := file_Open("qwe.txt", 'a');
    res := file_PutS("Time: ", hndl1);
    buffer := x;
    to (string); print(buffer);
    res := file_PutS(string, 1, hndl1);
    res := file_PutC("\n", hndl1);
    res := file_Close(hndl1);

    I Hope this helps

    Best regards

    Paul

    Comment


    • Something
      Something commented
      Editing a comment
      Great. It works.
      But the
      res := file_PutC("\n", hndl1);
      doesn't seem to work.

      This is what I get:
      "Time: 1Time: 2Time: 3Time: 4Time: 5Time: 6Time: 7"
      It won't create a new line. Instead it ll make a vertical double arrow (I tried pasting it here but it doesn't seem visible, the arrow is right after the number.).

      Similar problem with \t (tab). It writes capital "R" instead. I get:
      "Time: 1RTime: 2RTime: 3R"

      If I put the "\n" in
      "res := file_PutS("Time: \n", hndl1);"
      It ignores the "\n".
      If I put a "\t" there, it works.

      What can I do to fix this?
      Last edited by Something; 27th April 2017, 10:59 PM.

  • #3
    Hi,

    Could you try using this instead,

    res := file_PutC(10, hndl1);


    Best regards

    Paul

    Comment


    • #4
      Hi,

      This also works

      res := file_PutC('/n', hndl1);

      Best regards

      Paul

      Comment


      • Something
        Something commented
        Editing a comment
        It writes:
        Time: 1/Time: 2/Time: 3/Time: 4/

    • #5
      I figured it out!!!
      Instead of using
      Code:
      res := file_PutC("\n", hndl1);
      I used
      Code:
      res := file_PutS("\r\n", hndl1);
      Because windows notepad doesn't recognize "\n" as line break character. We have to use something called a Windows carriage returns "\r\n".

      Comment


      • #6
        Now the only question remains is if it possible to read content of the .txt file from the display?

        Comment


        • #7
          Something,

          There is an application note which can help you in your task.
          ViSi Read and Write to microSD:

          Please study the source file inside the Zipped project.
          http://www.4dsystems.com.au/appnote/4D-AN-00077/

          Best regards,

          Comment


          • #8
            I tried that. It only displays the content of the SD card. I want the content of the .txt file in the SD card, if thats possible.

            I tried
            Code:
            x := file_Open("qwe.txt", 'r');
            print(x);
            But it prints
            15788
            or some other number.

            Comment


            • #9
              Hi,

              You would have to use thr file_Read command to get the inividual bytes from the file.


              res := file_Read(memblock,20, hndl1);

              I hope this helps

              Paul

              Comment


              • #10

                Thanks Paul. I tried using
                Code:
                var memblock;
                hndl1 := file_Open("data.txt", 'r');
                    res := file_Read(memblock, 20, hndl1);  // read and store in variable.
                    print(memblock);             // print the stored value
                    res := file_Close(hndl1);
                The data.txt file contains the word "Hello".
                But the code prints "0".
                I also tried
                Code:
                file_GetC(hndl1);
                This prints out "72", which is the ASCII code for "H".

                What am I doing wrong?

                My code: memoryRead_deletable.zip

                Comment


                • #11
                  Hi

                  This work's but I am sure there is a better way

                  var memblock[100];

                  hndl1 := file_Open("data.txt", 'r');
                  res := file_Read(memblock, 20, hndl1); // read and store in variable.
                  res := file_Close(hndl1);

                  var n;
                  for(n := 0; n < 20; n++)
                  putch((memblock[n]) & 0xff);
                  putch((memblock[n] >> 8) & 0xff);
                  next

                  or you could use this,

                  var n;
                  hndl1 := file_Open("data.txt", 'r');
                  for(n := 0; n < 20; n++)
                  putch(file_GetC(hndl1));
                  next
                  res := file_Close(hndl1);

                  I hope this helps

                  Best regards

                  Paul

                  Comment


                  • #12
                    Hi

                    This is the better way.

                    var memblock[100];

                    hndl1 := file_Open("data.txt", 'r');
                    res := file_Read(memblock, 20, hndl1); // read and store in variable.
                    print([STR] memblock);
                    res := file_Close(hndl1);

                    Best regards

                    Paul

                    Comment


                    • Something
                      Something commented
                      Editing a comment
                      Thank you so much, Paul. This work!!!!

                  • #13
                    The above code has been helpful. Hopefully someone can help me with some additional functionality:

                    Let's say I want to go back and overwrite the Time 2: slot. How would I do this?
                    I want to seek to a certain line number based on the passed argument 'n' and then append what is written on the line.
                    Initially I was trying to file_Seek, but that wasn't working so instead I tried looping newlines to get to the line number desired.
                    This goes to the correct line number, but overwrites everything in its path, but overwrites preceding lines in the process.

                    func writeuSD (var n)
                    var y ;
                    str_GetW(&i, &d) ;
                    hndl1 := file_Open("Config.txt", 'a');
                    to (string); print(d);
                    //res := file_Seek(hndl1, 0x0000, 0x0005*n ) ;
                    res := file_Seek(hndl1, 0x0000, 0x0000) ;
                    for (y :=0 ; y<=n ; y++)
                    res := file_PutC('\n', hndl1) ;
                    next
                    res := file_PutS(string, hndl1);
                    res := file_Close(hndl1);
                    n := 0 ;
                    endfunc

                    Regards,
                    Justin

                    Comment

                    Working...
                    X