FUNCTION "FC_ParseCheckForEnd" : Bool
TITLE = FC_ParseCheckForEnd
{ S7_Optimized_Access := 'TRUE' }
AUTHOR : 'e.yueksel'
FAMILY : Parser
VERSION : 0.1
//Diese Baustein überprüft neue Telegramme auf Endekennung entsprechend der Telegrammdefinition.
   VAR_INPUT 
      bInNewData : Byte;   // The byte we compare to the end marker 
      arInBuffer : Array[*] of Byte;   // The buffer (Array of bytes) we are currently parsing
      nInBufferLen : DInt;   // Length of the buffer
      stInTelegramDefinition : "UDT_TelegramDefinition";   // The definition of the telegram (Parameters)
   END_VAR

   VAR_TEMP 
      j : DInt;   // Loop variable
   END_VAR


BEGIN
	 IF (#stInTelegramDefinition.nEndMarkerLen > 0) THEN
	    IF (#bInNewData = #stInTelegramDefinition.sEndMarker[#stInTelegramDefinition.nEndMarkerLen] AND
	        #nInBufferLen >= UDINT_TO_DINT(#stInTelegramDefinition.nStartMarkerLen + #stInTelegramDefinition.nEndMarkerLen)) THEN
	        FOR #j := 0 TO UDINT_TO_DINT(#stInTelegramDefinition.nEndMarkerLen - 1) DO
	            IF (#arInBuffer[#nInBufferLen - 1 - #j] <> #stInTelegramDefinition.sEndMarker[UDINT_TO_DINT(#stInTelegramDefinition.nEndMarkerLen) - #j]) THEN
	                #FC_ParseCheckForEnd := FALSE;
	                RETURN;
	            END_IF;
	        END_FOR;
	        
	        IF (#stInTelegramDefinition.nLength > 0) THEN
	            #FC_ParseCheckForEnd := (#nInBufferLen = UDINT_TO_DINT(#stInTelegramDefinition.nLength));
	        ELSE
	            #FC_ParseCheckForEnd := TRUE;
	        END_IF;
	    ELSE
	        #FC_ParseCheckForEnd := FALSE;
	    END_IF;
	ELSE
	    #FC_ParseCheckForEnd := (#nInBufferLen = UDINT_TO_DINT(#stInTelegramDefinition.nLength));
	END_IF; 
	
END_FUNCTION

