Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Logfile.txt - only one entry is written

2019-02-28
2019-03-04
  • odin12345678 - 2019-02-28

    Hello,

    I'm using Codesys V3.5 SP10 Patch 4 pbF with File Access lib 3.5.7.0 and want to write down my measurement results into a logfile.txt. Creating and writing into the logfile works well. However only the last result is written into the logfile. There is only one entry. What can I do to get all events into my logfile. A statemachine didn't helped. Here is my code:

    PROGRAM WRITE_LOG
    VAR
       dirOpen   : FILE.Open;
       fileClose  : FILE.Close;
       fileWrite  : FILE.Write;
       dirClose   : FILE.DirClose;
       hFile : CAA.HANDLE;
       hDir : CAA.HANDLE;
       projectpath : CAA.FILENAME;
       filename   : CAA.FILENAME;
       
       str_writestr:STRING;
       
       time_object_get :DTU.GetDateAndTime;
    END_VAR
    
    str_writestr:='';
    time_object_get(xExecute:=TRUE);
    dirOpen (sFileName:=GVL.filename_log, eFileMode:=FILE.MODE.MWRITE, xExecute:=TRUE);
    IF NOT time_object_get.xError AND NOT time_object_get.xBusy AND NOT time_object_get.xError THEN
        GVL.logtime:=time_object_get.dtDateAndTime;
       time_object_get(xExecute:=FALSE);
    END_IF
    IF dirOpen.xDone AND NOT dirOpen.xError THEN
       hFile := dirOpen.hFile;
       str_writestr:=CONCAT(DT_TO_STRING(GVL.logtime),' ');
       str_writestr:=CONCAT(str_writestr,GVL.logstring);
       str_writestr:=CONCAT(str_writestr,'$R$N');  //Maybe this doesn't work properly??? :| 
       fileWrite(hFile:=hFile, pBuffer:=ADR(str_writestr), szSize:=TO_UDINT(StrLen(ADR(str_writestr))), udiTimeOut:=100000, xExecute:=TRUE);
       
    IF fileWrite.xDone AND NOT fileWrite.xError AND NOT fileClose.xDone THEN
          fileClose(hFile:=hFile, xExecute := TRUE);
       ELSIF fileClose.xDone THEN
          dirOpen(xExecute:=FALSE);
          fileClose(xExecute:=FALSE);
          fileWrite(xExecute:=FALSE);
       END_IF
    END_IF
    
     
  • Lo5tNet - 2019-02-28

    MWRITE mode is like creating a new file every time. So unless you change the pointer to the end of the file it will constantly erase the data.

    Try changing:

    dirOpen (sFileName:=GVL.filename_log, eFileMode:=FILE.MODE.MWRITE, xExecute:=TRUE);
    

    To:

    dirOpen (sFileName:=GVL.filename_log, eFileMode:=FILE.MODE.MAPPD, xExecute:=TRUE);
    
     
  • odin12345678 - 2019-03-04

    Comingback4u hat geschrieben:
    MWRITE mode is like creating a new file every time. So unless you change the pointer to the end of the file it will constantly erase the data.
    Try changing:

    dirOpen (sFileName:=GVL.filename_log, eFileMode:=FILE.MODE.MWRITE, xExecute:=TRUE);
    

    To:

    dirOpen (sFileName:=GVL.filename_log, eFileMode:=FILE.MODE.MAPPD, xExecute:=TRUE);
    

    Thanks guys!!!This one worked for me

     

Log in to post a comment.