Read Text File

2016-02-19
2017-06-21
  • Gyzmologist - 2016-02-19

    I want to read a text file containing two INT pairs into a two dimension INT array. The text files will be created externally and copied onto the controller when updated. This will be a headless system (no display).

    Does anyone have an ST program example of opening and reading a text file? I downloaded an example used in the help file, but it's not very helpful. I have a lot of programming experience with multiple languages but am new to CodeSys and IEC PLCs.

    TIA!
    Gyz

     
  • Anonymous - 2016-02-21

    Originally created by: scott_cunningham

    You will need to use the library SysFile and the functions: SysFileOpen, SysFileRead and SysFileClose. Here is a sample view of a ReadFile program. I suggest adding an FB_EXIT() method (see online help) to the ReadFile program which calls SysFileClose - that helps avoid file handles being left open when the system is interrupted... Hope this helps!

    HINT: Some hardware platforms do not support AM_APPEND modes (typically flash based memory devices that don't have hard drives/CF cards/SD cards).

    PROGRAM ReadFile
    VAR CONSTANT
       MAX_BUFFER : __XWORD := 999;
    END_VAR
    VAR_INPUT
       FileName : STRING := 'c:/test.txt';
    END_VAR
    VAR_OUTPUT
       Done : BOOL := FALSE;
       ErrCode : SysFile.SYS_FILE_STATUS := SysFile.SYS_FILE_STATUS.FS_NO_FILE;
       Data : ARRAY[0..MAX_BUFFER] OF BYTE;
       BytesRead : __XWORD := 0;
    END_VAR
    VAR
       pResult : POINTER TO SysFile.RTS_IEC_RESULT;
       FileHandle : SysFile.RTS_IEC_HANDLE;
       pFileData : POINTER TO BYTE := ADR(Data);
    END_VAR
    //try to open file
    FileHandle := SysFileOpen(szFile:= FileName, am:=AM_READ, pResult:=pResult);
    //verify found a file
    IF FileHandle = SysFile.RTS_INVALID_HANDLE THEN
       //no file found
       ErrCode := SysFile.SYS_FILE_STATUS.FS_NO_FILE;
       Done := TRUE;
    ELSE
       //try to read file out
       BytesRead := SysFileRead(hFile:=FileHandle, pbyBuffer:=pFileData, ulSize:=MAX_BUFFER, pResult:=pResult);
       
       //check if actually read any bytes
       IF BytesRead > 0 THEN
          ErrCode := SysFile.SYS_FILE_STATUS.FS_OK;
       ELSE
          ErrCode := SysFile.SYS_FILE_STATUS.FS_NO_FILE;
       END_IF
       Done := TRUE;
    END_IF
    //close file!!!!
    SysFileClose(hFile:=FileHandle);
    
     
  • Gyzmologist - 2016-02-29

    That was very helpful - thanks!

    I have a some questions not answered in the help file that I'd like to ask.

    Q1: Is the data read from the file altered in any way, such a stripping off line terminations (cr/lf)?

    Q2: Do the Open/Read/Close functions block execution until completed (success or fail), or do I have to set up a loop and wait for them to complete?

    Thanks! I really appreciate your help.
    Gyz

     
  • Anonymous - 2016-02-29

    Originally created by: scott_cunningham

    To answer your questions:

    Q1: I think the characters are still there for you to parse. You will need to make a small test - I don't 100% remember.

    Q2: The open/read/writes are function calls and so they execute and that's it - no need to "wait". Double check how long the execute time is - depending on size of read and hardware platform....

    For embedded systems you typically need to read smaller chunks instead of a large file.

     
  • Oliver - 2017-06-20

    Hello,
    it seems that the code of scott_cunningham does exactly what I want to do in my application. However, I got error messages in the variable declaration section (e.g. in this row
    ErrCode: SysFile.SYS_FILE_STATUS := SysFile.SYS_FILE_STATUS.FS_NO_FILE;):
    "Identifier SysFile not defined".
    In the program section there the functions from the SysFile are linked to the Sysfile library. I tried to find this library, but it seems to be very well hidden.
    My Codesys version is V3.5 and I'm also new in Codesys programming.
    Anyone has got a hint for me?
    Greetings!
    Oliver

     
  • Oliver - 2017-06-21

    OK I found the errors:
    1. the SysFile library was not activated. It took me quite a while to locate it. It is within the Data_Server library.
    2. after the activation of the Data_server library in my project, I had to relocate the SysFile variables.

    Now there are no errors anymore and I can test the functionality of the code

    Thanks so far!

    Oliver

     

Log in to post a comment.