Announcement

Collapse
No announcement yet.

Reading SD file at the beagining

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

  • Reading SD file at the beagining

    Hello I am trying to read a file from SD at the begining of the program to configure the system, but I never read this file at the begininig. In the middle of the code I write and read the file and this works correctly. Why I cant read the file at the begining??



    func main()


    //Se montan las imagenes de la tarjeta SD
    //putstr("GSA Composystem batery charger ...\n");
    if (!(file_Mount()))
    while(!(file_Mount()))
    putstr("Drive not mounted...");
    pause(200);
    gfx_Cls();
    pause(200);
    wend
    endif

    hndl := file_LoadImageControl("gauges1.dat", "gauges1.gci", 1);
    gfx_Set(SCREEN_MODE,LANDSCAPE) ;

    custom := file_LoadImageControl("Batery_R.Dat", "Batery_R.Gci", 1);
    img_Show(custom, 0); // custom gauge using graphics composer

    custom_cargando := file_LoadImageControl("cargando.Dat", "cargando.Gci", 1);
    img_Show(custom_cargando, 0); // custom gauge using graphics composer

    custom_descargando := file_LoadImageControl("descarga.Dat", "descarga.Gci", 1);
    img_Show(custom_descargando, 0); // custom gauge using graphics composer


    // This is the part doent work I never enter in this if,

    if (file_Exists("NcCarga.txt"))

    hndCarga := file_Open("NcCarga.txt","r") ;
    print("AA ---- Abrimos para lectura");
    print("\n");
    file_Read(&Carga,1,hndCarga);
    print("AA ----- Leemos un valor de carga:", Carga );
    print("\n");
    file_Close(hndCarga);
    pause(200);

    else

    // I always go to this part of the code, so I write and create de file, at the secondtime and the following times I switch on the LCD it shoul go to the other part of the code because I have created the file, but I allwais go to this part


    pause(1000); // Esperamos a estabilizar tensiones antes de medir
    Vstep := pin_Read(PA0) ; // Lectura analogica de la tension de bateria
    Carga := CargaTension(Vstep);

    hndCarga := file_Open("NcCarga.txt","w");
    print("BB ---- Medimos Tension");
    print("\n");
    file_Write(&Carga,1,hndCarga);
    print("BB ---- Escribimos un valor de carga:", Carga );
    print("\n");
    file_Close(hndCarga);
    pause(200);


    endif

    //....................... The code continues....









    // In this funtion I write and read the file several times during the Display is working, but when it restart I dont real the file at the beginig. can you help me??



    func CargaCorriente (var t1,var StepCar, var StepOut)


    var devuelvo;


    // Cada 50 segundos guardamos el valor de nivel de carga en el fichero Carga.txt


    GrabaCargaTime:= ((sys_T()) - ContadorGrabacion) ;
    if ((GrabaCargaTime > 50000)) //50 segundos

    TimeProbe := TimeProbe + 1;
    gfx_MoveTo(10, 300);
    print("Numero de grabacion en fichero",TimeProbe);
    print("\n");

    // Borramos para escribir
    file_Erase("NcCarga.txt");
    //print("1 Borramos fichero antes de grabar");
    //print("\n");

    if (!(hndCarga := file_Open("NcCarga.txt","w")))

    print("2 Abrimos para ecritura");
    print("\n");
    file_Write(&Carga,1,hndCarga);
    print("3 Escrimos carga", Carga);
    print("\n");
    file_Close(hndCarga);
    print("4 Cerramos escritura");
    print("\n");
    pause(200);

    endif

    //// Lectura

    if (!(hndCarga := file_Open("NcCarga.txt","r")))

    print("5 Abrimos para Comprobar la lectura");
    print("\n");
    file_Read(&Carga,1,hndCarga);
    print("6 Leemos un valor de carga:", Carga );
    print("\n");
    file_Close(hndCarga);
    print("4 Cerramos lectura");
    print("\n");
    pause(200);

    endif


    ContadorGrabacion := sys_T();

    endif



    return(devuelvo);


    endfunc







  • #2
    Hi,

    Welcome to the forum!

    Sorry for the late reply.

    Please use a single quote when you are selecting the mode while opening a file.
    Code:
    hndCarga := file_Open("NcCarga.txt",'r') ;
    hndCarga := file_Open("NcCarga.txt",'w') ;
    Please let me know if that resolves the issue.

    Best Regards,
    Kevin

    Comment


    • #3
      Thanks for the answer.

      I have compruebe that the problem is that the file is not created in the SD card . I have try this simple code to study the problem and it doesnt work, I have read the SD and the file has been not created. This is the code:




      if (!(file_Mount()))
      while(!(file_Mount()))
      print("1---Drive not mounted...");
      pause(500);
      gfx_Cls();
      pause(200);
      wend
      endif
      print("2---Drive mounted...");


      // ********** The file is mounted



      Carga :=1;

      repeat

      // **************** Create the file and write the value

      file_Erase("NcCarga.txt");
      hndCarga := file_Open("NcCarga.txt","w");
      Lectura:= file_Error(); // ----------->I read error number 18
      file_Write(&Carga,1,hndCarga);
      file_Close(hndCarga);

      pause(200);

      // **************** Read the file and compruebe if it exixt

      Lectura := file_Exists("NcCarga.txt");
      print(" ----- Exist the file: ", Lectura); // ----------->Tha value is 0 ( the file has not been created)

      hndCarga := file_Open("NcCarga.txt","r") ;
      Lectura:= file_Error(); // ----------->I read error number 18

      Lectura := file_Read(&i,1,hndCarga);
      print("xx ----- The value readed i:", i ); // ----------->The value of i is 0

      file_Close(hndCarga);

      pause(5000);
      Carga := Carga+1;

      forever

      Comment


      • #4
        Sorry I have found one problem I wrote (change " for '), but Now the file is created but I cant write the value of Carga to the file. I open the txt and is empty.

        hndCarga := file_Open("NcCarga.txt","w"); instead of hndCarga := file_Open("NcCarga.txt",'w');

        The code is:


        var filecarga := "ficherodecarga.txt"

        file_Erase(filecarga);
        hndCarga := file_Open(filecarga ,'w');
        Lectura := file_Write(&Carga,1,hndCarga);
        print("---- Escribimos un valor de menfile:", Carga );
        print(" ----- Miro numero de bites grabados: ", Lectura);
        print("\n");

        file_Close(hndCarga);
        pause(200);


        print("-------------------------------------------------------------------");
        print("\n");
        print("-------------------------------------------------------------------");
        print("\n");



        if ( file_Exists(filecarga) )

        hndCarga := file_Open(filecarga ,'r') ;
        Lectura := file_Read(&Carga,1,hndCarga);
        print(" ----- Leemos un valor del fihero:", Carga );
        print("xx ----- Numero de bytes leidos:", Lectura);
        print("\n");
        file_Close(hndCarga);
        print("-------------------------------------------------");
        print("\n");

        endif


        pause(10000);
        Carga := Carga+1;

        Last edited by [email protected]; 1 week ago.

        Comment


        • #5
          Hi,

          Please try this code:


          Code:
          var Lectura, strnum[5];
          var Carga[5];
          var hndCarga;
          var increment;
          var filecarga := "ficherodecarga.txt" ;
          
              repeat
          
              if (file_Exists(filecarga) )
                  hndCarga := file_Open(filecarga ,'r') ;
                  Lectura := file_Read(Carga,sizeof(strnum)*2,hndCarga);
                   txt_MoveCursor(0,0);
                  print(" ----- Leemos un valor del fihero:", [STR]Carga );
                  print("xx ----- Numero de bytes leidos:", Lectura);
                  print("\n");
                  file_Close(hndCarga);
                  print("-------------------------------------------------");
                  print("\n");
                  pause(200);
                  file_Erase(filecarga);
          
              else
          
                  increment+=1;
                  to(strnum);print(increment);
          
                  hndCarga := file_Open(filecarga ,'w');
                  Lectura := file_Write(str_Ptr(strnum),sizeof(strnum)*2,hndCarga);
                  txt_MoveCursor(8,0);
                  print("---- Escribimos un valor de menfile:", [STR]strnum );
                  print("----- Miro numero de bites grabados: ", Lectura);
                  print("\n");
                  file_Close(hndCarga);
                  print("-------------------------------------------------------------------");
                  pause(200);
          
              endif
          
              forever
          Best Regards,
          Kevin

          Comment


          • #6
            Thanks for the answer. It works, Thanks!!

            The key is use the funtion str_Ptr in file_Write. Thanks for your help!!

            Comment

            Working...
            X