FUNCTION "FC_ParseCheckForStart" : Bool
TITLE = FC_ParseCheckForStart
{ S7_Optimized_Access := 'TRUE' }
AUTHOR : 'e.yueksel'
FAMILY : Parser
VERSION : 0.1
//Diese Baustein überprüft neue Telegramme auf Anfangskennung entsprechend der Telegrammdefinition.
   VAR_INPUT 
      bInNewData : Byte;   // The byte we compare to the Start marker 
      arInBuffer : Array[*] of Byte;   // The buffer (Array of bytes) we are 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.nStartMarkerLen > 0) THEN
	    IF (#bInNewData = #stInTelegramDefinition.sStartMarker[#stInTelegramDefinition.nStartMarkerLen] AND
	        (#nInBufferLen >= UDINT_TO_DINT(#stInTelegramDefinition.nStartMarkerLen))) THEN
	        FOR #j := 0 TO UDINT_TO_DINT(#stInTelegramDefinition.nStartMarkerLen - 1) DO
	            IF (#arInBuffer[#nInBufferLen -1 - #j] <> #stInTelegramDefinition.sStartMarker[UDINT_TO_DINT(#stInTelegramDefinition.nStartMarkerLen)-#j]) THEN
	                #FC_ParseCheckForStart := FALSE;
	                RETURN;
	            END_IF;
	        END_FOR;
	        
	        #FC_ParseCheckForStart := TRUE;
	    ELSE
	        #FC_ParseCheckForStart := FALSE;
	    END_IF;
	ELSE
	    #FC_ParseCheckForStart := FALSE;  // In case there is no start pattern specified, the parser state machine recognizes the telegram start itself regarding to its state.
	END_IF;
END_FUNCTION

