FUNCTION_BLOCK "FB_F2H_Management"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      xInQuit : Bool;
      wsIn_ObjectName : WString[50];
      udtIn_DataMES : "UDT_Telegram_GOHDRespM";
      t_InTRO505Watchdog : Time;
      t_InTRO510Watchdog : Time;
      t_InTRO514Watchdog : Time;
      t_InTRO518Watchdog : Time;
      t_InTRO522Watchdog : Time;
   END_VAR

   VAR_IN_OUT 
      stInOutJam5000_1 : Struct
         arCarrier : Array[1.."cJam5000_1"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5000_2 : Struct
         arCarrier : Array[1.."cJam5000_2"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5001_1 : Struct
         arCarrier : Array[1.."cJam5001_1"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5001_2 : Struct
         arCarrier : Array[1.."cJam5001_2"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5002_1 : Struct
         arCarrier : Array[1.."cJam5002_1"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5002_2 : Struct
         arCarrier : Array[1.."cJam5002_2"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5002_3 : Struct
         arCarrier : Array[1.."cJam5002_3"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5002_4 : Struct
         arCarrier : Array[1.."cJam5002_4"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5002_5 : Struct
         arCarrier : Array[1.."cJam5002_5"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5002_6 : Struct
         arCarrier : Array[1.."cJam5002_6"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5002_7 : Struct
         arCarrier : Array[1.."cJam5002_7"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5002_8 : Struct
         arCarrier : Array[1.."cJam5002_8"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5002_9 : Struct
         arCarrier : Array[1.."cJam5002_9"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5002_10 : Struct
         arCarrier : Array[1.."cJam5002_10"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5003_1 : Struct
         arCarrier : Array[1.."cJam5003_1"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5003_2 : Struct
         arCarrier : Array[1.."cJam5003_2"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5004_1 : Struct
         arCarrier : Array[1.."cJam5004_1"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5004_2 : Struct
         arCarrier : Array[1.."cJam5004_2"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5005_1 : Struct
         arCarrier : Array[1.."cJam5005_1"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5005_2 : Struct
         arCarrier : Array[1.."cJam5005_2"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5005_3 : Struct
         arCarrier : Array[1.."cJam5005_3"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5006_1 : Struct
         arCarrier : Array[1.."cJam5006_1"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5006_2 : Struct
         arCarrier : Array[1.."cJam5006_2"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5006_3 : Struct
         arCarrier : Array[1.."cJam5006_3"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5007_1 : Struct
         arCarrier : Array[1.."cJam5007_1"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5007_2 : Struct
         arCarrier : Array[1.."cJam5007_2"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5007_3 : Struct
         arCarrier : Array[1.."cJam5007_3"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5008_1 : Struct
         arCarrier : Array[1.."cJam5008_1"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5008_2 : Struct
         arCarrier : Array[1.."cJam5008_2"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      stInOutJam5008_3 : Struct
         arCarrier : Array[1.."cJam5008_3"] of "stCarrier";
         stData : "stJamData";
      END_STRUCT;
      udt_InOut_Station1_1 : "UDT_Workstation";
      udt_InOut_Station1 : "UDT_Workstation";
      udt_InOut_Station2 : "UDT_Workstation";
      udt_InOut_Station3 : "UDT_Workstation";
      udt_InOut_Station4 : "UDT_Workstation";
      udt_TRO503 : "UDT_MFR_StandardTRO";
      udt_TRO501 : "UDT_MFR_StandardTRO";
      udt_TRO505 : "UDT_MFR_StandardTRO";
      udt_TRO510 : "UDT_MFR_StandardTRO";
      udt_TRO514 : "UDT_MFR_StandardTRO";
      udt_TRO518 : "UDT_MFR_StandardTRO";
      udt_TRO522 : "UDT_MFR_StandardTRO";
      udt_InOut_HandshakeDataMES : "udt_HandshakeMessageFromMES";
   END_VAR

   VAR 
      udt_DeleteBuffer { S7_SetPoint := 'False'} : "UDT_Telegram_GOHDRespM";
      xDSucheAbgeschlossen : Bool;
      xSucheStart : Bool;
      n_FreierIndex : USInt;
      n_JamAreaIDX : USInt;
      xIDGefunden : Bool;
      xIDBereitsVorhanden : Bool;
      xIDHinzugefügt : Bool;
      xWaitingForRelease : Bool;
      x_Release : Bool;
      xCarrierFound : Bool;
      xLoadedEntranceActive : Bool;
      xEmptyEntranceActive : Bool;
      n_IDXSearchData { S7_SetPoint := 'True'} : USInt;
      n_UpperBound : USInt;
      n_LowerBound : USInt;
      n_DatenBufferIDX : USInt;
      nSearchCarrierIDX : Int;
      nSearchCarrierIDestination : Int;
      n_DatenJamArea : USInt;
      nIDWithoutDest : DInt;
      n_ScanBarcode : DInt;
   END_VAR
   VAR RETAIN
      n_EntranceSequenceLoaded : DInt;
      n_EntranceSequenceEmpty : DInt;
   END_VAR
   VAR 
      st_JamAreaCarrier : Struct
         st_Data { S7_SetPoint := 'False'} : "stCarrier";
         str_Type { S7_SetPoint := 'True'} : String;
      END_STRUCT;
      lfb_TRO505Scan { S7_SetPoint := 'False'} : "FB_ScanMonitor";
      lfb_TRO510Scan : "FB_ScanMonitor";
      lfb_TRO514Scan : "FB_ScanMonitor";
      lfb_TRO518Scan : "FB_ScanMonitor";
      lfb_TRO522Scan : "FB_ScanMonitor";
      lfb_TRO505Handshake : "FB_TROHandshakeAMR";
      lfb_TRO510Handshake : "FB_TROHandshakeAMR";
      lfb_TRO514Handshake : "FB_TROHandshakeAMR";
      lfb_TRO518Handshake { S7_SetPoint := 'False'} : "FB_TROHandshakeAMR";
      lfb_TRO522Handshake { S7_SetPoint := 'False'} : "FB_TROHandshakeAMR";
      x_TRO505NewDestination { S7_SetPoint := 'True'} : Bool;
      x_TRO510NewDestination : Bool;
      x_TRO514NewDestination : Bool;
      x_TRO518NewDestination : Bool;
      x_TRO522NewDestination : Bool;
   END_VAR
   VAR RETAIN
      nStationSaveForEmptyCarrier : USInt;
      nMinCountEmptyStation : USInt;
   END_VAR
   VAR 
      ton_WatchdogStep10 {InstructionName := 'TON_TIME'; LibVersion := '1.0'; S7_SetPoint := 'False'} : TON_TIME;
      ton_WatchdogStep20 {InstructionName := 'TON_TIME'; LibVersion := '1.0'} : TON_TIME;
      ton_WatchdogStep110 {InstructionName := 'TON_TIME'; LibVersion := '1.0'; S7_SetPoint := 'False'} : TON_TIME;
      ton_WatchdogStep120 {InstructionName := 'TON_TIME'; LibVersion := '1.0'} : TON_TIME;
      nErrorCounterLoaded { S7_SetPoint := 'True'} : Int;
      nErrorCounterEmpty : Int;
      nErrorIDEmpty : Int;
      nErrorIDLoaded : Int;
      xErrorLoaded : Bool;
      xErrorEmpty : Bool;
      PrgAlarm_FailmessageEmpty {InstructionName := 'Program_Alarm'; LibVersion := '1.0'; S7_SetPoint := 'False'} : Program_Alarm;
      PrgAlarm_FailmessageLoaded {InstructionName := 'Program_Alarm'; LibVersion := '1.0'} : Program_Alarm;
      wsErrorMessage1Empty { S7_SetPoint := 'True'} : WString[50];
      wsErrorMessage2Empty : WString[50];
      wsErrorMessage3Empty : WString[50];
      wsErrorMessage1Loaded { S7_SetPoint := 'True'} : WString[50];
      wsErrorMessage2Loaded : WString[50];
      wsErrorMessage3Loaded : WString[50];
   END_VAR
   VAR RETAIN
      nDestinationEmptySave : Int;
   END_VAR
   VAR 
      n_StepBufferMES : Int;
      udt_DataMESSave { S7_SetPoint := 'False'} : "UDT_Payload_GOHDRespM";
      n_EmptyElementIDX { S7_SetPoint := 'True'} : Int;
      n_EmptyElementIDXSave : Int;
      n_CounterDestination : USInt;
   END_VAR

   VAR_TEMP 
      str_FillingLevel : Struct
         n_temp5000_1 : USInt;
         n_temp5000_2 : USInt;
         n_temp5001_1 : USInt;
         n_temp5001_2 : USInt;
         n_temp5002_1 : USInt;
         n_temp5002_2 : USInt;
         n_temp5002_3 : USInt;
         n_temp5002_4 : USInt;
         n_temp5002_5 : USInt;
         n_temp5002_6 : USInt;
         n_temp5002_7 : USInt;
         n_temp5002_8 : USInt;
         n_temp5002_9 : USInt;
         n_temp5002_10 : USInt;
         n_temp5003_1 : USInt;
         n_temp5003_2 : USInt;
         n_temp5004_1 : USInt;
         n_temp5004_2 : USInt;
         n_temp5005_1 : USInt;
         n_temp5005_2 : USInt;
         n_temp5005_3 : USInt;
         n_temp5006_1 : USInt;
         n_temp5006_2 : USInt;
         n_temp5006_3 : USInt;
         n_temp5007_1 : USInt;
         n_temp5007_2 : USInt;
         n_temp5007_3 : USInt;
         n_temp5008_1 : USInt;
         n_temp5008_2 : USInt;
         n_temp5008_3 : USInt;
      END_STRUCT;
      nTempRetVal : Int;
      tTempLocalTime {InstructionName := 'DTL'; LibVersion := '1.0'} : DTL;
   END_VAR

   VAR CONSTANT 
      cMultiPurposeStation1 : Int := 1;
      cMultiPurposeStation2 : Int := 2;
      cMultiPurposeStation3 : Int := 3;
      cStationActive : Int := 1;
      c011Workstation11 : USInt := 11;   // Workstation 1.1
      c010Workstation1 : USInt := 10;   // Workstation 1.0
      c020Workstation2 : USInt := 20;   // Workstation 2.0
      c030Workstation3 : USInt := 30;   // Workstation 3.0
      c040Workstation4 : USInt := 40;   // Workstation 4.0
      "~~~~Sequence~~~~~~~" : Int;
      c000_Wait : Int;
      c002_WaitForNewData : Int := 2;
      c004_ReadNewData : Int := 4;
      c006_WriteData : Int := 6;
      c010_CheckDestination : Int := 10;
      c020_WaitCarrierGone : Int := 20;
      c030_StopTRO : Int := 30;
      c900_Error : Int := 900;
      c910_ErrorReset : Int := 910;
      "~~~~~Destination~~~~~" : Int;
      cDestinationJam5002_9 : Int := 529;
      "~~~~~~~FunktionCode~~~~~" : Int;
      c000_undefiniert : Int;
      c002_LeereCarrier : Int := 2;
      c001_BeladeneCarrier : Int := 1;
      "~~~~~~ActivationCode~~~~~~" : Int;
      c0Inactive : Int;
      c1Active : Int := 1;
      "~~~~ErrorID~~~~~" : Int;
      c010EmptyDestinationNotRdy : Int := 10;
      c020EmptyTROFail : Int := 20;
      c110LoadedDestinationNotRdy : Int := 110;
      c120LoadedTROFail : Int := 120;
      c000_NoError : Int;
      "~~~~~~~~BufferSequence~~~~~~~~" : Int;
      c000_Wait_1 : Int;
      c010_NewData : Int := 10;
      c020_SearchEmptyElement : Int := 20;
      c030_Finish : Int := 30;
      c040_Reset : Int := 40;
   END_VAR


BEGIN
	
	REGION Info
	(* Company: Schönenberger Systeme GmbH / (C) Copyright (2022). All rights reserved.
	-----------------------------------------------------------------------------------------
	Title: FB_F2H_Management
	Comment/Function: manages carrier way finding in area AMR
	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-03-08 | -                  |                  | Header added on 08.03.2025
	                                                                   
	*)
	END_REGION
	
	REGION ToDo
	    (*
	    - volle Carrier direkt am Scanner bearbeiten, kein Buffer ***CHECK***
	    - Activation Status berücksichtigen ***CHECK***
	    
	    - Leere Carrier bei Moduswechsel rausfahren ***TODO***
	    *)
	    
	END_REGION
	
	REGION Local Time
	    
	    
	    // read local time for timestamp ------------------------------------------
	    #nTempRetVal := RD_LOC_T(#tTempLocalTime);
	    
	    
	END_REGION
	
	REGION Station Counts
	    //Wurde anders gelöst, keine Zähler die hoch und runtergezählt werden, sondern die Daten der Carrier werden ausgewertet
	    
	    // #udt_InOut_Station1_1.nCountReal := UINT_TO_INT(#stInOutJam5003_1.stData.Status.nCount);
	    // #udt_InOut_Station1_1.nCountTotal := #udt_InOut_Station1_1.nCountReal + #udt_InOut_Station1_1.nCountReserved;
	    
	    // #udt_InOut_Station1.nCountReal := UINT_TO_INT(#stInOutJam5005_1.stData.Status.nCount);
	    // #udt_InOut_Station1.nCountTotal := #udt_InOut_Station1.nCountReal + #udt_InOut_Station1.nCountReserved;
	    
	    // #udt_InOut_Station2.nCountReal := UINT_TO_INT(#stInOutJam5006_1.stData.Status.nCount);
	    // #udt_InOut_Station2.nCountTotal := #udt_InOut_Station2.nCountReal + #udt_InOut_Station2.nCountReserved;
	    
	    // #udt_InOut_Station3.nCountReal := UINT_TO_INT(#stInOutJam5007_1.stData.Status.nCount);
	    // #udt_InOut_Station3.nCountTotal := #udt_InOut_Station3.nCountReal + #udt_InOut_Station3.nCountReserved;
	    
	    // #udt_InOut_Station4.nCountReal := UINT_TO_INT(#stInOutJam5008_1.stData.Status.nCount);
	    // #udt_InOut_Station4.nCountTotal := #udt_InOut_Station4.nCountReal + #udt_InOut_Station4.nCountReserved;
	    
	    
	END_REGION
	
	
	REGION Analyse needs of empty carrier
	    
	    //Search Station under min. buffer state
	    #nStationSaveForEmptyCarrier := 0;
	    
	    IF #udt_InOut_Station1_1.nActivationStatus = #c1Active
	        AND #udt_InOut_Station1_1.nFunctionCode = #c002_LeereCarrier
	        AND #udt_InOut_Station1_1.nCountTotal < 7
	    THEN
	        #nStationSaveForEmptyCarrier := #c011Workstation11;
	    END_IF;
	    
	    IF #udt_InOut_Station1.nActivationStatus = #c1Active
	        AND #udt_InOut_Station1.nFunctionCode = #c002_LeereCarrier
	        AND #udt_InOut_Station1.nCountTotal < 7
	    THEN
	        #nStationSaveForEmptyCarrier := #c010Workstation1;
	    END_IF;
	    
	    IF #udt_InOut_Station2.nActivationStatus = #c1Active
	        AND #udt_InOut_Station2.nFunctionCode = #c002_LeereCarrier
	        AND #udt_InOut_Station2.nCountTotal < 7
	    THEN
	        #nStationSaveForEmptyCarrier := #c020Workstation2;
	    END_IF;
	    
	    IF #udt_InOut_Station3.nActivationStatus = #c1Active
	        AND #udt_InOut_Station3.nFunctionCode = #c002_LeereCarrier
	        AND #udt_InOut_Station3.nCountTotal < 7
	    THEN
	        #nStationSaveForEmptyCarrier := #c030Workstation3;
	    END_IF;
	    
	    IF #udt_InOut_Station4.nActivationStatus = #c1Active
	        AND #udt_InOut_Station4.nFunctionCode = #c002_LeereCarrier
	        AND #udt_InOut_Station4.nCountTotal < 50//7
	    THEN
	        #nStationSaveForEmptyCarrier := #c040Workstation4;
	    END_IF;
	    
	    //Find Station with the lowest buffer
	    IF #nStationSaveForEmptyCarrier = 0
	    THEN
	        #nMinCountEmptyStation := 20;//15;
	        
	        IF #udt_InOut_Station1_1.nActivationStatus = #c1Active
	            AND #udt_InOut_Station1_1.nFunctionCode = #c002_LeereCarrier
	            AND #udt_InOut_Station1_1.nCountTotal < #nMinCountEmptyStation
	        THEN
	            #nMinCountEmptyStation := INT_TO_USINT( #udt_InOut_Station1_1.nCountTotal);
	            #nStationSaveForEmptyCarrier := #c011Workstation11;
	        END_IF;
	        
	        IF #udt_InOut_Station1.nActivationStatus = #c1Active
	            AND #udt_InOut_Station1.nFunctionCode = #c002_LeereCarrier
	            AND #udt_InOut_Station1.nCountTotal < #nMinCountEmptyStation
	        THEN
	            #nMinCountEmptyStation := INT_TO_USINT(#udt_InOut_Station1.nCountTotal);
	            #nStationSaveForEmptyCarrier := #c010Workstation1;
	        END_IF;
	        
	        IF #udt_InOut_Station2.nActivationStatus = #c1Active
	            AND #udt_InOut_Station2.nFunctionCode = #c002_LeereCarrier
	            AND #udt_InOut_Station2.nCountTotal < #nMinCountEmptyStation
	        THEN
	            #nMinCountEmptyStation := INT_TO_USINT(#udt_InOut_Station2.nCountTotal);
	            #nStationSaveForEmptyCarrier := #c020Workstation2;
	        END_IF;
	        
	        IF #udt_InOut_Station3.nActivationStatus = #c1Active
	            AND #udt_InOut_Station3.nFunctionCode = #c002_LeereCarrier
	            AND #udt_InOut_Station3.nCountTotal < #nMinCountEmptyStation
	        THEN
	            #nMinCountEmptyStation := INT_TO_USINT(#udt_InOut_Station3.nCountTotal);
	            #nStationSaveForEmptyCarrier := #c030Workstation3;
	        END_IF;
	        
	        IF #udt_InOut_Station4.nActivationStatus = #c1Active
	            AND #udt_InOut_Station4.nFunctionCode = #c002_LeereCarrier
	            AND #udt_InOut_Station4.nCountTotal < #nMinCountEmptyStation
	        THEN
	            #nMinCountEmptyStation := INT_TO_USINT(#udt_InOut_Station4.nCountTotal);
	            #nStationSaveForEmptyCarrier := #c040Workstation4;
	        END_IF;
	        
	    END_IF;
	    
	END_REGION
	
	
	
	
	REGION F2H Entrance handshake loaded
	    
	    CASE #n_EntranceSequenceLoaded OF
	        #c000_Wait:
	            REGION c000_Wait
	                //Action
	                #udt_TRO501.stCtrl.xStop := True;
	                #nErrorCounterLoaded := 1;
	                
	                IF #stInOutJam5001_2.arCarrier[1].nDestination > 0 THEN
	                    #stInOutJam5001_2.arCarrier[1].nDestination := 0;
	                END_IF;
	                
	                //Transition
	                IF //#udt_TRO501.stScanResult.xCarrierPresent
	                    #stInOutJam5001_2.arCarrier[1].nBarcode > 2000 AND #stInOutJam5001_2.arCarrier[1].bStatus.%X0
	                    AND #stInOutJam5001_2.arCarrier[1].nDestination = 0
	             //       AND NOT #udt_InOut_HandshakeDataMES.str_Ctrl.xNewDataAvaible
	                THEN
	                    #n_EntranceSequenceLoaded := #c002_WaitForNewData;
	                END_IF;
	            END_REGION
	
	            
	        #c002_WaitForNewData:
	            REGION c002_WaitForNewData
	                //Action
	                
	                
	                //Transition
	                IF #udt_InOut_HandshakeDataMES.str_Ctrl.xNewDataAvaible
	                THEN
	                    #n_EntranceSequenceLoaded := #c004_ReadNewData;
	                END_IF;
	            END_REGION
	
	            
	        #c004_ReadNewData:
	            
	            REGION c004_ReadNewData
	                //Action
	                #udt_DataMESSave := #udtIn_DataMES.stPayload;
	                #udt_InOut_HandshakeDataMES.str_Status.xReadingNewData := True;
	                
	                //Transition
	                IF #udt_DataMESSave.nBarcode > 0
	                THEN
	                    #n_EntranceSequenceLoaded := #c006_WriteData;
	                    //Else Error
	                END_IF;
	            END_REGION
	
	            
	        #c006_WriteData:
	            
	            REGION c006_WriteData
	                //Action
	                
	                #stInOutJam5001_2.arCarrier[1].nDestination := STRING_TO_INT(#udt_DataMESSave.arActions[#nErrorCounterLoaded]);
	                #udt_InOut_HandshakeDataMES.str_Status.xNewDataSaved := True;
	                #udt_InOut_HandshakeDataMES.str_Status.xReadingNewData := False;
	                
	                
	                // 1. unten udt_DataMESSave verwenden? warum udtIn_DataMES?
	                // 2. Prüfen ob der Barcode vom Telegram auch dem Barcode an erster Stelle entspricht, nicht nur > 0
	                // 3. wir haben hier einen Scanner, also müsste der Barcode im Jam und der Barcode vom Telegram theoretisch immer passen, ansonsten Fehler
	                
	                
	                //Transition
	                IF
	                    //#udtIn_DataMES.stPayload.nBarcode > 0
	                    NOT #udt_InOut_HandshakeDataMES.str_Ctrl.xNewDataAvaible 
	                THEN
	                    #n_EntranceSequenceLoaded := #c010_CheckDestination;
	                END_IF;
	            END_REGION
	           
	        #c010_CheckDestination:
	            REGION c010_CheckDestination
	                //Action
	                
	                //Transition
	                CASE #stInOutJam5001_2.arCarrier[1].nDestination OF
	                    #c011Workstation11:
	                        IF #stInOutJam5002_1.stData.Status.nCount < #stInOutJam5002_1.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_2.stData.Status.nCount < #stInOutJam5002_2.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station1_1.nCountTotal < #stInOutJam5003_1.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station1_1.nActivationStatus = 1
	                            AND #udt_InOut_Station1_1.nFunctionCode = #c001_BeladeneCarrier
	                            //AND NOT #xEmptyEntranceActive
	                        THEN
	                            #udt_TRO501.stCtrl.xStop := FALSE;
	                            //#xLoadedEntranceActive := True;
	                            // reserviert Zähler erhöhen 
	                            //#udt_InOut_Station1_1.nCountReserved += 1;
	                            #n_EntranceSequenceLoaded := #c020_WaitCarrierGone;
	                        END_IF;
	                        
	                    #c010Workstation1:
	                        IF #stInOutJam5002_1.stData.Status.nCount < #stInOutJam5002_1.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_2.stData.Status.nCount < #stInOutJam5002_2.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_3.stData.Status.nCount < #stInOutJam5002_3.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station1.nCountTotal < #stInOutJam5005_1.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station1.nActivationStatus = 1
	                            AND #udt_InOut_Station1.nFunctionCode = #c001_BeladeneCarrier
	                            //AND NOT #xEmptyEntranceActive
	                        THEN
	                            #udt_TRO501.stCtrl.xStop := FALSE;
	                            //#xLoadedEntranceActive := True;
	                            // reserviert Zähler erhöhen
	                            //#udt_InOut_Station1.nCountReserved += 1;
	                            #n_EntranceSequenceLoaded := #c020_WaitCarrierGone;
	                        END_IF;
	                        
	                    #c020Workstation2:
	                        IF #stInOutJam5002_1.stData.Status.nCount < #stInOutJam5002_1.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_2.stData.Status.nCount < #stInOutJam5002_2.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_3.stData.Status.nCount < #stInOutJam5002_3.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_4.stData.Status.nCount < #stInOutJam5002_4.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station2.nCountTotal < #stInOutJam5006_1.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station2.nActivationStatus = 1
	                            AND #udt_InOut_Station2.nFunctionCode = #c001_BeladeneCarrier
	                            // AND NOT #xEmptyEntranceActive
	                        THEN
	                            #udt_TRO501.stCtrl.xStop := FALSE;
	                            //#xLoadedEntranceActive := True;
	                            // reserviert Zähler erhöhen 
	                            // #udt_InOut_Station2.nCountReserved += 1;
	                            #n_EntranceSequenceLoaded := #c020_WaitCarrierGone;
	                        END_IF;
	                        
	                    #c030Workstation3:
	                        IF #stInOutJam5002_1.stData.Status.nCount < #stInOutJam5002_1.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_2.stData.Status.nCount < #stInOutJam5002_2.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_3.stData.Status.nCount < #stInOutJam5002_3.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_4.stData.Status.nCount < #stInOutJam5002_4.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_5.stData.Status.nCount < #stInOutJam5002_5.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station3.nCountTotal < #stInOutJam5007_1.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station3.nActivationStatus = 1
	                            AND #udt_InOut_Station3.nFunctionCode = #c001_BeladeneCarrier
	                            //AND NOT #xEmptyEntranceActive
	                        THEN
	                            #udt_TRO501.stCtrl.xStop := FALSE;
	                            //#xLoadedEntranceActive := True;
	                            // reserviert Zähler erhöhen 
	                            // #udt_InOut_Station3.nCountReserved += 1;
	                            #n_EntranceSequenceLoaded := #c020_WaitCarrierGone;
	                        END_IF;
	                        
	                    #c040Workstation4:
	                        IF #stInOutJam5002_1.stData.Status.nCount < #stInOutJam5002_1.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_2.stData.Status.nCount < #stInOutJam5002_2.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_3.stData.Status.nCount < #stInOutJam5002_3.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_4.stData.Status.nCount < #stInOutJam5002_4.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_5.stData.Status.nCount < #stInOutJam5002_5.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_6.stData.Status.nCount < #stInOutJam5002_6.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station4.nCountTotal < #stInOutJam5008_1.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station4.nActivationStatus = 1
	                            AND #udt_InOut_Station4.nFunctionCode = #c001_BeladeneCarrier
	                            //AND NOT #xEmptyEntranceActive
	                        THEN
	                            #udt_TRO501.stCtrl.xStop := FALSE;
	                            //#xLoadedEntranceActive := True;
	                            // reserviert Zähler erhöhen 
	                            // #udt_InOut_Station4.nCountReserved += 1;
	                            #n_EntranceSequenceLoaded := #c020_WaitCarrierGone;
	                        END_IF;
	                        
	                END_CASE;
	                
	                IF #ton_WatchdogStep110.Q
	                    AND #nErrorCounterLoaded < 4
	                THEN
	                    #nErrorCounterLoaded += 1;
	                    #n_EntranceSequenceLoaded := #c006_WriteData;
	                ELSIF #ton_WatchdogStep110.Q
	                    AND #nErrorCounterLoaded >= 4
	                THEN
	                    #nErrorIDEmpty := #c110LoadedDestinationNotRdy;
	                    #n_EntranceSequenceLoaded := #c900_Error;
	                END_IF;
	                
	                
	                
	            END_REGION
	
	        #c020_WaitCarrierGone:
	            REGION c020_WaitCarrierGone
	                //Action
	                #nErrorCounterLoaded := 1;
	                #udt_InOut_HandshakeDataMES.str_Status.xReadingNewData := False;
	                #udt_InOut_HandshakeDataMES.str_Status.xNewDataSaved := False;
	                #udt_InOut_HandshakeDataMES.str_Status.xDataIncorrect := False;
	                
	                //Transition
	                IF (* NOT #udt_TRO501.stScanResult.xCarrierPresent *)
	                    (* #stInOutJam5001_2.arCarrier[1].nBarcode > 0 AND #stInOutJam5001_2.arCarrier[1].bStatus.%X0
	                    AND *)#stInOutJam5001_2.arCarrier[1].nDestination = 0 //TEST
	                THEN
	                    #n_EntranceSequenceLoaded := #c030_StopTRO;
	                ELSIF #ton_WatchdogStep120.Q
	                THEN
	                    #nErrorIDLoaded := #c120LoadedTROFail;
	                    #udt_TRO503.stCtrl.xStop := TRUE;
	                    #n_EntranceSequenceLoaded := #c900_Error;
	                END_IF;
	            END_REGION
	
	            
	        #c030_StopTRO:
	            REGION c030_StopTRO
	                //Action
	                #udt_TRO501.stCtrl.xStop := TRUE;
	                //#xLoadedEntranceActive := FALSE;      PrioManager geregelt
	                //Transition
	                #n_EntranceSequenceLoaded := #c000_Wait;
	            END_REGION
	           
	        #c900_Error:
	            REGION c900_Error
	                //Action
	                #xErrorLoaded := True;
	                
	                //Transition
	                IF #xInQuit
	                THEN
	                    #n_EntranceSequenceLoaded := #c910_ErrorReset;
	                END_IF;
	            END_REGION
	
	            
	        #c910_ErrorReset:
	            REGION c910_ErrorReset
	                //Action
	                #xErrorLoaded := False;
	                #nErrorIDLoaded := 0;
	                #nErrorCounterLoaded := 0;
	                //Transition
	                #n_EntranceSequenceLoaded := #c000_Wait;
	            END_REGION
	            
	    END_CASE;
	END_REGION
	
	REGION F2H Entrance handshake empty
	    
	    CASE #n_EntranceSequenceEmpty OF
	        #c000_Wait:
	            REGION c000_Wait
	                //Action
	                #udt_TRO503.stCtrl.xStop := True;
	                
	                IF #stInOutJam5000_2.arCarrier[1].nDestination > 0 THEN
	                    #stInOutJam5000_2.arCarrier[1].nDestination := 0;
	                END_IF;
	                
	                //Transition
	                IF //#udt_TRO503.stScanResult.xCarrierPresent
	                    #stInOutJam5000_2.arCarrier[1].nBarcode > 2000 AND #stInOutJam5000_2.arCarrier[1].bStatus.%X0
	                    AND #stInOutJam5000_2.arCarrier[1].nDestination = 0
	                    AND #nStationSaveForEmptyCarrier > 0
	                THEN
	                    #nDestinationEmptySave := #nStationSaveForEmptyCarrier;
	                    #n_EntranceSequenceEmpty := #c010_CheckDestination;
	                END_IF;
	                
	            END_REGION
	            
	        #c010_CheckDestination:
	            REGION c010_CheckDestination
	                //Action
	                
	                //Transition
	                CASE #nStationSaveForEmptyCarrier OF
	                    #c011Workstation11:
	                        IF #stInOutJam5002_1.stData.Status.nCount < #stInOutJam5002_1.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_2.stData.Status.nCount < #stInOutJam5002_2.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station1_1.nCountTotal < (#stInOutJam5003_1.stData.Config.nCapacitySet + #stInOutJam5003_2.stData.Config.nCapacitySet)
	                            AND #udt_InOut_Station1_1.nActivationStatus = 1
	                            AND #udt_InOut_Station1_1.nFunctionCode = #c002_LeereCarrier
	                            //AND NOT #xLoadedEntranceActive
	                        THEN
	                            #udt_TRO503.stCtrl.xStop := FALSE;
	                            #stInOutJam5000_2.arCarrier[1].nDestination := #c011Workstation11;
	                            //#xEmptyEntranceActive := True;
	                            // reserviert Zähler erhöhen 
	                            //   #udt_InOut_Station1_1.nCountReserved += 1;
	                            #n_EntranceSequenceEmpty := #c020_WaitCarrierGone;
	                        END_IF;
	                        
	                    #c010Workstation1:
	                        IF #stInOutJam5002_1.stData.Status.nCount < #stInOutJam5002_1.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_2.stData.Status.nCount < #stInOutJam5002_2.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_3.stData.Status.nCount < #stInOutJam5002_3.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station1.nCountTotal < #stInOutJam5005_1.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station1.nActivationStatus = 1
	                            AND #udt_InOut_Station1.nFunctionCode = #c002_LeereCarrier
	                            //AND NOT #xLoadedEntranceActive
	                        THEN
	                            #udt_TRO503.stCtrl.xStop := FALSE;
	                            #stInOutJam5000_2.arCarrier[1].nDestination := #c010Workstation1;
	                            //#xEmptyEntranceActive := True;
	                            // reserviert Zähler erhöhen 
	                            //   #udt_InOut_Station1.nCountReserved += 1;
	                            #n_EntranceSequenceEmpty := #c020_WaitCarrierGone;
	                        END_IF;
	                        
	                    #c020Workstation2:
	                        IF #stInOutJam5002_1.stData.Status.nCount < #stInOutJam5002_1.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_2.stData.Status.nCount < #stInOutJam5002_2.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_3.stData.Status.nCount < #stInOutJam5002_3.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_4.stData.Status.nCount < #stInOutJam5002_4.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station2.nCountTotal < #stInOutJam5006_1.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station2.nActivationStatus = 1
	                            AND #udt_InOut_Station2.nFunctionCode = #c002_LeereCarrier
	                            //AND NOT #xLoadedEntranceActive
	                        THEN
	                            #udt_TRO503.stCtrl.xStop := FALSE;
	                            #stInOutJam5000_2.arCarrier[1].nDestination := #c020Workstation2;
	                            // #xEmptyEntranceActive := True;
	                            // reserviert Zähler erhöhen 
	                            //    #udt_InOut_Station2.nCountReserved += 1;
	                            #n_EntranceSequenceEmpty := #c020_WaitCarrierGone;
	                        END_IF;
	                        
	                    #c030Workstation3:
	                        IF #stInOutJam5002_1.stData.Status.nCount < #stInOutJam5002_1.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_2.stData.Status.nCount < #stInOutJam5002_2.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_3.stData.Status.nCount < #stInOutJam5002_3.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_4.stData.Status.nCount < #stInOutJam5002_4.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_5.stData.Status.nCount < #stInOutJam5002_5.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station3.nCountTotal < #stInOutJam5007_1.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station3.nActivationStatus = 1
	                            AND #udt_InOut_Station3.nFunctionCode = #c002_LeereCarrier
	                            // AND NOT #xLoadedEntranceActive
	                        THEN
	                            #udt_TRO503.stCtrl.xStop := FALSE;
	                            #stInOutJam5000_2.arCarrier[1].nDestination := #c030Workstation3;
	                            // #xEmptyEntranceActive := True;
	                            // reserviert Zähler erhöhen 
	                            //     #udt_InOut_Station3.nCountReserved += 1;
	                            #n_EntranceSequenceEmpty := #c020_WaitCarrierGone;
	                        END_IF;
	                        
	                    #c040Workstation4:
	                        IF (* #stInOutJam5002_1.stData.Status.nCount < #stInOutJam5002_1.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_2.stData.Status.nCount < #stInOutJam5002_2.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_3.stData.Status.nCount < #stInOutJam5002_3.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_4.stData.Status.nCount < #stInOutJam5002_4.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_5.stData.Status.nCount < #stInOutJam5002_5.stData.Config.nCapacitySet
	                            AND #stInOutJam5002_6.stData.Status.nCount < #stInOutJam5002_6.stData.Config.nCapacitySet
	                            AND #udt_InOut_Station4.nCountTotal < #stInOutJam5008_1.stData.Config.nCapacitySet *)
	                            (* AND *) #udt_InOut_Station4.nActivationStatus = 1
	                            AND #udt_InOut_Station4.nFunctionCode = #c002_LeereCarrier
	                            // AND NOT #xLoadedEntranceActive
	                        THEN
	                            #udt_TRO503.stCtrl.xStop := FALSE;
	                            #stInOutJam5000_2.arCarrier[1].nDestination := #c040Workstation4;
	                            //#xEmptyEntranceActive := True;
	                            // reserviert Zähler erhöhen 
	                            //     #udt_InOut_Station4.nCountReserved += 1;
	                            #n_EntranceSequenceEmpty := #c020_WaitCarrierGone;
	                        END_IF;
	                        
	                END_CASE;
	                
	                IF #ton_WatchdogStep10.Q
	                    AND #nErrorCounterEmpty < 5
	                THEN
	                    #nErrorCounterEmpty += 1;
	                    #n_EntranceSequenceEmpty := #c000_Wait;
	                ELSIF #ton_WatchdogStep10.Q
	                    AND #nErrorCounterEmpty >= 5
	                THEN
	                    #nErrorIDEmpty := #c010EmptyDestinationNotRdy;
	                    #n_EntranceSequenceEmpty := #c900_Error;
	                END_IF;
	            END_REGION
	  
	        #c020_WaitCarrierGone:
	            REGION c020_WaitCarrierGone
	                //Action
	                
	                
	                //Transition
	                IF #stInOutJam5000_2.arCarrier[1].nDestination = 0 //TEST//NOT #udt_TRO503.stScanResult.xCarrierPresent
	                THEN
	                    #n_EntranceSequenceEmpty := #c030_StopTRO;
	                ELSIF #ton_WatchdogStep20.Q
	                THEN
	                    #nErrorIDEmpty := #c020EmptyTROFail;
	                    #udt_TRO503.stCtrl.xStop := TRUE;
	                    #n_EntranceSequenceEmpty := #c900_Error;
	                END_IF;
	            END_REGION
	            
	        #c030_StopTRO:
	            REGION c030_StopTRO
	                //Action
	                #udt_TRO503.stCtrl.xStop := TRUE;
	                #stInOutJam5000_2.arCarrier[1].nDestination := 0;
	                // #xEmptyEntranceActive := False;
	                //Transition
	                #n_EntranceSequenceEmpty := #c000_Wait;
	            END_REGION
	       
	        #c900_Error:
	            //Action
	            #xErrorEmpty := True;
	            
	            //Transition
	            IF #xInQuit
	            THEN
	                #n_EntranceSequenceEmpty := #c910_ErrorReset;
	            END_IF;
	            
	        #c910_ErrorReset:
	            //Action
	            #xErrorEmpty := False;
	            #nErrorIDEmpty := 0;
	            #nErrorCounterEmpty := 0;
	            //Transition
	            #n_EntranceSequenceEmpty := #c000_Wait;
	            
	    END_CASE;
	END_REGION
	
	REGION Error Handling
	  
	        
	        //Quit setzt Error Word zurück
	        IF #xInQuit
	        THEN
	            #xErrorEmpty := False;
	        END_IF;
	        
	        IF #xErrorEmpty AND #nErrorIDEmpty = #c010EmptyDestinationNotRdy
	        THEN
	            
	            #wsErrorMessage1Empty := WString#'Empty Carrier For Station';
	            #wsErrorMessage2Empty := INT_TO_WSTRING(#stInOutJam5000_2.arCarrier[1].nDestination);
	            #wsErrorMessage3Empty := WSTRING#'Station not Ready';
	        ELSIF #xErrorEmpty AND #nErrorIDEmpty = #c020EmptyTROFail
	        THEN
	            
	            #wsErrorMessage1Empty := WString#'TRO503 For Station';
	            #wsErrorMessage2Empty := DINT_TO_WSTRING(#stInOutJam5000_2.arCarrier[1].nDestination);
	            #wsErrorMessage3Empty := WSTRING#'not releasing Carrier';
	        ELSIF #xErrorEmpty AND #nErrorIDEmpty = #c000_NoError
	        THEN
	            #wsErrorMessage1Empty := WString#'';
	            #wsErrorMessage2Empty := WString#'';
	            #wsErrorMessage3Empty := WSTRING#'';
	            
	        END_IF;
	        
	        #PrgAlarm_FailmessageEmpty(SIG := #xErrorEmpty,
	                              TIMESTAMP := #tTempLocalTime,
	                              SD_1 := #wsIn_ObjectName,
	                              SD_2 := #wsErrorMessage1Empty,
	                              SD_3 := #wsErrorMessage2Empty,
	                              SD_4 := #nErrorIDEmpty);
	        
	        
	        
	        
	   //Quit setzt Error Word zurück
	   IF #xInQuit
	   THEN
	       #xErrorLoaded := False;
	   END_IF;
	   
	   IF #xErrorLoaded AND #nErrorIDLoaded = #c110LoadedDestinationNotRdy
	   THEN
	       
	       #wsErrorMessage1Loaded := WString#'Loaded Carrier For Station';
	       #wsErrorMessage2Loaded := INT_TO_WSTRING(#stInOutJam5001_2.arCarrier[1].nDestination);
	       #wsErrorMessage3Loaded := WSTRING#'Station not Ready';
	   ELSIF #xErrorLoaded AND #nErrorIDLoaded = #c120LoadedTROFail
	   THEN
	       
	       #wsErrorMessage1Loaded := WString#'TRO501 For Station';
	       #wsErrorMessage2Loaded := DINT_TO_WSTRING(#stInOutJam5001_2.arCarrier[1].nDestination);
	       #wsErrorMessage3Loaded := WSTRING#'not releasing Carrier';
	   ELSIF #xErrorLoaded AND #nErrorIDLoaded = #c000_NoError
	   THEN
	       #wsErrorMessage1Loaded := WString#'';
	       #wsErrorMessage2Loaded := WString#'';
	       #wsErrorMessage3Loaded := WSTRING#'';
	       
	   END_IF;
	   
	   #PrgAlarm_FailmessageLoaded(SIG := #xErrorLoaded,
	                              TIMESTAMP := #tTempLocalTime,
	                              SD_1 := #wsIn_ObjectName,
	                              SD_2 := #wsErrorMessage1Loaded,
	                              SD_3 := #wsErrorMessage2Loaded,
	                              SD_4 := #nErrorIDLoaded);
	   
	        
	   
	    
	END_REGION
	
	
	REGION Kapazitaeten ermitteln
	    
	    REGION 5000
	        #str_FillingLevel.n_temp5000_1 := UINT_TO_USINT(#stInOutJam5000_1.stData.Status.nCount * 100 / INT_TO_UINT( "cJam5000_1"));
	        #str_FillingLevel.n_temp5000_2 := UINT_TO_USINT(#stInOutJam5000_2.stData.Status.nCount * 100 / INT_TO_UINT(  "cJam5000_2"));
	    END_REGION
	    
	    REGION 5001
	        #str_FillingLevel.n_temp5001_1 := UINT_TO_USINT(#stInOutJam5001_1.stData.Status.nCount * 100 / INT_TO_UINT("cJam5001_1"));
	        #str_FillingLevel.n_temp5001_2 := UINT_TO_USINT(#stInOutJam5001_2.stData.Status.nCount * 100 / INT_TO_UINT("cJam5001_2"));
	    END_REGION
	    
	    REGION 5002
	        #str_FillingLevel.n_temp5002_1 := UINT_TO_USINT(#stInOutJam5002_1.stData.Status.nCount * 100 / INT_TO_UINT("cJam5002_1"));
	        #str_FillingLevel.n_temp5002_2 := UINT_TO_USINT(#stInOutJam5002_2.stData.Status.nCount * 100 / INT_TO_UINT("cJam5002_2"));
	        #str_FillingLevel.n_temp5002_3 := UINT_TO_USINT(#stInOutJam5002_3.stData.Status.nCount * 100 / INT_TO_UINT("cJam5002_3"));
	        #str_FillingLevel.n_temp5002_4 := UINT_TO_USINT(#stInOutJam5002_4.stData.Status.nCount * 100 / INT_TO_UINT("cJam5002_4"));
	        #str_FillingLevel.n_temp5002_5 := UINT_TO_USINT(#stInOutJam5002_5.stData.Status.nCount * 100 / INT_TO_UINT("cJam5002_5"));
	        #str_FillingLevel.n_temp5002_6 := UINT_TO_USINT(#stInOutJam5002_6.stData.Status.nCount * 100 / INT_TO_UINT("cJam5002_6"));
	        #str_FillingLevel.n_temp5002_7 := UINT_TO_USINT(#stInOutJam5002_7.stData.Status.nCount * 100 / INT_TO_UINT("cJam5002_7"));
	        #str_FillingLevel.n_temp5002_8 := UINT_TO_USINT(#stInOutJam5002_8.stData.Status.nCount * 100 / INT_TO_UINT("cJam5002_8"));
	        #str_FillingLevel.n_temp5002_9 := UINT_TO_USINT(#stInOutJam5002_9.stData.Status.nCount * 100 / INT_TO_UINT("cJam5002_9"));
	        #str_FillingLevel.n_temp5002_10 := UINT_TO_USINT(#stInOutJam5002_10.stData.Status.nCount * 100 / INT_TO_UINT("cJam5002_10"));
	        
	    END_REGION
	    
	    REGION 5003
	        #str_FillingLevel.n_temp5003_1 := UINT_TO_USINT(#stInOutJam5003_1.stData.Status.nCount * 100 / INT_TO_UINT("cJam5003_1"));
	        #str_FillingLevel.n_temp5003_2 := UINT_TO_USINT(#stInOutJam5003_2.stData.Status.nCount * 100 / INT_TO_UINT("cJam5003_2"));
	        
	    END_REGION
	    
	    REGION 5004
	        #str_FillingLevel.n_temp5004_1 := UINT_TO_USINT(#stInOutJam5004_1.stData.Status.nCount * 100 / INT_TO_UINT("cJam5004_1"));
	        #str_FillingLevel.n_temp5004_2 := UINT_TO_USINT(#stInOutJam5004_2.stData.Status.nCount * 100 / INT_TO_UINT("cJam5004_2"));
	        
	    END_REGION
	    
	    REGION 5005
	        #str_FillingLevel.n_temp5005_1 := UINT_TO_USINT(#stInOutJam5005_1.stData.Status.nCount * 100 / INT_TO_UINT("cJam5005_1"));
	        #str_FillingLevel.n_temp5005_2 := UINT_TO_USINT(#stInOutJam5005_2.stData.Status.nCount * 100 / INT_TO_UINT("cJam5005_2"));
	        #str_FillingLevel.n_temp5005_3 := UINT_TO_USINT(#stInOutJam5005_3.stData.Status.nCount * 100 / INT_TO_UINT("cJam5005_3"));
	        
	    END_REGION
	    
	    REGION 5006
	        #str_FillingLevel.n_temp5006_1 := UINT_TO_USINT(#stInOutJam5006_1.stData.Status.nCount * 100 / INT_TO_UINT("cJam5006_1"));
	        #str_FillingLevel.n_temp5006_2 := UINT_TO_USINT(#stInOutJam5006_2.stData.Status.nCount * 100 / INT_TO_UINT("cJam5006_2"));
	        #str_FillingLevel.n_temp5006_3 := UINT_TO_USINT(#stInOutJam5006_3.stData.Status.nCount * 100 / INT_TO_UINT("cJam5006_3"));
	    END_REGION
	    
	    REGION 5007
	        #str_FillingLevel.n_temp5007_1 := UINT_TO_USINT(#stInOutJam5007_1.stData.Status.nCount * 100 / INT_TO_UINT("cJam5007_1"));
	        #str_FillingLevel.n_temp5007_2 := UINT_TO_USINT(#stInOutJam5007_2.stData.Status.nCount * 100 / INT_TO_UINT("cJam5007_2"));
	        #str_FillingLevel.n_temp5007_3 := UINT_TO_USINT(#stInOutJam5007_3.stData.Status.nCount * 100 / INT_TO_UINT("cJam5007_3"));
	        
	    END_REGION
	    
	    REGION 5008
	        #str_FillingLevel.n_temp5008_1 := UINT_TO_USINT(#stInOutJam5008_1.stData.Status.nCount * 100 / INT_TO_UINT ("cJam5008_1"));
	        #str_FillingLevel.n_temp5008_2 := UINT_TO_USINT(#stInOutJam5008_2.stData.Status.nCount * 100 / INT_TO_UINT("cJam5008_2"));
	        #str_FillingLevel.n_temp5008_3 := UINT_TO_USINT(#stInOutJam5008_3.stData.Status.nCount * 100 / INT_TO_UINT("cJam5008_3"));
	    END_REGION
	    
	
	    
	END_REGION
	
	
	
	
	
	
	REGION Seperator Monitoring
	    
	    
	    #lfb_TRO505Scan(t_InNoProgressTime := #t_InTRO505Watchdog,
	                    x_OutAssignNewDest => #x_TRO505NewDestination,
	                    udt_Scan_Result := #udt_TRO505.stScanResult);
	    
	    
	    #lfb_TRO510Scan(t_InNoProgressTime := #t_InTRO510Watchdog,
	                    x_OutAssignNewDest => #x_TRO510NewDestination,
	                    udt_Scan_Result := #udt_TRO510.stScanResult);
	    
	    
	    #lfb_TRO514Scan(t_InNoProgressTime := #t_InTRO514Watchdog,
	                    x_OutAssignNewDest => #x_TRO514NewDestination,
	                    udt_Scan_Result := #udt_TRO514.stScanResult);
	    
	        
	    #lfb_TRO518Scan(t_InNoProgressTime := #t_InTRO518Watchdog,
	                    x_OutAssignNewDest => #x_TRO518NewDestination,
	                    udt_Scan_Result := #udt_TRO518.stScanResult);
	    
	    
	    #lfb_TRO522Scan(t_InNoProgressTime := #t_InTRO522Watchdog,
	                    x_OutAssignNewDest => #x_TRO522NewDestination,
	                    udt_Scan_Result := #udt_TRO522.stScanResult);
	    
	    
	    // Bei Zieländerung stop und reset an TRO -> Ready vom TRO fällt weg -> reset wird zurückgesetzt -> Ready wieder da -> Stop zurücksetzen
	    
	    
	    
	    
	    #lfb_TRO505Handshake(xInNewDestination := #x_TRO505NewDestination,
	                         nInDestination := #cDestinationJam5002_9,
	                         udt_InOutTRO := #udt_TRO505,
	                         st_InOutJamDestData := #stInOutJam5002_3.stData,
	                         arCarrier := #stInOutJam5002_2.arCarrier);
	    
	    #lfb_TRO510Handshake(xInNewDestination := #x_TRO510NewDestination,
	                         nInDestination := #cDestinationJam5002_9,
	                         udt_InOutTRO := #udt_TRO510,
	                         st_InOutJamDestData := #stInOutJam5002_4.stData,
	                         arCarrier := #stInOutJam5002_3.arCarrier);
	    
	    #lfb_TRO514Handshake(xInNewDestination := #x_TRO514NewDestination,
	                         nInDestination := #cDestinationJam5002_9,
	                         udt_InOutTRO := #udt_TRO514,
	                         st_InOutJamDestData := #stInOutJam5002_5.stData,
	                         arCarrier := #stInOutJam5002_4.arCarrier);
	    
	    
	    #lfb_TRO518Handshake(xInNewDestination := #x_TRO518NewDestination,
	                         nInDestination := #cDestinationJam5002_9,
	                         udt_InOutTRO := #udt_TRO518,
	                         st_InOutJamDestData := #stInOutJam5002_6.stData,
	                         arCarrier := #stInOutJam5002_5.arCarrier);
	    
	    
	    #lfb_TRO522Handshake(xInNewDestination := #x_TRO522NewDestination,
	                         nInDestination := #cDestinationJam5002_9,
	                         udt_InOutTRO := #udt_TRO522,
	                         st_InOutJamDestData := #stInOutJam5002_7.stData,
	                         arCarrier := #stInOutJam5002_6.arCarrier);
	    
	END_REGION
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
END_FUNCTION_BLOCK

