FUNCTION_BLOCK "FB_UpdateMessage"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      x_InSendMessage : Bool;
   END_VAR

   VAR_OUTPUT 
      x_OutReady : Bool;
      x_OutBusy : Bool;
      x_OutDone : Bool;
   END_VAR

   VAR_IN_OUT 
      udt_InOutHandshakeMES : "udt_HandshakeMessageToMES";
      arrstr_InOutMessageData : Array[1.."cNoPositions"] of Struct
         n_MessageType : USInt;   // 0:= ungueltig; 1:= gefunden: 2:= nicht gefunden
         n_PartID : DInt;
      END_STRUCT;
   END_VAR

   VAR 
      n_Sequence : UInt;
      xError : Bool;
      n_tempSearchIdxsave : Int;
   END_VAR

   VAR_TEMP 
      n_tempSearchIdx : Int;
   END_VAR

   VAR CONSTANT 
      c01_PartGefunden : UInt := 1;
      c02_PartNichtGefunden : UInt := 2;
      c000_CheckNoError : UInt := 0;
      c010_WaitForStart : UInt := 10;
      c020_SetNewMessage : UInt := 20;
      c030_SendMessage : UInt := 30;
      c040_ResetMessageData : UInt := 40;
   END_VAR


BEGIN
	
	REGION Info
	(* Company: Schönenberger Systeme GmbH / (C) Copyright (2022). All rights reserved.
	-----------------------------------------------------------------------------------------
	Title: FB_UpdateMessage
	Comment/Function: Send Message to MES
	Library/Family: -
	Author: department: "Automatisierung" ; person in charge / contact: C.Nottelmann (Automated Intelligence)
	Target System: CPU 1518F-4 PN/DP
	Engineering: TIA Portal V20
	Restrictions: -
	Requirements: -
	-----------------------------------------------------------------------------------------
	Change log table:
	Version     | Date       | Signature (Safety) | Expert in charge | Changes applied
	------------|------------|--------------------|------------------|-----------------------
	001.000.000 | 2025-08-26 | -                  |                  | Header added on 26.08.2025
	                                                                   
	*)
	END_REGION
	
	
	CASE #n_Sequence OF
	    #c000_CheckNoError:
	        //Action
	        
	        
	        
	        //Transition
	        IF NOT #udt_InOutHandshakeMES.str_State.b_Error
	        THEN
	            #n_Sequence := #c010_WaitForStart;
	        END_IF;
	        
	    #c010_WaitForStart:
	        //Action
	        #x_OutDone := False;
	        #x_OutReady := True;
	        
	        //Transition
	        FOR #n_tempSearchIdx := 1 TO "cNoPositions" DO
	            IF #x_InSendMessage AND #arrstr_InOutMessageData[#n_tempSearchIdx].n_PartID > 0 AND #arrstr_InOutMessageData[#n_tempSearchIdx].#n_MessageType > 0
	            THEN
	                #n_tempSearchIdxsave := #n_tempSearchIdx;
	                #n_Sequence := #c020_SetNewMessage;
	            END_IF;
	            EXIT;
	        END_FOR;
	        
	        
	        
	        
	    #c020_SetNewMessage:
	        //Action
	        #udt_InOutHandshakeMES.str_Ctrl.b_NewMessage := True;
	        #x_OutReady := False;
	        #x_OutBusy := True;
	        
	        //Transition
	        IF #udt_InOutHandshakeMES.str_State.b_Ready
	        THEN
	            #n_Sequence := #c030_SendMessage;
	        END_IF;
	        
	        
	    #c030_SendMessage:
	        //Action
	        IF #arrstr_InOutMessageData[#n_tempSearchIdxsave].#n_MessageType = #c01_PartGefunden
	        THEN
	            #udt_InOutHandshakeMES.str_Ctrl.udt_MessageData.nReasonCode := #c01_PartGefunden;
	            
	        ELSIF #arrstr_InOutMessageData[#n_tempSearchIdxsave].#n_MessageType = #c02_PartNichtGefunden
	        THEN
	            #udt_InOutHandshakeMES.str_Ctrl.udt_MessageData.nReasonCode := #c02_PartNichtGefunden;
	            
	        ELSE
	            ;
	        END_IF;
	        
	        #udt_InOutHandshakeMES.str_Ctrl.udt_MessageData.nBarcode := #arrstr_InOutMessageData[#n_tempSearchIdxsave].#n_PartID;
	        
	        
	        
	        
	        //Transtition
	        IF #udt_InOutHandshakeMES.str_State.b_MessageReceived
	        THEN
	            #n_Sequence := #c040_ResetMessageData;
	        END_IF;
	        
	    #c040_ResetMessageData:
	        //Action
	        #x_OutBusy := False;
	        #x_OutDone := True;
	        #arrstr_InOutMessageData[#n_tempSearchIdxsave].#n_MessageType := 0;
	        #arrstr_InOutMessageData[#n_tempSearchIdxsave].n_PartID := 0;
	        #udt_InOutHandshakeMES.str_Ctrl.udt_MessageData.nReasonCode := 0;
	        #udt_InOutHandshakeMES.str_Ctrl.udt_MessageData.nBarcode:= 0;
	        #udt_InOutHandshakeMES.str_Ctrl.b_NewMessage := False;
	        #n_tempSearchIdxsave := 1;
	        
	        //Transition
	        IF NOT #udt_InOutHandshakeMES.str_State.b_MessageReceived
	        THEN
	            #n_Sequence := #c000_CheckNoError;
	        END_IF;
	        
	        
	        
	        
	        
	        
	END_CASE;
	
	
	
	
	
	
	
END_FUNCTION_BLOCK

