Announcement

Collapse
No announcement yet.

Write and read variables on SD card

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

  • Write and read variables on SD card

    Hello,

    I'm trying to store variables on the SD card so I can read them again when I turn on the power.

    I can write the name of the variable but the value that follows is not good.

    Here is my code:

    Code:
    func writeToSD()
    
    var hndl1;
    var res;
    var numberOfBall := 10;
    var nbOfExtraBall := 9;
    var slowFirst := 8;
    var fastFirst := 7;
    var slowNext := 6;
    var fastNext := 5;
    var isBallPerBall := 4;
    
    
    file_Erase("params.ini");
    
    hndl1 := file_Open("params.ini", 'w');
    
    res := file_PutS("numberOfBall: ", hndl1);
    res := file_PutC(numberOfBall, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("nbOfExtraBall: ", hndl1);
    res := file_PutC(nbOfExtraBall, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("slowFirst: ", hndl1);
    res := file_PutC(slowFirst, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("fastFirst: ", hndl1);
    res := file_PutC(fastFirst, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("slowNext: ", hndl1);
    res := file_PutC(slowNext, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("fastNext: ", hndl1);
    res := file_PutC(fastNext, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("isBallPerBall: ", hndl1);
    res := file_PutC(isBallPerBall, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_Close(hndl1);
    
    txt_MoveCursor(0, 0);
    putstr("Write");
    endfunc
    Here is what the file shows me under windows:

    Click image for larger version

Name:	a.png
Views:	1
Size:	6.3 KB
ID:	63452

    instead of this:

    Code:
    numberOfBall: 10
    nbOfExtraBall: 9
    slowFirst: 8
    fastFirst: 7
    slowNext: 6
    fastNext: 5
    isBallPerBall: 4
    I would also like to be able to read this file and assign the values ​​to the variables of my program but I do not find how to do it.

    Thanks for your help.
    Last edited by A.Pell; 14th May 2018, 02:58 AM.

  • #2
    Hello,

    Welcome to the forum,

    This code will put your values as text in your file.

    Code:
    func writeToSD()
    
    var hndl1;
    var res;
    var numberOfBall := 10;
    var nbOfExtraBall := 9;
    var slowFirst := 8;
    var fastFirst := 7;
    var slowNext := 6;
    var fastNext := 5;
    var isBallPerBall := 4;
    var string[10];
    
    
    file_Erase("params.ini");
    
    hndl1 := file_Open("params.ini", 'w');
    
    res := file_PutS("numberOfBall: ", hndl1);
     to (string); print(numberOfBall);
    res := file_PutS(string, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("nbOfExtraBall: ", hndl1);
     to (string); print(nbOfExtraBall);
    res := file_PutS(string, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("slowFirst: ", hndl1);
     to (string); print(slowFirst);
    res := file_PutS(string, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("fastFirst: ", hndl1);
     to (string); print(fastFirst);
    res := file_PutS(string, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("slowNext: ", hndl1);
     to (string); print(slowNext);
    res := file_PutS(string, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("fastNext: ", hndl1);
     to (string); print(fastNext);
    res := file_PutS(string, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_PutS("isBallPerBall: ", hndl1);
     to (string); print(isBallPerBall);
    res := file_PutS(string, hndl1);
    res := file_PutS("\r\n", hndl1);
    
    res := file_Close(hndl1);
    
    txt_MoveCursor(0, 0);
    putstr("Write");
    endfunc
    It might be better to put your values as integers to the file rather than text, using text, you will have to parse the whole file. You can use file_PutW and file_GetW eg

    Code:
    func writeToSD()
    
    var hndl1;
    var res;
    var numberOfBall := 10;
    var nbOfExtraBall := 9;
    var slowFirst := 8;
    var fastFirst := 7;
    var slowNext := 6;
    var fastNext := 5;
    var isBallPerBall := 4;
    var string[10];
    
    
    file_Erase("params.ini");
    
    hndl1 := file_Open("params.ini", 'w');
    
    res := file_PutW(numberOfBall, hndl1);
    res := file_PutW(nbOfExtraBall, hndl1);
    res := file_PutW(slowFirst, hndl1);
    res := file_PutW(fastFirst, hndl1);
    res := file_PutW(slowNext, hndl1);
    res := file_PutW(fastNext, hndl1);
    res := file_PutW(isBallPerBall, hndl1);
    
    res := file_Close(hndl1);
    
    txt_MoveCursor(0, 0);
    putstr("Write");
    
    hndl1 := file_Open("params.ini", 'r');
    
    var i;
    print("\r\n");
    for(i := 0; i<7; i++)
    print(file_GetW(hndl1));
    print("\r\n");
    next
    
    res := file_Close(hndl1);
    
    endfunc
    I hope this helps

    Best regards

    Paul



    Comment


    • #3
      Thank you, that works.
      Here is my code it can help someone else

      Code:
      func saveToSD()
      
          var hndl1;
          var res;
      
          file_Erase("params.ini");
          hndl1 := file_Open("params.ini", 'w');
      
          res := file_PutW(numberOfBall, hndl1);
          res := file_PutW(nbOfExtraBall, hndl1);
          res := file_PutW(slowFirst, hndl1);
          res := file_PutW(fastFirst, hndl1);
          res := file_PutW(slowNext, hndl1);
          res := file_PutW(fastNext, hndl1);
          res := file_PutW(isBallPerBall, hndl1);
      
          res := file_Close(hndl1);
      
      endfunc
      
      func readSD()
      
          var hndl1;
          var i;
      
          hndl1 := file_Open("params.ini", 'r');
      
          for(i := 0; i<7; i++)
              switch(i)
                  case 0:
                      numberOfBall := file_GetW(hndl1);
                  break;
                  case 1:
                      nbOfExtraBall := file_GetW(hndl1);
                  break;
                  case 2:
                      slowFirst := file_GetW(hndl1);
                  break;
                  case 3:
                      fastFirst := file_GetW(hndl1);
                  break;
                  case 4:
                      slowNext := file_GetW(hndl1);
                  break;
                  case 5:
                      fastNext := file_GetW(hndl1);
                  break;
                  case 6:
                      isBallPerBall := file_GetW(hndl1);
                  break;
              endswitch
          next
      
          file_Close(hndl1);
      
      endfunc
      Last edited by A.Pell; 14th May 2018, 11:59 PM.

      Comment

      Working...
      X