How to update string variable from the temperature sensor file

2022-06-30
2022-07-01
  • master-student - 2022-06-30

    Hi, I am able to read a file and make a string from that. But the read action is only happening once so that the string stays constant. I want the the string to be updated after every one second.

    This is the code,

    VAR
    
           sFileName        : FILE.CAA.FILENAME;
           FileOpen         : File.Open;
           FileClose        : File.Close;
           FileRead         : File.Read;
           iState           : UINT;   
           hfile            : FILE.CAA.HANDLE;
           sReadLine        : STRING(1000);
    
        END_VAR
        VAR_INPUT
           xInit            : BOOL;
        END_VAR
        VAR_OUTPUT
           xError           : BOOL;
           xDone            : BOOL;   
    
        END_VAR
    CASE iState OF
         0: 
          sFileName := '/sys/bus/w1/devices/28-44260f1e64ff/w1_slave';
          xDone := FALSE;
          xError := FALSE;
          iState:= 3; 
    
    3:    // Open the file with read access. 
          FileOpen(xExecute:= TRUE, sFileName:= sFileName, xExclusive:= FALSE, eFileMode:= File.MODE.MREAD);
          IF FileOpen.xDone = TRUE THEN
             hfile := FileOpen.hFile;
             FileOpen(xExecute:= FALSE);
             iState := 4;
          ELSIF FileOpen.xError = TRUE THEN
             iState:= 32767;
             FileOpen(xExecute:= FALSE);
          END_IF
    
       4: // Read the string from the file.
    
          FileRead(xExecute:= TRUE, hFile:= hfile, pBuffer:= ADR(sReadLine), szBuffer:= SIZEOF(sReadLine));
          IF FileRead.xDone = TRUE THEN
             iState := 5;
             // Cut off the unused characters.
             //sReadLine := MID(sReadLine, ANY_TO_INT(FileRead.szSize), 1);
             FileRead(xExecute:= FALSE);
          ELSIF FileRead.xError = TRUE THEN
             iState:= 32767;
             FileRead(xExecute:= FALSE);
          END_IF
    
    
       5: // Close that file.
          FileClose(xExecute:= TRUE, hFile:= hfile);
          IF FileClose.xDone = TRUE THEN
             iState:= 6;
             FileClose(xExecute:= FALSE);
          ELSIF FileClose.xError = TRUE THEN
             iState:= 32767;
             FileClose(xExecute:= FALSE);
          END_IF
    
    
        END_CASE
    

    I would be super happy if someone can guide me

     

    Last edit: master-student 2022-06-30
  • paulpotat

    paulpotat - 2022-07-01

    Could'nt you just change this line :

    iState:= 6;
    

    To :

    iState:= 0;
    

    So your state machine goes back to state 0 and it loops infinitely ? If you want to time it you can just add a wait state with a timer.

     

Log in to post a comment.