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 and Read using Codesys

naganathas
2012-08-16
2018-09-17
  • naganathas - 2012-08-16

    Hi,

    Can you please provide me a working sample code for Write and Read File using Codesys v3 ?

    Thanks,

    Naganatha Subramanian R

     
  • conoscenza - 2013-02-22

    Did you solve that?

     
  • TimvH

    TimvH - 2013-02-23

    It is advisable to use the CAA_FILE.library.
    In the help of V3 is an example.
    Libraries > System Libraries > CAA Libraries > CAA_FILE.library > Examples > File - Standard (EXAMPLE)

     
  • shooter - 2013-02-24

    old post

     
  • conoscenza - 2013-02-28

    Thank you so much for the answer.

    I don't have the example, can you send me it?
    Is it possible to attacc in a post?

    Thank you again!

     
  • TimvH

    TimvH - 2013-03-01

    Below you can find the copy from the help. I didn't test this and have no idea whether it works directly without compile errors.
    You should off course also add the USE CASE library "File access".

    Please also check the store at http://store.codesys.com. After signing in you can download a package with a "File Utilities" example.

    PROGRAM FILE.STANDARD_PRG
    VAR
    xFileStdInit: BOOL:=FALSE;
    uiFileStdState: UINT:=0;
    sFileName: CAA.FILENAME:= 'TestFile.txt';
    hFile: CAA.HANDLE;
    sFileTestString: STRING:='Hello caa library user';
    sFileString: STRING:='';
    szFileSize1: CAA.SIZE := 0;
    szFileSize2: CAA.SIZE := 0;
    filop: FILE.Open;
    filwr: FILE.Write;
    filrd: FILE.Read;
    filcl: FILE.Close;
    END_VAR
    IF NOT xFileStdInit THEN
    filop(xExecute:=FALSE);
    filcl(xExecute:=FALSE);
    filwr(xExecute:=FALSE);
    filrd(xExecute:=FALSE);
    xFileStdInit:=TRUE;
    uiFileStdState:=0;
    ELSE
    CASE uiFileStdState OF
    0: (* create a new file *)
    filop.sFileName:=sFileName;
    filop.eFileMode:=FILE.MRDWR;
    filop.xExclusive:=TRUE;
    filop( xExecute:=TRUE);
    IF filop.xDone THEN
    hFile:=filop.hFile;
    uiFileStdState:=1;
    END_IF
    IF filop.xError THEN
    (* error handling*)
    ;
    END_IF
    1:(* write text in the file *)
    filwr.hFile:=hFile;
    filwr.pBuffer:=ADR(sFileTestString);
    szFileSize1:=SIZEOF(sFileTestString);
    filwr.szSize:=szFileSize1;
    filwr.udiTimeOut:=100000; (* 100ms Timeout *)
    filwr( xExecute:=TRUE);
    IF filwr.xDone THEN
    uiFileStdState:=2;
    END_IF
    IF filwr.xError THEN
    (* error handling*)
    ;
    END_IF
    2:(* read file - TestFile.txt*)
    filrd.hFile:=hFile;
    filrd.udiTimeOut:=100000; (* 100ms Timeout *)
    filrd.pBuffer:=ADR(sFileString);
    filrd.szBuffer:=255;
    filrd( xExecute:=TRUE);
    IF filrd.xDone THEN
    szFileSize2:=filrd.szSize;
    IF szFileSize2 = szFileSize1 THEN
    uiFileStdState:=3;
    ELSE
    (* error handling*)
    ;
    END_IF
    END_IF
    IF filrd.xError THEN
    (* error handling*)
    ;
    END_IF
    3: (* close file - TestFile.txt *)
    filcl.hFile:=hFile;
    filcl( xExecute:=TRUE);
    IF filcl.xDone THEN
    uiFileStdState:=4;
    END_IF
    IF filcl.xError THEN
    (* error handling*)
    ;
    END_IF
    4: (* end of example *)
    ;
    END_CASE
    END_IF
    
     
  • conoscenza - 2013-03-04

    Thank you so much!

    I will test it!

     
  • RudolfAtITD - 2013-03-16

    Hello - from a new user!
    I have integrated the relevant statements from the "official" example for opening a file with the CAA File library. The statement "filop.eFileMode:=FILE.MRDWR" produces an error (in a German language system):



    The tooltip on ".eFileMode" shows me:


    When I use this file mode value "FILE_MWRRD, the same error - with this sentence - will be shown.

    Where is my mistake - I have no idea?!

    System: CODESYS V3.5 SP2 Patch 2 on a Windows 7 system.

     
  • TimvH

    TimvH - 2013-03-17

    The example is from a somewhat older library.
    You should now use:
    filop.eFileMode:=file.MODE.MRDWR;

     
  • RudolfAtITD - 2013-03-17

    Hello TinvH,

    thank you - this is the solution that works! 3S has some problems with its online help maintenance.

    With regards, Rudolf.

     
  • bezzaouia - 2014-11-27

    thank you so much it was helpful for me too

     
  • drsumruay - 2018-07-06

    [PLEASE] I'm newbie in PLC programming, I can't update my value of string when I'm changing text in txtfile while the programming is logging and running.

     
  • ANU - 2018-09-17

    TimvH hat geschrieben:
    It is advisable to use the CAA_FILE.library.
    In the help of V3 is an example.
    Libraries > System Libraries > CAA Libraries > CAA_FILE.library > Examples > File - Standard (EXAMPLE)

    I have used The same Example and its Working but the .txt File is not human readable Format .
    And also i have taken DINT Array in sFileTestString.
    How can i save file with Proper Format.

    receipe.txt [403 Bytes]

     
  • johnlee - 2018-09-17

    Hi Anu,

    declare string type
    sFileTestString : string(500);
    cnt : int := 500;

    assign the integer into string;

    sFileTestString := int_to_string(int);

    then in the file write function the buffer is point to the string.

    filwr.pBuffer:=ADR(sFileTestString);

    By this way you can get human readable data. To store all the data type into human readable you need to convert it into string, and this is very painful job to do.

    Thank you
    John Lee

     

Log in to post a comment.