FUNCTION_BLOCK "CheckForTimeOut"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      lastContactTime { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : LDT;
      timeOut { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : LTime;
      enable { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : Bool;
   END_VAR

   VAR_OUTPUT 
      isTimedOut { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : Bool;
   END_VAR

   VAR 
      statCurrentTime { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : LDT;
      statTimeDiff { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : LTime;
      instProgramAlarm {InstructionName := 'Program_Alarm'; LibVersion := '1.0'; ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'; S7_SetPoint := 'False'} : Program_Alarm;
   END_VAR

   VAR_TEMP 
      tempRetValueSysTime : Int;
      tempIsTimedOut : Bool;
   END_VAR


BEGIN
	REGION Description header
	    //==================================================================================
	    // SIEMENS / (C) Copyright 2023
	    //----------------------------------------------------------------------------------
	    // Title: CheckForTimeOut
	    // Comment/Function: compares LastContactTime to a timeOut
	    // Library/Family: 
	    // Author: SIEMENS SIMATIC Systems Support (SUP)
	    // Target System: 
	    // Engineering:
	    // Restrictions: 
	    // Requirements: 
	    //----------------------------------------------------------------------------------
	    // Change log table:
	    // Version | Date | Expert in charge | Changes applied
	    //-------------|------------|------------------|------------------------------------
	    // 001.000.000 | 2023-02-13 | SUP SPH | First released version
	    //==================================================================================
	END_REGION Description header
	
	IF #enable THEN
	    
	    REGION Check Timeout
	        //Read system time
	        #tempRetValueSysTime := RD_SYS_T(#statCurrentTime);
	        
	        //calculate time difference
	        #statTimeDiff := T_DIFF(IN1 := #statCurrentTime, IN2 := #lastContactTime);
	        
	        //compare time difference to timeOut
	        IF #statTimeDiff > #timeOut THEN
	            #tempIsTimedOut := TRUE;
	        ELSE
	            #tempIsTimedOut := FALSE;
	        END_IF;
	        //fire program alarm
	        #instProgramAlarm(SIG := false);//#tempIsTimedOut);
	    END_REGION  Check Timeout
	    
	ELSE
	    #tempIsTimedOut := FALSE;
	END_IF;
	
	REGION Write Output
	    #isTimedOut := #tempIsTimedOut;
	END_REGION Write Output 
END_FUNCTION_BLOCK

