Announcement

Collapse
No announcement yet.

Accesing to files stored in SD, from external functions

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

  • Accesing to files stored in SD, from external functions

    Hello,

    I had a problem reading a file in the SD card.

    When I use this code (see below) from a function of my program, all runs fine.


    But when I use an external function for doing that:

    pPointer_I2C := file_LoadFunction("ReadOption.4xe") ;
    if(!pPointer_I2C)
    return;
    endif



    then it can not open or create the "options file".

    Is there any limitation of using functions for accessing to SD, from External Functions?
    Can not access to global data and vars form this external function?


    Code for reading my file stored in the SD card:

    //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    // Global data and vars
    #CONST
    myFile:= "File01.txt";

    N_OPTIONS 5
    #END

    var OptionsValues[6];
    //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    func main()

    // If file does not exist, I create it, and write 1 inside.
    if(! file_Exists(myFile))
    handle := file_Open(myFile, 'w');
    file_PutC(1, handle);
    file_Close(handle);
    endif

    // If file exists, I read and load value from it.
    handle := file_Open(myFile, 'r');
    OptionsValues[N_OPTIONS] := file_GetC(handle) ;
    file_Close(handle);


    endfunc



    Thanks in advance,
    Valentin
    Last edited by Valen; 29 December 2014, 05:57 AM.

  • #2
    Are you thinking that
    Code:
        if(!pPointer_I2C)
    will call the loaded function and turn it's result?

    It won't, it will just check the function pointer for 0.

    You probably meant to say
    Code:
        if(!pPointer_I2C())
    And that might be problematic as you sample function doesn't appear to return anything.
    Mark

    Comment


    • #3
      The problem may be in the long filename. Note that only short 8.3 filenames are supported.

      regards,
      Valentin

      Comment


      • #4
        Thank you for helping me,


        I think my problem is because the specific of this case:

        when I do:
        OptionsValues[N_OPTIONS] := file_GetC(handle) ;
        I am trying to update a global array var, which is shared with the main program (both .4dg files, main and function, share the vars definitions).


        But ┬┐Do both "4dg" files use the same OptionsValues[] array, or when I compile this projects, I have two differents copies, and for that, main program always read '0' in its array?

        In that case,
        What is the way for updating this global array, from a external function?

        Thanks,
        Last edited by Valen; 3 January 2015, 03:27 AM.

        Comment


        • #5
          Globals are global only to the current program, So a loaded function has different globals.

          You need to pass an address to the function that updates the memory in the main program.

          Have a look at http://www.4dsystems.com.au/appnote/4D-AN-00055/
          Mark

          Comment


          • #6
            In short:

            Example of method of passing vars by reference (not by value):

            // My var definitions.
            Var iHola; // It is an integer
            var Options[10]; // It is an array

            Modifying these values in main() program:
            iHola := 0x08; // Load 0x08 in iHola var
            Options[3] := 0x02; // Load 0x02 in position 3 of array Options


            Passing these vars to a function:
            myFunc (&iHola, Options);


            and inside, the function uses these values that way:
            myFunc (piHola, pOptions)

            *piHola := 0x04;
            // Load 0x04 in iHola var
            Options[3] := 0x06; // Load 0x06 in position 3 of array Options

            endfunc

            That way, there is only one iHola var, and only one Options array in the program, and its contents can by modified from main() program of from another external function.


            ( If myFunc is an External Function which it is stored in ReadOpts.4fn file, then I have to load it in RAM, before using)
            var myFunc;
            myFunc := file_LoadFunction("ReadOpts.4fn") ;
            Last edited by Valen; 9 January 2015, 04:35 PM.

            Comment

            Working...
            X