FUNCTION_BLOCK "FB_LV1BlockStorageManagement"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      stIn_JamDataBlock : Struct
         stIn_JamData01 : Struct
            udt_JamData01_1 : "stJamData";
            udt_JamData01_2 : "stJamData";
         END_STRUCT;
         stIn_JamData02 : Struct
            udt_JamData02_1 : "stJamData";
            udt_JamData02_2 : "stJamData";
            udt_JamData02_3 : "stJamData";
         END_STRUCT;
         stIn_JamData03 : Struct
            udt_JamData03_1 : "stJamData";
            udt_JamData03_2 : "stJamData";
         END_STRUCT;
         stIn_JamData04 : Struct
            udt_JamData04_1 : "stJamData";
            udt_JamData04_2 : "stJamData";
            udt_JamData04_3 : "stJamData";
         END_STRUCT;
         stIn_JamData05 : Struct
            udt_JamData05_1 : "stJamData";
            udt_JamData05_2 : "stJamData";
         END_STRUCT;
         stIn_JamData06 : Struct
            udt_JamData06_1 : "stJamData";
            udt_JamData06_2 : "stJamData";
         END_STRUCT;
         stIn_JamData07 : Struct
            udt_JamData07_1 : "stJamData";
            udt_JamData07_2 : "stJamData";
         END_STRUCT;
         stIn_JamData08 : Struct
            udt_JamData08_1 : "stJamData";
            udt_JamData08_2 : "stJamData";
         END_STRUCT;
         stIn_JamData09 : Struct
            udt_JamData09_1 : "stJamData";
            udt_JamData09_2 : "stJamData";
         END_STRUCT;
      END_STRUCT;
      nIn_StorageMode : USInt;   // 0 = uneven; 1= even
      xInQuit : Bool;
      wsIn_ObjectName : WString[50];
      nInBlockDestination1 : Int;
      nInBlockDestination2 : Int;
      nInBlockDestination3 : Int;
      nInBlockDestination4 : Int;
   END_VAR

   VAR_OUTPUT 
      xOut_StorageRelease : Bool;
   END_VAR

   VAR_IN_OUT 
      xError : Bool;
      nErrorID : USInt;
      xInOut_CapacityConfigChanged : Bool;
      arrudt_Storage1n1 : Array[1.."cNoLines"] of "stStorageLine";
      udt_Storage1n1BlockData : "UDT_StorageBlockData";
      arrudt_Storage1n2 : Array[1.."cNoLines"] of "stStorageLine";
      udt_Storage1n2BlockData : "UDT_StorageBlockData";
      arrudt_Storage1n3 : Array[1.."cNoLines"] of "stStorageLine";
      udt_Storage1n3BlockData : "UDT_StorageBlockData";
      arrudt_Storage1n4 : Array[1.."cNoLines"] of "stStorageLine";
      udt_Storage1n4BlockData : "UDT_StorageBlockData";
      udtInOut_CarrierAtScanner : "stCarrier";
   END_VAR

   VAR 
      st_AreaDisabledLevel1 { S7_SetPoint := 'False'} : "udt_AreaDisabledLv1";
      nTempCalculateStorageCapacity : USInt;
      str_CapacityStorageBlock { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : Struct
         nCapacityStorage001 { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : UInt;
         nCapacityStorage002 { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : UInt;
         nCapacityStorage003 { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : UInt;
         nCapacityStorage004 { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : UInt;
      END_STRUCT;
      n_Block11IDX : USInt;
      n_CarrierTyp : USInt;
      stTROCarrier : Struct
         st_Data { S7_SetPoint := 'False'} : "stCarrier";
         str_Type { S7_SetPoint := 'True'} : String;
      END_STRUCT;
      stTROCarrierLast : Struct
         st_Data { S7_SetPoint := 'False'} : "stCarrier";
         str_Type { S7_SetPoint := 'True'} : String;
      END_STRUCT;
      stTROCarrierClear : Struct
         st_Data { S7_SetPoint := 'False'} : "stCarrier";
         str_Type { S7_SetPoint := 'True'} : String;
      END_STRUCT;
      xDestinationGranted : Bool;
      PrgAlarm_Failmessage {InstructionName := 'Program_Alarm'; LibVersion := '1.0'; S7_SetPoint := 'False'} : Program_Alarm;
      wsErrorMessage1 : WString[50];
      wsErrorMessage2 : WString[50];
      wsErrorMessage3 : WString[50];
   END_VAR

   VAR_TEMP 
      nTempMinBefuellungBlock : USInt;
      nTempLinieIDX : USInt;
      nTempRetVal : Int;
      tTempLocalTime {InstructionName := 'DTL'; LibVersion := '1.0'} : DTL;
      stTemp_ErrorString1 : WString[50];
      n_TempSell : Int;
   END_VAR

   VAR CONSTANT 
      cAreaEmpty : Byte := 16#1;
      cAreaOK : Byte;
      "~~~~~~~~~~~CarrierType~~~~~~~~~~~~~" : Byte;
      c000_undefinert : USInt;
      c001_CarrierKurz : USInt := 1;
      c002_CarrierLang : USInt := 2;
      c003_CarrierLeer : USInt := 3;
      "~~~~~~~~~~~StorageMode~~~~~~~~~~~~~" : USInt;
      c000_uneven : USInt;
      c001_even : USInt := 1;
      "~~~~~~~~~~~ErrorID~~~~~~~~~~~~~" : USInt;
      c000_KeinFehler : USInt;
      c010_CarrierDatenFehlerhaft : USInt := 10;
      c020_KeinZielVerfuegbar : USInt := 20;
   END_VAR


BEGIN
	
	REGION Info
	(* Company: Schönenberger Systeme GmbH / (C) Copyright (2022). All rights reserved.
	-----------------------------------------------------------------------------------------
	Title: FB_LV1BlockStorageManagement
	Comment/Function: manages storage in to the individual storage Blocks on Level 1
	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-05-08 | -                  |                  | Header added on 08.05.2025
	                                                                   
	*)
	END_REGION
	
	REGION Local Time
	    
	    
	    // read local time for timestamp ------------------------------------------
	    #nTempRetVal := RD_LOC_T(#tTempLocalTime);
	    
	    
	
	    
	END_REGION
	
	REGION calculate max storage size of Block x
	    // Lagergröße der Blöcke berechnen
	    IF "FirstScan" OR #xInOut_CapacityConfigChanged
	    THEN
	        FOR #nTempCalculateStorageCapacity := 1 TO 22 DO
	            IF #nTempCalculateStorageCapacity >= 1 AND #nTempCalculateStorageCapacity <= 21
	            THEN
	                IF #nTempCalculateStorageCapacity = 1
	                THEN
	                    #str_CapacityStorageBlock.nCapacityStorage001 := 0;
	                    #str_CapacityStorageBlock.nCapacityStorage002 := 0;
	                    #str_CapacityStorageBlock.nCapacityStorage003 := 0;
	                    #str_CapacityStorageBlock.nCapacityStorage004 := 0;
	                    
	                    
	                END_IF;
	                
	               
	                
	                IF NOT #arrudt_Storage1n1[#nTempCalculateStorageCapacity].stData.Status.bByte.%X2
	                THEN
	                    #str_CapacityStorageBlock.nCapacityStorage001 := #str_CapacityStorageBlock.nCapacityStorage001 + #arrudt_Storage1n1[#nTempCalculateStorageCapacity].stData.Config.nCapacitySet;
	                END_IF;
	                IF NOT #arrudt_Storage1n2[#nTempCalculateStorageCapacity].stData.Status.bByte.%X2
	                THEN
	                    #str_CapacityStorageBlock.nCapacityStorage002 := #str_CapacityStorageBlock.nCapacityStorage002 + #arrudt_Storage1n2[#nTempCalculateStorageCapacity].stData.Config.nCapacitySet;
	                END_IF;
	                IF NOT #arrudt_Storage1n3[#nTempCalculateStorageCapacity].stData.Status.bByte.%X2
	                THEN
	                    #str_CapacityStorageBlock.nCapacityStorage003 := #str_CapacityStorageBlock.nCapacityStorage003 + #arrudt_Storage1n3[#nTempCalculateStorageCapacity].stData.Config.nCapacitySet;
	                END_IF;
	                IF NOT #arrudt_Storage1n4[#nTempCalculateStorageCapacity].stData.Status.bByte.%X2
	                THEN
	                    #str_CapacityStorageBlock.nCapacityStorage004 := #str_CapacityStorageBlock.nCapacityStorage004 + #arrudt_Storage1n4[#nTempCalculateStorageCapacity].stData.Config.nCapacitySet;
	                END_IF;
	                
	                
	                
	            END_IF;
	        END_FOR;
	        
	        #xInOut_CapacityConfigChanged := False;
	        
	    END_IF;
	END_REGION
	
	
	
	REGION Error/ Jam Group Areas
	    
	    REGION Block 1
	        
	        #st_AreaDisabledLevel1.str_Block.xSection01disabled := #stIn_JamDataBlock.stIn_JamData01.udt_JamData01_1.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData01.udt_JamData01_1.Status.bByte <> #cAreaEmpty
	        AND #stIn_JamDataBlock.stIn_JamData01.udt_JamData01_2.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData01.udt_JamData01_2.Status.bByte <> #cAreaEmpty;
	        
	        #st_AreaDisabledLevel1.str_Block.xSection02disabled := #stIn_JamDataBlock.stIn_JamData02.udt_JamData02_1.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData02.udt_JamData02_1.Status.bByte <> #cAreaEmpty
	        AND #stIn_JamDataBlock.stIn_JamData02.udt_JamData02_2.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData02.udt_JamData02_2.Status.bByte <> #cAreaEmpty
	        AND #stIn_JamDataBlock.stIn_JamData02.udt_JamData02_3.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData02.udt_JamData02_3.Status.bByte <> #cAreaEmpty;
	
	        
	        #st_AreaDisabledLevel1.str_Block.xSection03disabled := #stIn_JamDataBlock.stIn_JamData03.udt_JamData03_1.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData03.udt_JamData03_1.Status.bByte <> #cAreaEmpty
	        AND #stIn_JamDataBlock.stIn_JamData03.udt_JamData03_2.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData03.udt_JamData03_2.Status.bByte <> #cAreaEmpty;
	        
	        #st_AreaDisabledLevel1.str_Block.xSection04disabled := #stIn_JamDataBlock.stIn_JamData04.udt_JamData04_1.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData04.udt_JamData04_1.Status.bByte <> #cAreaEmpty
	        AND #stIn_JamDataBlock.stIn_JamData04.udt_JamData04_2.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData04.udt_JamData04_2.Status.bByte <> #cAreaEmpty
	        AND #stIn_JamDataBlock.stIn_JamData04.udt_JamData04_3.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData04.udt_JamData04_3.Status.bByte <> #cAreaEmpty;
	
	        
	        #st_AreaDisabledLevel1.str_Block.xSection05disabled := #stIn_JamDataBlock.stIn_JamData05.udt_JamData05_1.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData05.udt_JamData05_1.Status.bByte <> #cAreaEmpty
	        AND #stIn_JamDataBlock.stIn_JamData05.udt_JamData05_2.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData05.udt_JamData05_2.Status.bByte <> #cAreaEmpty;
	
	        
	        
	        #st_AreaDisabledLevel1.str_Block.xSection06disabled := #stIn_JamDataBlock.stIn_JamData06.udt_JamData06_1.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData06.udt_JamData06_1.Status.bByte <> #cAreaEmpty
	        AND #stIn_JamDataBlock.stIn_JamData06.udt_JamData06_2.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData06.udt_JamData06_2.Status.bByte <> #cAreaEmpty;
	
	        
	        #st_AreaDisabledLevel1.str_Block.xSection07disabled := #stIn_JamDataBlock.stIn_JamData07.udt_JamData07_1.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData07.udt_JamData07_1.Status.bByte <> #cAreaEmpty
	        AND #stIn_JamDataBlock.stIn_JamData07.udt_JamData07_2.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData07.udt_JamData07_2.Status.bByte <> #cAreaEmpty;
	
	        
	        #st_AreaDisabledLevel1.str_Block.xSection08disabled := #stIn_JamDataBlock.stIn_JamData08.udt_JamData08_1.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData08.udt_JamData08_1.Status.bByte <> #cAreaEmpty
	        AND #stIn_JamDataBlock.stIn_JamData08.udt_JamData08_2.Status.bByte <> #cAreaOK
	        AND #stIn_JamDataBlock.stIn_JamData08.udt_JamData08_2.Status.bByte <> #cAreaEmpty;
	
	        
	        
	        #st_AreaDisabledLevel1.str_Block.xBlockdisabled := #st_AreaDisabledLevel1.str_Block.xSection01disabled
	        OR #st_AreaDisabledLevel1.str_Block.xSection02disabled;
	        
	        
	        #xOut_StorageRelease := NOT #st_AreaDisabledLevel1.str_Block.xBlockdisabled;
	        
	    END_REGION
	END_REGION
	
	REGION Carrier prüfen
	    IF #udtInOut_CarrierAtScanner.nBarcode > 10000
	  //      AND #udtInOut_CarrierAtScanner.nDestination = 0
	 //       AND #udtInOut_CarrierAtScanner.bStatus.%X3
	        AND NOT #udtInOut_CarrierAtScanner.bStatus.%X1 //Wenn Loaded nicht gesetzt nehmen wir einfach an das der Carrier leer ist...
	    THEN
	        #n_CarrierTyp := #c003_CarrierLeer;  //leer
	    ELSIF #udtInOut_CarrierAtScanner.nBarcode > 10000
	      //  AND #udtInOut_CarrierAtScanner.nDestination = 0
	        AND #udtInOut_CarrierAtScanner.bStatus.%X3
	        AND #udtInOut_CarrierAtScanner.bStatus.%X1
	    THEN
	        #n_CarrierTyp := #c001_CarrierKurz; ///kurz und beladen
	    ELSE
	    //     #udtInOut_CarrierAtScanner.nBarcode > 10000
	    //     AND #udtInOut_CarrierAtScanner.nDestination = 0
	    //     AND NOT #udtInOut_CarrierAtScanner.bStatus.%X3
	    //     AND NOT #udtInOut_CarrierAtScanner.bStatus.%X1
	    // THEN
	        #n_CarrierTyp := #c000_undefinert; // kein Carrier oder undefiniert
	    END_IF;
	END_REGION
	
	REGION Find Destination
	    IF NOT #xDestinationGranted
	    THEN
	        CASE #n_CarrierTyp OF
	            #c000_undefinert:
	                #xError := True;
	                #nErrorID := #c010_CarrierDatenFehlerhaft;
	            #c001_CarrierKurz:
	                
	                CASE #nIn_StorageMode OF
	                        
	                    #c000_uneven:
	                        REGION Uneven
	                            //Blöcken werden ungleich gefüllt
	                            FOR #n_Block11IDX := 1 TO 21 DO
	                                IF #n_Block11IDX >= 1 AND #n_Block11IDX <= 21
	                                THEN
	                                    
	                                    
	                                    IF NOT #st_AreaDisabledLevel1.str_Block.xSection01disabled AND #arrudt_Storage1n1[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n1[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n1[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n1[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #udt_Storage1n1BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage001
	                                        AND NOT #arrudt_Storage1n1[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := #nInBlockDestination1;//11100;
	                                     //   #udt_Storage1n1BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF NOT #st_AreaDisabledLevel1.str_Block.xSection02disabled AND #arrudt_Storage1n2[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n2[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n2[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n2[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #udt_Storage1n2BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage002
	                                        AND NOT #arrudt_Storage1n2[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := #nInBlockDestination1;//11200;
	                                    //    #udt_Storage1n2BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF NOT #st_AreaDisabledLevel1.str_Block.xSection03disabled AND #arrudt_Storage1n3[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n3[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n3[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n3[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #udt_Storage1n3BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage003
	                                        AND NOT #arrudt_Storage1n3[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := #nInBlockDestination1;//11300;
	                                  //      #udt_Storage1n3BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF NOT #st_AreaDisabledLevel1.str_Block.xSection04disabled AND #arrudt_Storage1n4[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n4[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n4[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n4[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #udt_Storage1n4BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage004
	                                        AND NOT #arrudt_Storage1n4[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := #nInBlockDestination1;//11400;
	                                  //      #udt_Storage1n4BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                        
	                                    END_IF;
	                                    
	                                END_IF;
	                                IF #n_Block11IDX = 21 AND #stTROCarrier.st_Data.nDestination = 0
	                                THEN
	                                    #xError := True;
	                                    #nErrorID := #c020_KeinZielVerfuegbar;
	                                END_IF;
	                            END_FOR;
	                        END_REGION
	                    #c001_even:
	                        REGION Even
	                            //Minimale Befüllung in der Linie ermitteln 
	                            #nTempMinBefuellungBlock := #arrudt_Storage1n1[1].stData.Status.nCount;
	                            FOR #nTempLinieIDX := 2 TO 12 DO
	                                IF #arrudt_Storage1n1[#nTempLinieIDX].stData.Status.nCount < #nTempMinBefuellungBlock THEN
	                                    #nTempMinBefuellungBlock := #arrudt_Storage1n1[#nTempLinieIDX].stData.Status.nCount;
	                                END_IF;
	                                IF #arrudt_Storage1n2[#nTempLinieIDX].stData.Status.nCount < #nTempMinBefuellungBlock THEN
	                                    #nTempMinBefuellungBlock := #arrudt_Storage1n2[#nTempLinieIDX].stData.Status.nCount;
	                                END_IF;
	                                IF #arrudt_Storage1n3[#nTempLinieIDX].stData.Status.nCount < #nTempMinBefuellungBlock THEN
	                                    #nTempMinBefuellungBlock := #arrudt_Storage1n3[#nTempLinieIDX].stData.Status.nCount;
	                                END_IF;
	                                IF #arrudt_Storage1n4[#nTempLinieIDX].stData.Status.nCount < #nTempMinBefuellungBlock THEN
	                                    #nTempMinBefuellungBlock := #arrudt_Storage1n4[#nTempLinieIDX].stData.Status.nCount;
	                                END_IF;
	                            END_FOR;
	                            
	                            
	                            
	                            
	                            
	                            FOR #n_Block11IDX := 1 TO 21 DO
	                                IF #n_Block11IDX >= 1 AND #n_Block11IDX <= 21
	                                THEN
	                                    
	                                    IF NOT #st_AreaDisabledLevel1.str_Block.xSection01disabled AND #arrudt_Storage1n1[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n1[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n1[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n1[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #arrudt_Storage1n1[#n_Block11IDX].stData.Status.nCount = #nTempMinBefuellungBlock
	                                        AND #udt_Storage1n1BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage001
	                                        AND NOT #arrudt_Storage1n1[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := 11100;
	                                  //      #udt_Storage1n1BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF NOT #st_AreaDisabledLevel1.str_Block.xSection02disabled AND #arrudt_Storage1n2[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n2[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n2[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n2[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #arrudt_Storage1n2[#n_Block11IDX].stData.Status.nCount = #nTempMinBefuellungBlock
	                                        AND #udt_Storage1n2BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage002
	                                        AND NOT #arrudt_Storage1n2[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := 11200;
	                                  //      #udt_Storage1n2BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF NOT #st_AreaDisabledLevel1.str_Block.xSection03disabled AND #arrudt_Storage1n3[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n3[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n3[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n3[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #arrudt_Storage1n3[#n_Block11IDX].stData.Status.nCount = #nTempMinBefuellungBlock
	                                        AND #udt_Storage1n3BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage003
	                                        AND NOT #arrudt_Storage1n3[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := 11300;
	                                  //      #udt_Storage1n3BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF NOT #st_AreaDisabledLevel1.str_Block.xSection04disabled AND #arrudt_Storage1n4[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n4[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n4[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n4[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #arrudt_Storage1n4[#n_Block11IDX].stData.Status.nCount = #nTempMinBefuellungBlock
	                                        AND #udt_Storage1n4BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage004
	                                        AND NOT #arrudt_Storage1n4[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := 11400;
	                                 //       #udt_Storage1n4BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    END_IF;
	                                END_IF;
	                                IF #n_Block11IDX = 21 AND #stTROCarrier.st_Data.nDestination = 0
	                                THEN
	                                    #xError := True;
	                                    #nErrorID := #c020_KeinZielVerfuegbar;
	                                END_IF;
	                            END_FOR;
	                            
	                        END_REGION
	                END_CASE;
	                
	            #c003_CarrierLeer:
	                
	                CASE #nIn_StorageMode OF
	                        
	                    #c000_uneven:
	                        REGION Uneven
	                            //Blöcken werden ungleich gefüllt
	                            FOR #n_Block11IDX := 1 TO 21 DO
	                                IF #n_Block11IDX >= 1 AND #n_Block11IDX <= 21
	                                THEN
	                                    
	                                    
	                                    IF NOT #st_AreaDisabledLevel1.str_Block.xSection01disabled AND #arrudt_Storage1n1[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n1[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n1[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n1[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #udt_Storage1n1BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage001
	                                        AND #arrudt_Storage1n1[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := #nInBlockDestination1;
	                                 //       #udt_Storage1n1BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF NOT #st_AreaDisabledLevel1.str_Block.xSection02disabled AND #arrudt_Storage1n2[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n2[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n2[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n2[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #udt_Storage1n2BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage002
	                                        AND #arrudt_Storage1n2[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := #nInBlockDestination2;
	                                  //      #udt_Storage1n2BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF (* NOT #st_AreaDisabledLevel1.str_Block.xSection03disabled AND #arrudt_Storage1n3[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n3[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n3[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n3[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #udt_Storage1n3BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage003 *)
	                                        //AND
	                                        #arrudt_Storage1n3[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := #nInBlockDestination3;
	                                   //     #udt_Storage1n3BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF NOT #st_AreaDisabledLevel1.str_Block.xSection04disabled AND #arrudt_Storage1n4[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n4[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n4[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n4[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #udt_Storage1n4BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage004
	                                        AND #arrudt_Storage1n4[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := #nInBlockDestination4;
	                                   //     #udt_Storage1n4BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    END_IF;
	                                    
	                                END_IF;
	                                IF #n_Block11IDX = 21 AND #stTROCarrier.st_Data.nDestination = 0
	                                THEN
	                                    #xError := True;
	                                    #nErrorID := #c020_KeinZielVerfuegbar;
	                                END_IF;
	                            END_FOR;
	                        END_REGION
	                    #c001_even:
	                        REGION Even
	                            //Minimale Befüllung in der Linie ermitteln 
	                            #nTempMinBefuellungBlock := #arrudt_Storage1n1[1].stData.Status.nCount;
	                            FOR #nTempLinieIDX := 2 TO 12 DO
	                                IF #arrudt_Storage1n1[#nTempLinieIDX].stData.Status.nCount < #nTempMinBefuellungBlock THEN
	                                    #nTempMinBefuellungBlock := #arrudt_Storage1n1[#nTempLinieIDX].stData.Status.nCount;
	                                END_IF;
	                                IF #arrudt_Storage1n2[#nTempLinieIDX].stData.Status.nCount < #nTempMinBefuellungBlock THEN
	                                    #nTempMinBefuellungBlock := #arrudt_Storage1n2[#nTempLinieIDX].stData.Status.nCount;
	                                END_IF;
	                                IF #arrudt_Storage1n3[#nTempLinieIDX].stData.Status.nCount < #nTempMinBefuellungBlock THEN
	                                    #nTempMinBefuellungBlock := #arrudt_Storage1n3[#nTempLinieIDX].stData.Status.nCount;
	                                END_IF;
	                                IF #arrudt_Storage1n4[#nTempLinieIDX].stData.Status.nCount < #nTempMinBefuellungBlock THEN
	                                    #nTempMinBefuellungBlock := #arrudt_Storage1n4[#nTempLinieIDX].stData.Status.nCount;
	                                END_IF;
	                            END_FOR;
	                            
	                            
	                            
	                            
	                            
	                            FOR #n_Block11IDX := 1 TO 21 DO
	                                IF #n_Block11IDX >= 1 AND #n_Block11IDX <= 21
	                                THEN
	                                    
	                                    IF NOT #st_AreaDisabledLevel1.str_Block.xSection01disabled AND #arrudt_Storage1n1[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n1[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n1[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n1[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #arrudt_Storage1n1[#n_Block11IDX].stData.Status.nCount = #nTempMinBefuellungBlock
	                                        AND #udt_Storage1n1BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage001
	                                        AND #arrudt_Storage1n1[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := 11100;
	                              //          #udt_Storage1n1BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF NOT #st_AreaDisabledLevel1.str_Block.xSection02disabled AND #arrudt_Storage1n2[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n2[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n2[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n2[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #arrudt_Storage1n2[#n_Block11IDX].stData.Status.nCount = #nTempMinBefuellungBlock
	                                        AND #udt_Storage1n2BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage002
	                                        AND #arrudt_Storage1n2[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := 11200;
	                                //        #udt_Storage1n2BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF NOT #st_AreaDisabledLevel1.str_Block.xSection03disabled AND #arrudt_Storage1n3[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n3[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n3[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n3[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #arrudt_Storage1n3[#n_Block11IDX].stData.Status.nCount = #nTempMinBefuellungBlock
	                                        AND #udt_Storage1n3BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage003
	                                        AND #arrudt_Storage1n3[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := 11300;
	                               //         #udt_Storage1n3BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                    ELSIF NOT #st_AreaDisabledLevel1.str_Block.xSection04disabled AND #arrudt_Storage1n4[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n4[#n_Block11IDX].stData.Config.nCapacityMax
	                                        AND #arrudt_Storage1n4[#n_Block11IDX].stData.Status.nCount < #arrudt_Storage1n4[#n_Block11IDX].stData.Config.nCapacitySet
	                                        AND #arrudt_Storage1n4[#n_Block11IDX].stData.Status.nCount = #nTempMinBefuellungBlock
	                                        AND #udt_Storage1n4BlockData.nCountTotal < #str_CapacityStorageBlock.nCapacityStorage004
	                                        AND #arrudt_Storage1n4[#n_Block11IDX].stData.Config.bByte.%X3
	                                        AND NOT #xDestinationGranted
	                                    THEN
	                                        #stTROCarrier.st_Data.nDestination := 11400;
	                               //         #udt_Storage1n4BlockData.nCountReserved += 1;
	                                        #xDestinationGranted := True;
	                                        EXIT;
	                                        
	                                    END_IF;
	                                    
	                                    IF #n_Block11IDX = 21 AND #stTROCarrier.st_Data.nDestination = 0
	                                    THEN
	                                        #xError := True;
	                                        #nErrorID := #c020_KeinZielVerfuegbar;
	                                    END_IF;
	                                END_IF;
	                            END_FOR;
	                            
	                        END_REGION
	                END_CASE;
	                
	        END_CASE;
	    END_IF;
	END_REGION
	
	REGION Error 
	    
	    //Quit setzt Error Word zurück
	    IF #xInQuit
	    THEN
	        #xError := False;
	    END_IF;
	    
	    IF #xError AND #nErrorID = #c010_CarrierDatenFehlerhaft
	    THEN
	        #wsErrorMessage1 := WString#'Carrier';
	        #wsErrorMessage2 := DINT_TO_WSTRING(#udtInOut_CarrierAtScanner.nBarcode);
	       // #wsErrorMessage3 := WSTRING#'Daten nicht eindeutig';
	    ELSIF #xError AND #nErrorID = #c020_KeinZielVerfuegbar
	    THEN
	        #wsErrorMessage1 := WString#'Carrier';
	        #wsErrorMessage2 := DINT_TO_WSTRING(#udtInOut_CarrierAtScanner.nBarcode);
	        //#wsErrorMessage3 := WSTRING#'kein Ziel verfuegbar';
	    ELSIF #xError AND #nErrorID = #c000_KeinFehler
	    THEN
	        #wsErrorMessage1 := WString#'';
	        #wsErrorMessage2 := WString#'';
	        #wsErrorMessage3 := WSTRING#'';
	        
	    END_IF;
	    
	    #PrgAlarm_Failmessage(SIG := FALSE, //xError,
	                          TIMESTAMP := #tTempLocalTime,
	                          SD_1 := #wsIn_ObjectName,
	                          SD_2 := #wsErrorMessage1,
	                          SD_3 := #wsErrorMessage2,
	                          SD_4 := #nErrorID);
	    
	END_REGION
	
	REGION Output Mapping 
	    IF #xDestinationGranted
	    THEN
	        #udtInOut_CarrierAtScanner.nDestination := #stTROCarrier.st_Data.nDestination;
	    END_IF;
	END_REGION
	
	REGION Reset
	    IF #udtInOut_CarrierAtScanner.nBarcode = 0 OR (#stTROCarrierLast.st_Data.nBarcode <> #udtInOut_CarrierAtScanner.nBarcode)
	    THEN
	        #xDestinationGranted := False;
	        #stTROCarrier.st_Data.nDestination := 0;
	        #udtInOut_CarrierAtScanner.nDestination := 0;
	    END_IF;
	END_REGION
	
	#stTROCarrierLast.st_Data.nBarcode := #udtInOut_CarrierAtScanner.nBarcode;
END_FUNCTION_BLOCK

