Create a string by extracting value from a file

2022-06-22
2022-06-26
  • master-student - 2022-06-22

    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 2022-06-22
  • paulpotat

    paulpotat - 2022-06-22

    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.

     
    • master-student - 2022-06-25

      Hi,

      Thanks for the input. I tried working with CAA file and the examples but i did not managed to come up with a solution. I first opened a directory and then a opened file but can't copy a value to a string, out of it. Could you may be tell me what function should I use exactly use or an example for the problem? Thanks

       
  • TimvH

    TimvH - 2022-06-25

    You can use the "Read" functionality. When you call this (after you opened the file succesfully), the result is written in the buffer you have assigned when you call the Read function. Then it reads the amount of the size of the buffer, of if the file is smaller than the buffer, the whole file. From this buffer, you can use other functions to find the string you are looking for.

    So in the example at the top, the variable "Data" will contain the contents you read from the file.

     
  • TimvH

    TimvH - 2022-06-25

    You can use the "Read" functionality. When you call this (after you opened the file succesfully), the result is written in the buffer you have assigned when you call the Read function. Then it reads the amount of the size of the buffer, of if the file is smaller than the buffer, the whole file. From this buffer, you can use other functions to find the string you are looking for.

    So in the example at the top, the variable "Data" will contain the contents you read from the file.

     
    • master-student - 2022-06-26

      Thanks, I am trying to use the GVL examples but I am getting the following error "C0077: Unknown type: 'CAA_FILENAME'. any idea?

       
    • master-student - 2022-06-26
       

      Last edit: master-student 2022-06-26
    • master-student - 2022-06-26
       

      Last edit: master-student 2022-06-26
    • master-student - 2022-06-26
       

      Last edit: master-student 2022-06-26

Log in to post a comment.