FUNCTION_BLOCK "FB_TROHandshakeAMR"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      xInNewDestination : Bool;
      nInDestination : Int;
   END_VAR

   VAR_IN_OUT 
      udt_InOutTRO : "UDT_MFR_StandardTRO";
      st_InOutJamDestData : "stJamData";
      arCarrier : Array[*] of "stCarrier";
   END_VAR

   VAR 
      n_Sequenz : UInt;
      nUpperBound : DInt;
      nLowerBound : DInt;
      nBarcodeSave : DInt;
   END_VAR

   VAR CONSTANT 
      c000_Wait : UInt := 0;
      c010_SetStop : UInt := 10;
      c020_NewDestination : UInt := 20;
      c030_Reset : UInt := 30;
      c040_WaitReady : UInt := 40;
      c050_ResetStop : UInt := 50;
   END_VAR


BEGIN
	
	
	
	
	REGION Arraygröße auslesen
	    
	    #nUpperBound:=  UPPER_BOUND(ARR := #arCarrier, DIM := 1);
	    #nLowerBound := LOWER_BOUND(ARR := #arCarrier, DIM := 1);
	    
	END_REGION
	
	
	
	REGION Sequenz
	    
	    IF #nBarcodeSave <> #arCarrier[#nLowerBound].nBarcode
	    THEN
	        #n_Sequenz := #c000_Wait;
	    END_IF;
	    
	    
	    CASE #n_Sequenz OF
	        #c000_Wait:
	            //Action
	            #udt_InOutTRO.stCtrl.xStop := FALSE;
	            #udt_InOutTRO.stCtrl.xReset := FALSE;
	            #nBarcodeSave := #arCarrier[#nLowerBound].nBarcode;
	            //Transition
	            IF #xInNewDestination AND #st_InOutJamDestData.Status.nCount < #st_InOutJamDestData.Config.nCapacitySet
	            THEN
	                #n_Sequenz := #c010_SetStop;
	            END_IF;
	            
	        #c010_SetStop:
	            //Action
	            #udt_InOutTRO.stCtrl.xStop := True;
	            
	            
	            //Transition
	            IF NOT #udt_InOutTRO.stState.xReady
	            THEN
	                #n_Sequenz := #c020_NewDestination;
	            END_IF;
	            
	        #c020_NewDestination:
	            //Action
	            #arCarrier[#nUpperBound].nDestination := #nInDestination;
	            
	            //Transiton
	            #n_Sequenz := #c030_Reset;
	            
	        #c030_Reset:
	            //Action
	            #udt_InOutTRO.stCtrl.xReset := True;
	            
	            //Transition
	            IF NOT #udt_InOutTRO.stState.xReady
	            THEN
	                #n_Sequenz := #c040_WaitReady;
	            END_IF;
	            
	        #c040_WaitReady:
	            //Action
	            #udt_InOutTRO.stCtrl.xReset := FALSE;
	            
	            //Transition
	            IF #udt_InOutTRO.stState.xReady
	            THEN
	                #n_Sequenz := #c040_WaitReady;
	            END_IF;
	            
	        #c050_ResetStop:
	            //Action
	            #udt_InOutTRO.stCtrl.xStop := FALSE;
	            
	            //Transition
	            #n_Sequenz := #c000_Wait;
	            
	            
	        END_CASE;
	        
	    END_REGION
	
END_FUNCTION_BLOCK

