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

File write question

KMor77
2019-10-16
2019-10-17
  • KMor77 - 2019-10-16

    I have a simple question regarding writing to a file.

    The example in Codesys is

    2: ( write text in the file )
    filewrite.hFile:=hFile;
    filewrite.pBuffer:=ADR(filestring);
    filesize1:=SIZEOF(filestring);
    filewrite.szSize:=filesize1;
    filewrite.udiTimeOut:=100000; ( 100ms Timeout )
    filewrite(xExecute:=TRUE);
    IF filewrite.xDone THEN
    uiDemoStatus:=uiDemoStatus+1;
    END_IF
    IF filewrite.xError THEN
    ( error handling)
    ;
    END_IF

    why isn't the write reinitialized each time during the program loop? It seems that you would never get to set the done flag.

     
  • TimvH

    TimvH - 2019-10-16

    You're right. Somewhere should also be a fileWrite(xExecute := FALSE);
    Otherwise it will only write once.

     
  • KMor77 - 2019-10-17

    Yes I realized that the FB responds to the rising edge of the execute flag. It seems the code before the write call would be executed needlessly every program loop.

    would it be better to include a "IF filewrite.xExecute = FALSE" before defining the size, buffer address etc.?

     
  • dFx

    dFx - 2019-10-17

    File operations are async. Meaning that they may last way more than one cycle till done.
    Just pass the correct parameters when your xexecute is rising from 0 to 1.
    Then you should not stop it till the done( may be error) flag. Otherwise you could have inconsistent file writings.

    On the exemple provided, the xexecute flag is not reseted because when it's done or when the file is not open for a write, the write function is not called.

     

Log in to post a comment.