FUNCTION "FC_SHR_HangerAddPin" : Void
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      stInCarrier : "stCarrierPin";
      nInPos : DInt;   // Aktuelle Position auf der Kette
      nInTarget : USInt;   // Ziel-Station f. Trolley bei My Theresa = 1
      nInConvStations : Int;   // neu hinzugefügt, statt Konstante
      nInStationsMax : Int;   // neu hinzugefügt, statt Konstante
   END_VAR

   VAR_IN_OUT 
      stInOutConveyorStations : "stConveyorStations";
   END_VAR

   VAR_TEMP 
      nCounter : Int;   // Arraynummer bzw. Haken / Trolley
   END_VAR


BEGIN
	// ****************************************
	// Setzt im Online-Register einen neuen Hanger (am Anfang)
	// ****************************************
	
	IF #stInOutConveyorStations.arTrolleysOnline[1].stPinValues.nPosAct = #stInOutConveyorStations.arStationsPos[#nInPos].nPosIncr THEN       // Wenn bereits ein Bügel eingetragen ist auf der Position
	    RETURN;
	END_IF;
	// Alles im Online-Register eins weiter schieben
	FOR #nCounter := (#nInConvStations - 1) TO 1 BY -1 DO       // Weiterkopieren um eine Position
	    #stInOutConveyorStations.arTrolleysOnline[#nCounter + 1] := #stInOutConveyorStations.arTrolleysOnline[#nCounter];
	END_FOR;
	// Startwerte setzen
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X0 := TRUE;
	IF #nInTarget >= 1 AND #nInTarget <= #nInStationsMax THEN
	    #stInOutConveyorStations.arTrolleysOnline[1].stPinValues.nTarget := #nInTarget;
	    #stInOutConveyorStations.arTrolleysOnline[1].stPinValues.nTargetPos := #stInOutConveyorStations.arStationsPos[#nInTarget].nPosIncr;
	END_IF;
	
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.nBarcode := #stInCarrier.stCarrier.nBarcode;
	
	#stInOutConveyorStations.arTrolleysOnline[1].stPinValues.nPosAct := #stInOutConveyorStations.arStationsPos[#nInPos].nPosIncr;
	#stInOutConveyorStations.stData.nTrolleysOnline += 1;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X1 := #stInCarrier.stCarrier.bStatus.%X1;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X2 := #stInCarrier.stCarrier.bStatus.%X2;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X3 := #stInCarrier.stCarrier.bStatus.%X3;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X4 := #stInCarrier.stCarrier.bStatus.%X4;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X5 := #stInCarrier.stCarrier.bStatus.%X5;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X6 := #stInCarrier.stCarrier.bStatus.%X6;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X7 := #stInCarrier.stCarrier.bStatus.%X7;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X8 := #stInCarrier.stCarrier.bStatus.%X8;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X9 := #stInCarrier.stCarrier.bStatus.%X9;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X10 := #stInCarrier.stCarrier.bStatus.%X10;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X11 := #stInCarrier.stCarrier.bStatus.%X11;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X12 := #stInCarrier.stCarrier.bStatus.%X12;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X13 := #stInCarrier.stCarrier.bStatus.%X13;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X14 := #stInCarrier.stCarrier.bStatus.%X14;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.bStatus.%X15 := #stInCarrier.stCarrier.bStatus.%X15;
	#stInOutConveyorStations.arTrolleysOnline[1].stCarrier.nDestination := #stInCarrier.stCarrier.nDestination; // EMBI 11.06.2025
	
	// Dummy-Barcode für Hanger vergeben. Negative Werte sind Dummys
	#stInOutConveyorStations.stData.nBarcodeNew -= 1;
	IF #stInOutConveyorStations.stData.nBarcodeNew < -2000000 THEN       // Wenn Zähler Kleiner als 2 Mio dann von vorne anfangen. 
	    #stInOutConveyorStations.stData.nBarcodeNew := -1;
	END_IF;
	
	IF #stInCarrier.stCarrier.nBarcode > 0 THEN
	    #stInOutConveyorStations.arTrolleysOnline[1].stCarrier.nBarcode := #stInCarrier.stCarrier.nBarcode;
	ELSE
	    #stInOutConveyorStations.arTrolleysOnline[1].stCarrier.nBarcode := #stInOutConveyorStations.stData.nBarcodeNew;
	END_IF;
END_FUNCTION

