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

Create a string by extracting value from a file

3 days ago
2 days ago
  • master-student

    master-student - 3 days ago

    I want to create a program to extract value from a file and create a sting (later I would want to use this string as an input value).

    Also i found this solution to open the file and read the values. But I did not managed to make a string.

    VAR_INPUT
       FileName : STRING := '/sys/bus/w1/devices/28-44260f1e64ff/w1_slave';
    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);
    

    Please help.

     

    Last edit: master-student 3 days ago
  • paulpotat

    paulpotat - 2 days ago

    Hello,
    I think it would be easier for you to use the CAAFile library instead of Sysfile.
    The File.Open FB of the CAA_File library will directly return a string with the content of your file. You can find examples here.

     

Log in to post a comment.