FUNCTION "FC_Jam" : Void
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_OUTPUT 
      stOutCarrierStatsStandard : "CarrierStatisticsStandard";
      stOutCarrierStatsAdv : "CarrierStatisticsStorage";
   END_VAR

   VAR_IN_OUT 
      arInOutJamCarrier : Array[*] of "stCarrier";
      stInOutData : "stJamData";
   END_VAR

   VAR_TEMP 
      nIdx : Int;
      nArrayMax : Int;
   END_VAR

   VAR CONSTANT 
      cForecast : USInt := 110;
      cRetrieveSeqSeat : USInt := 1;
      cRetrieveSeqRest : USInt := 2;
      cRetrieveExpress : USInt := 3;
      cDummy : USInt := 1;
   END_VAR


BEGIN
	//Prüft Einstellwert
	IF (#stInOutData.Config.nCapacitySet > #stInOutData.Config.nCapacityMax) OR (#stInOutData.Config.nCapacitySet <= 0) THEN
	    #stInOutData.Config.nCapacitySet := #stInOutData.Config.nCapacityMax;
	END_IF;
	
	//Init
	#stOutCarrierStatsStandard.nEmptyCarrier := 0;
	#stOutCarrierStatsStandard.nLoadedCarrier := 0;
	#stOutCarrierStatsStandard.nDummyCarrier := 0;
	
	#stOutCarrierStatsAdv.nDest211 := 0;
	#stOutCarrierStatsAdv.nDest212 := 0;
	#stOutCarrierStatsAdv.nDest213 := 0;
	#stOutCarrierStatsAdv.nDest214 := 0;
	#stOutCarrierStatsAdv.nDest221 := 0;
	#stOutCarrierStatsAdv.nDest222 := 0;
	#stOutCarrierStatsAdv.nDest223 := 0;
	#stOutCarrierStatsAdv.nDest224 := 0;
	#stOutCarrierStatsAdv.nDest231 := 0;
	#stOutCarrierStatsAdv.nDest232 := 0;
	#stOutCarrierStatsAdv.nDest233 := 0;
	#stOutCarrierStatsAdv.nDest234 := 0;
	
	//Ermittelt den Status/Füllstand des Staubereichs
	#nArrayMax := DINT_TO_INT(UPPER_BOUND(ARR := #arInOutJamCarrier, DIM := 1));
	
	IF NOT #arInOutJamCarrier[1].bStatus.%X0 THEN //Wenn 1. Teil leer, dann muss Schleife nicht ausgeführt werden
	    #stInOutData.Status.nCount := 0;
	    #stInOutData.Status.bByte.%X0 := TRUE;
	    
	    //Jam ist leer
	    #stOutCarrierStatsStandard.nEmptyCarrier := 0;
	    #stOutCarrierStatsStandard.nLoadedCarrier := 0;
	    #stOutCarrierStatsStandard.nDummyCarrier := 0;
	    
	    #stOutCarrierStatsAdv.nDest211 := 0;
	    #stOutCarrierStatsAdv.nDest212 := 0;
	    #stOutCarrierStatsAdv.nDest213 := 0;
	    #stOutCarrierStatsAdv.nDest214 := 0;
	    #stOutCarrierStatsAdv.nDest221 := 0;
	    #stOutCarrierStatsAdv.nDest222 := 0;
	    #stOutCarrierStatsAdv.nDest223 := 0;
	    #stOutCarrierStatsAdv.nDest224 := 0;
	    #stOutCarrierStatsAdv.nDest231 := 0;
	    #stOutCarrierStatsAdv.nDest232 := 0;
	    #stOutCarrierStatsAdv.nDest233 := 0;
	    #stOutCarrierStatsAdv.nDest234 := 0;
	    #stOutCarrierStatsAdv.nDestAMRLoaded := 0;
	    #stOutCarrierStatsAdv.nDestF2HLoaded := 0;
	    #stOutCarrierStatsAdv.nDestF2HEmpty := 0;
	    #stOutCarrierStatsAdv.nDestInboundEmpty := 0;
	    #stOutCarrierStatsAdv.nDest11x := 0;
	    #stOutCarrierStatsAdv.nDest12x := 0;
	    #stOutCarrierStatsAdv.nDest13x := 0;
	    
	    
	    //****Für Statistics muss dies auskommentiert werden
	    // ELSIF #arInOutJamCarrier[#nArrayMax].xOnline THEN //Wenn letztes Teil belegt, dann muss Schleife nicht ausgeführt werden
	    //     #stInOutData.Status.nCount := #nArrayMax;
	    //     #stInOutData.Status.xEmpty := FALSE;
	    //*****
	    
	ELSE
	    FOR #nIdx := 1 TO #nArrayMax DO
	        //Statistics - nur wenn etwas vorhanden, ansonsten nicht machen
	        IF #arInOutJamCarrier[#nIdx].nBarcode <> 0 THEN
	            //Statistics Standard
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND NOT #arInOutJamCarrier[#nIdx].bStatus.%X1 AND #arInOutJamCarrier[#nIdx].nBarcode <> #cDummy THEN
	                #stOutCarrierStatsStandard.nEmptyCarrier += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].bStatus.%X1 AND #arInOutJamCarrier[#nIdx].nBarcode <> #cDummy THEN
	                #stOutCarrierStatsStandard.nLoadedCarrier += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nBarcode = #cDummy THEN
	                #stOutCarrierStatsStandard.nDummyCarrier += 1;
	            END_IF;
	            
	            //  Statistics Advanced (Bereichsbezogen)  
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest21100" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest21119" THEN
	                #stOutCarrierStatsAdv.nDest211 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest21200" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest21217" THEN
	                #stOutCarrierStatsAdv.nDest212 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest21300" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest21319" THEN
	                #stOutCarrierStatsAdv.nDest213 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest21400" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest21413" THEN
	                #stOutCarrierStatsAdv.nDest214 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest22100" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest22119" THEN
	                #stOutCarrierStatsAdv.nDest221 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest22200" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest22217" THEN
	                #stOutCarrierStatsAdv.nDest222 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest22300" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest22319" THEN
	                #stOutCarrierStatsAdv.nDest223 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest22400" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest22413" THEN
	                #stOutCarrierStatsAdv.nDest224 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest23100" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest23119" THEN
	                #stOutCarrierStatsAdv.nDest231 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest23200" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest23217" THEN
	                #stOutCarrierStatsAdv.nDest232 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest23300" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest23319" THEN
	                #stOutCarrierStatsAdv.nDest233 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination >= "cDest23400" AND #arInOutJamCarrier[#nIdx].nDestination <= "cDest23415" THEN
	                #stOutCarrierStatsAdv.nDest234 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestAMRLoaded" THEN
	                #stOutCarrierStatsAdv.nDestAMRLoaded += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestF2HLoaded" THEN
	                #stOutCarrierStatsAdv.nDestF2HLoaded += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestF2HEmpty" THEN
	                #stOutCarrierStatsAdv.nDestF2HEmpty += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestInboundEmpty" THEN
	                #stOutCarrierStatsAdv.nDestInboundEmpty += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestLvl1B1" THEN
	                #stOutCarrierStatsAdv.nDest11x += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestLvl1B2" THEN
	                #stOutCarrierStatsAdv.nDest12x += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestLvl1B3" THEN
	                #stOutCarrierStatsAdv.nDest13x += 1;
	            END_IF;
	            
	        END_IF;
	        
	        
	        IF #arInOutJamCarrier[#nIdx].nBarcode = 0 THEN //Zählt nicht die Teile sondern merkt sich die Positionen wo ein Teil vorhanden ist (minus 1)
	            #stInOutData.Status.nCount := INT_TO_UINT(#nIdx - 1);
	            #stInOutData.Status.bByte.%X0 := FALSE;
	            EXIT;
	            //****Für Statistics würde dies benötigt werden
	        ELSIF #nIdx = #nArrayMax THEN
	            #stInOutData.Status.nCount := INT_TO_UINT(#nArrayMax);
	            #stInOutData.Status.bByte.%X0 := FALSE;
	            EXIT;
	            //******
	        END_IF;
	    END_FOR;
	END_IF;
	
END_FUNCTION

