<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<program name="MainLoop" version="1.0.0" creationDate="1580483182" lastModifiedDate="1640775377" excludeFromBuild="FALSE" excludeFromBuildIfNotDef="">
	<vars>
		<localVars>
			<var name="iAlarmsWordID" type="UINT"/>
			<var name="ReadData" type="sysMbMRTU_FC03"/>
			<var name="iAlarmGroup" type="UINT"/>
			<var name="bitIndex" type="UINT"/>
			<var name="alarmWord" type="WORD"/>
			<var name="i" type="USINT"/>
			<var name="xInitCom" type="BOOL">
				<initValue>FALSE</initValue>
			</var>
			<var name="timerPollingVars" type="TON"/>
			<var name="xLocalIsAlarmTmp" type="BOOL"/>
			<var name="xLocalIsWiringFaultTmp" type="BOOL"/>
			<var name="timerPollingAlarm" type="TON"/>
			<var name="VarIndex" type="UINT"/>
			<var name="uiSimulationHMI" type="UINT"/>
			<var name="is_LCD_pressedOld" type="BOOL"/>
			<var name="uiActiveAlarmsTmp" type="UINT"/>
			<var name="uiActiveWarningTmp" type="UINT"/>
			<var name="usiRegulProbSel" type="USINT"/>
			<var name="OutDoorAlloced" type="UINT"/>
			<var name="uiContLoop" type="UINT"/>
			<var name="uiLoopDelay" type="UINT">
				<initValue>100</initValue>
			</var>
			<var name="uiContLoopAll" type="UINT"/>
		</localVars>
	</vars>
	<iecDeclaration active="FALSE"/>
	<sourceCode type="ST">
		<![CDATA[uiContLoop := uiContLoop+1;

IF (uiContLoop MOD uiLoopDelay) <> 0 THEN
	RETURN;
END_IF;

uiContLoopAll := uiContLoopAll+1;

// init vars
IF NOT xInitCom THEN
	xInitCom				:= TRUE;
	ReadData.addr			:= 1;	// Slave ID
	ReadData.timeout		:= 500;	// Time Out
	ReadData.waitBeforeSend	:= 10;	// Wait before send
	timerPollingAlarm.PT	:= 5000;
	timerPollingVars.PT		:= 250;
	xEnableVarsPolling		:= TRUE;
	xShowBack				:= TRUE;
	xShowUP					:= TRUE;
	xSelectKeyUP			:= TRUE;
	xShowDown				:= TRUE;
	xSelectKeyDown			:= TRUE;
END_IF;

(*************************************************)
(********* MOVED FROM ON TIMER SCRIPT - START ****)
(*************************************************)

	IF CircID = 1 THEN
		xShowCir1 			:= TRUE;
		xShowCir2 			:= FALSE;		
		IF xEnPIDCir1 THEN
			xShowLowSetCir1 := TRUE;
			xShowWaterSet   := FALSE;
			xShowPressCir1	:= NOT(xSuctionSetpointType); 	// 0=PRESS
			xShowTempCir1	:= xSuctionSetpointType;		// 1=TEMP
	//		xShowPressCir2	:= FALSE; 	
	//		xShowTempCir2	:= FALSE;
		ELSE
			xShowLowSetCir1 := xIsCompressorRack;
			xShowWaterSet   := TRUE;	// 0=TEMP Water set
			xShowPressCir1	:= FALSE;
			xShowTempCir1	:= FALSE;
	//		xShowPressCir2	:= FALSE; 
	//		xShowTempCir2	:= FALSE;
		END_IF;	
		xShowPressCir2	:= FALSE; 
		xShowTempCir2	:= FALSE;
		
	ELSIF CircID = 2 THEN
		xShowCir1 		:= FALSE;
		xShowCir2 		:= TRUE;
		IF xEnPIDCir1 THEN // cir 1 is alligned with cir2
			xShowLowSetCir1 := TRUE;
		ELSE
			xShowLowSetCir1 := xIsCompressorRack;
		END_IF;	
		
		
		IF xEnPIDCir2 THEN
			xShowWaterSet   := FALSE;
			xShowPressCir1	:= NOT(xSuctionSetpointType); 	// 0=PRESS
			xShowTempCir1	:= xSuctionSetpointType;		// 1=TEMP
			xShowPressCir2	:= FALSE; 	
			xShowTempCir2	:= FALSE;
		ELSE
			xShowWaterSet   := TRUE;	// 0=TEMP Water set
			xShowPressCir1	:= FALSE;
			xShowTempCir1	:= FALSE;
			xShowPressCir2	:= FALSE; 
			xShowTempCir2	:= FALSE;
		END_IF;				
		
	//	xShowWaterSet   := FALSE;
	//	xShowPressCir1	:= FALSE; 	
	//	xShowTempCir1	:= FALSE;			
	//	xShowPressCir2	:= NOT(xSuctionSetpointType); 	// 0=PRESS
	//	xShowTempCir2	:= xSuctionSetpointType;		// 1=TEMP
	ELSE
		CircID := 1;
	END_IF;
	
	
	w2b(word:=uiStatusC1);
	xCommandY1		:= w2b.b3;		// Bit3
	xCommandY2		:= w2b.b4;		// Bit4
	xCommandY3		:= w2b.b5;		// Bit5
	xCommandY4		:= w2b.b6;		// Bit6
	xVisY1C1		:= w2b.b12;	// Bit12
	xVisY2C1		:= w2b.b13;	// Bit13
	xVisY3C1		:= w2b.b14;	// Bit14
	xVisY4C1		:= w2b.b15;	// Bit15
	
	xMainPageScript := TRUE;
	(*
	IF VarIndex = 1 THEN
		xEnableAlarmPolling := FALSE; 
		retUint1 :=Video_GetParam(1,ADDR_DEFROST_STATUS_CIR1, 0, ?usiStatusDefrostCir1, tyUsint );
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 2 THEN
		retUint2 :=Video_GetParam(1,ADDR_DEFROST_STATUS_CIR2, 0, ?usiStatusDefrostCir2, tyUsint );	
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 3 THEN
		retUINT:= Video_GetParam(1, ADDR_WEB_ONOFF, 0, ?xOnOffRemote, tyBool );
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 4 THEN
		retUINT:= Video_GetParam(1, ADDR_PLC_ONOFF, 0, ?xRemoteStartStop, tyBool );
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 5 THEN
		retUINT:= Video_GetParam(1, ADDR_DI_ONOFF, 0, ?xLocalOnOff, tyBool );	
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 6 THEN
		retUINT := Video_GetParam(1, ADDR_REQCAP_CIR1, 0, ?rCapacityCir1, tyReal);
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 7 THEN
		retUINT := Video_GetParam(1, ADDR_REQCAP_CIR2, 0, ?rCapacityCir2, tyReal);
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 8 THEN
		retUINT := Video_GetParam(1, ADDR_RCAPMAX_CIR1, 0, ?rCapMaxCir1, tyReal);
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 9 THEN
		retUINT := Video_GetParam(1, ADDR_RCAPMAX_CIR2, 0, ?rCapMaxCir2, tyReal);
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 10 THEN	
		retUINT := Video_GetParam(1, ADDR_MODE_HEAT_COOL, 0, ?isModeHeat, tyBool);
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 11 THEN
		retUINT := Video_GetParam(1, ADDR_XREVERSEVALVE_C1, 0, ?isValveReverse1, tyBool);	
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 12 THEN
		retUINT := Video_GetParam(1, ADDR_ENOOCIR, 0, ?xEnOOCir, tyBool);
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 13 THEN
		retUINT := Video_GetParam(1,  ADDR_COMP_PB_CALC , 0, ?tempUINT1, tyUInt);
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 14 THEN
		IF(isModeHeat) THEN
			retUINT := Video_GetParam(1,  ADDR_COMP_PB_HEAT , 0, ?tempUINT,tyUInt);
		ELSIF NOT(isModeHeat)THEN
			retUINT := Video_GetParam(1,  ADDR_COMP_PB_COOL , 0, ?tempUINT,tyUInt);
		END_IF;
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 15 THEN
		retUINT := Video_GetParam(1,ADDR_XREVERSEVALVE_C2, 0, ?isValveReverse2, tyBool);
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 16 THEN
		retVarTest1 := Video_GetParam(1,  ADDR_UNIT_OP_MODE,   	0, ?usiOperatingMode, tyUInt );
		VarIndex := VarIndex +1;	
	ELSIF VarIndex = 17 THEN
		//Check if circuit 2 present	
		retVarTest2 := Video_GetParam(1, ADDR_NR_CIRC, 0, ?usiNbCircuits, tyUInt);
		xIsCircuit2 := (usiNbCircuits>1);
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 18 THEN	
		retVarTest3 := Video_GetParam(1,  17314,   				0, ?xSuctionSetpointType, tyBool );
		VarIndex := VarIndex +1;
	ELSIF VarIndex = 19 THEN
		retVarTest4 := Video_GetParam(1,  17305,   				0, ?xEnPIDCir1, tyBool );
		//retUint := Video_GetParam(1,  17305,   				0, ?xEnPIDCir2, tyBool );
		VarIndex := VarIndex +1;
		xEnableAlarmPolling 	:= TRUE; // enable alarm polling after the first data refresh
	ELSIF VarIndex = 20 THEN
		IF xEnableAlarmPolling = FALSE THEN
			VarIndex 				:= VarIndex +1;
			xEnableVarsPolling  	:= TRUE; // enable alarm polling after the first data refresh
		END_IF;
	ELSIF VarIndex = 21 THEN	
		IF xEnableVarsPolling = FALSE THEN
			VarIndex := 0;
		END_IF;
	ELSE
		VarIndex := 0;
	END_IF;
	*)
	
	IF usiStatusDefrostCir1>2 AND (usiStatusDefrostCir2 < 3) THEN
		iDefrostActive :=1;
	ELSIF(usiStatusDefrostCir1 < 3) AND (usiStatusDefrostCir2 > 2) THEN
		iDefrostActive := 2;
	ELSIF (usiStatusDefrostCir1 > 2) AND (usiStatusDefrostCir2 > 2)  THEN
		iDefrostActive := 0;
	END_IF;
	
	
	IF(usiStatusDefrostCir1<3 ) AND (usiStatusDefrostCir2 <3) THEN
		isDefrostIconVisible:=FALSE;
	ELSE
		isDefrostIconVisible:=TRUE;
	END_IF;
	
	
	(* Manage notify icon  *)
	IF (IsWiringFaultActive OR IsAlarmActive OR (IsLimiterActive > 0)) THEN
		isNotifyVisible := TRUE;
	ELSE
		isNotifyVisible := FALSE;
	END_IF;
	
	
	
	IF BlinkTimer <> 0 THEN
		BlinkTimer := BlinkTimer-1;
	ELSE
		BlinkTimer := 1;	
		CASE notifyAnimation OF 
			0: (* Null icon *)
				IF IsAlarmActive THEN
					notifyAnimation := 1;
				ELSIF IsWiringFaultActive THEN
					notifyAnimation := 3;
				ELSIF (IsLimiterActive > 0) THEN
					notifyAnimation := 4;
				ELSE
					notifyAnimation := 0;
				END_IF;
				
			1: (* Alarm icon *)
				IF IsWiringFaultActive THEN
					notifyAnimation := 3;
				ELSIF (IsLimiterActive > 0) THEN
					notifyAnimation := 4;
				ELSIF (IsAlarmActive AND (IsMuteActive = FALSE)) THEN
					notifyAnimation := 1;
				ELSE
					notifyAnimation := 0;
				END_IF;
				
			2: (* Reboot (not used anymore) icon *)
				(*IF IsWiringFaultActive THEN
					notifyAnimation := 3;
				ELSIF IsLimiterActive THEN
					notifyAnimation := 4;
				ELSE
					notifyAnimation := 0;
				END_IF;*)
				
			3: (* Wiring fault icon *)
				IF (IsLimiterActive > 0) THEN
					notifyAnimation := 4;
				ELSIF IsAlarmActive THEN
					notifyAnimation := 1;
				ELSE
					notifyAnimation := 0;
				END_IF;
				
			4: (* Limiter icon *)
				IF IsAlarmActive THEN
					notifyAnimation := 1;
				ELSIF IsWiringFaultActive THEN
					notifyAnimation := 3;
				ELSE
					notifyAnimation := 0;
				END_IF;
		END_CASE;
	END_IF;
	
	
	rTotalCapacity := rCapacityCir1 + rCapacityCir2;
	
	
	IF(xEnOOCir) THEN
	
		rMaxCapacity :=1000.00;
	
	ELSIF NOT (xEnOOCir) THEN
		rMaxCapacity := rCapMaxCir1 + rCapMaxCir2;
	END_IF;
	
	
	IF(isModeHeat) AND NOT(isValveReverse1)  THEN
		xBlink1:= TRUE;
	ELSIF NOT (isModeHeat) AND (isValveReverse1) THEN
		xBlink1:= TRUE;
	ELSE	
		xBlink1		:=FALSE;
		sysTimer_Old:=sysTimer;
	END_IF;
		
	
	IF (xIsCircuit2) THEN
		IF(isModeHeat) AND NOT (isValveReverse2)  THEN
			xBlink2:= TRUE;
		ELSIF NOT (isModeHeat) AND (isValveReverse2) THEN
			xBlink2:= TRUE;
		ELSE
	
			xBlink2:=FALSE;
		END_IF;
	
	ELSIF NOT(xIsCircuit2) THEN 
		//isHeatCoolVisible2:=FALSE;
		xBlink2:=FALSE;
	END_IF;
		
	
	iIconHeatCool:=TO_INT(isModeHeat);
	
	(* Blink transition Heat to cool *)
	
	IF(xBlink1) OR (xBlink2) THEN
		IF (sysTimer - sysTimer_Old)>PulseTime THEN		
			sysTimer_Old:=sysTimer;
			isHeatCoolVisible1:=NOT(isHeatCoolVisible1);
		END_IF;	
	
	ELSIF NOT (xBlink1)  AND NOT (xBlink2)THEN		
		isHeatCoolVisible1:=TRUE;	
	END_IF;
	
	(*On Off display message*)
	
	//IF (xRemoteStartStop) AND NOT (xOnOffRemote) OR NOT(xLocalOnOff)THEN
	//	IF (sysTimer-udRemoteOnTimer)>= OFFONMSGPERIOD THEN
//			tempUSINT:=Video_SendEvent(kWM_KEY,kKEY_VK_F4);	 
	//	END_IF;
	//END_IF;  
	
	(*Icon Eco-Comf Visible*)
	IF usiOperatingMode >1 THEN
		isComfEcoVisible:=TRUE;
	ELSE
		isComfEcoVisible:=FALSE;
	END_IF;
	
	
	IF(usiOperatingMode = 0 ) THEN
		iSetSystemStart:=0;
	ELSIF(usiOperatingMode = 1 ) THEN
		iSetSystemStart:=1;
	ELSIF(usiOperatingMode >1 ) THEN
		iSetSystemStart:=2;
	END_IF;
	
(***********************************************)
(********* END - MOVED FROM ON TIMER SCRIPT ****)
(***********************************************)






(**************************************************)
(********* MOVED FROM ON TIMER SCRIPT   START  ****)
(**************************************************)
	
	(* Dynamic visibilities related to the current access level *)
	IF uiSimulationHMI <> 0 THEN
		PermLevelCurr := TO_USINT(uiSimulationHMI);
	END_IF;
	PermLevActive[0] := TRUE;
	PermLevActive[1] := PermLevelCurr > 0;
	PermLevActive[2] := PermLevelCurr > 1;
	PermLevActive[3] := PermLevelCurr > 2;
	
	(* Automatic menu exit and permission level after timeout in page without pressing keys *)
	IF is_LCD_pressedOld <> sysLCDPressed.is_LCD_pressed THEN 
		udiCopySysTimer := sysTimer;
		sysBacklight 	:= 1;
	END_IF;
	is_LCD_pressedOld := sysLCDPressed.is_LCD_pressed;
	
	udiTimerMenu 		:= sysTimer - udiCopySysTimer;
	IF udiTimerMenu > (TO_UDINT(sysPar_BacklightTime) * USINT#1000) THEN
		IF (usiHmiPosition = HMI_IN_MENU) THEN
			retUINT 	:= Video_SendEvent(kWM_KEY,kKEY_VK_F9); (* Close page *)
		END_IF;
		PermLevelCurr 	:= 0;
		sysBacklight 	:= 0;
		udiCopySysTimer := sysTimer;
	END_IF;
	usiOldHmiPosition := usiHmiPosition;
	
	// Get Alarm Word
	timerPollingAlarm(IN:=TRUE);
	IF timerPollingAlarm.Q AND xEnableAlarmPolling THEN
		timerPollingAlarm(IN:=FALSE);
		
		IF iAlarmGroup = 0 THEN
			ReadData.base 		:= 10380;  // Address of the first alarm word wAlarmWord1 to wAlarmWord5
			ReadData.no	  		:= 5;	 // read 5 word means read 1-80 alarms 
			bitIndex			:= 0;
			uiActiveAlarmsTmp 	:= 0;
			AlarmActiveCode[0]	:= 0;
			IsAlarmActive	    := xLocalIsAlarmTmp; // STORE TEMP STATUS
			xLocalIsAlarmTmp	:= FALSE;			 // reset status
		ELSIF iAlarmGroup = 1 THEN
			ReadData.base 	:= 10385;  // Address of the first alarm word wAlarmWord6 to wAlarmWord10
			ReadData.no	  	:= 5;	 // read 5 word means read 81-160 alarms 
		
		ELSIF iAlarmGroup = 2 THEN
			ReadData.base 	:= 10390;  // Address of the first alarm word wAlarmWord11 to wAlarmWord15
			ReadData.no	  	:= 5;	 // read 5 word means read 161-240 alarms 
			
		ELSIF iAlarmGroup = 3 THEN
			ReadData.base 	:= 10400;  // Address of the first alarm word wAlarmWord10
			ReadData.no	  	:= 5;	 // read 5 word means read 301-380 alarms 
			bitIndex		:= 0;
			
			IsWiringFaultActive:= xLocalIsWiringFaultTmp;	// STORE TEMP STATUS TO NOTIFY ICONS
			xLocalIsWiringFaultTmp	:=FALSE;
		END_IF;
			
		ReadData();
		xTimeOut:=ReadData.error;// Schow icon Com Error
	
		IF iAlarmGroup = 0 OR iAlarmGroup = 1 OR iAlarmGroup = 2 THEN
			
			FOR i := 0 to 15 DO
				AlarmActive[bitIndex] :=ReadWordBit(ReadData.object1,i);
				IF AlarmActive[bitIndex] THEN 
					uiActiveAlarmsTmp										:= uiActiveAlarmsTmp+1; 
					AlarmActiveCode[uiActiveAlarmsTmp MOD MAX_ALARM_CODE] 	:= bitIndex;
				END_IF;
				bitIndex:=bitIndex+1;
			END_FOR;
			xLocalIsAlarmTmp 		:= xLocalIsAlarmTmp OR ReadData.object1>0 ;
			
			FOR i := 0 to 15 DO
				AlarmActive[bitIndex] :=ReadWordBit(ReadData.object2,i);	
				IF AlarmActive[bitIndex] THEN 
					uiActiveAlarmsTmp										:= uiActiveAlarmsTmp+1;  
					AlarmActiveCode[uiActiveAlarmsTmp MOD MAX_ALARM_CODE] 	:= bitIndex;
				END_IF;
				bitIndex:=bitIndex+1;
			END_FOR;
			xLocalIsAlarmTmp 		:= xLocalIsAlarmTmp OR ReadData.object2>0 ;
			
			FOR i := 0 to 15 DO
				AlarmActive[bitIndex] :=ReadWordBit(ReadData.object3,i);
				IF AlarmActive[bitIndex] THEN 
					uiActiveAlarmsTmp					:= uiActiveAlarmsTmp+1;   
					AlarmActiveCode[uiActiveAlarmsTmp MOD MAX_ALARM_CODE] 	:= bitIndex;
				END_IF;
				bitIndex:=bitIndex+1;
			END_FOR;
			xLocalIsAlarmTmp 		:= xLocalIsAlarmTmp OR ReadData.object3>0 ;
			
			FOR i := 0 to 15 DO
				AlarmActive[bitIndex] :=ReadWordBit(ReadData.object4,i);
				IF AlarmActive[bitIndex] THEN 
					uiActiveAlarmsTmp					:= uiActiveAlarmsTmp+1;   
					AlarmActiveCode[uiActiveAlarmsTmp MOD MAX_ALARM_CODE] 	:= bitIndex;
				END_IF;
				bitIndex:=bitIndex+1;
			END_FOR;
			xLocalIsAlarmTmp 		:= xLocalIsAlarmTmp OR ReadData.object4>0 ;
			
			FOR i := 0 to 15 DO
				AlarmActive[bitIndex] :=ReadWordBit(ReadData.object5,i);
				IF AlarmActive[bitIndex] THEN 
					uiActiveAlarmsTmp					:= uiActiveAlarmsTmp+1;   
					AlarmActiveCode[uiActiveAlarmsTmp MOD MAX_ALARM_CODE] 	:= bitIndex;
				END_IF;
				bitIndex:=bitIndex+1;
			END_FOR;
			xLocalIsAlarmTmp 		:= xLocalIsAlarmTmp OR ReadData.object5>0 ;
			
	
		ELSIF iAlarmGroup = 3 THEN
			uiActiveWarningTmp := 0;
			FOR i := 0 to 15 DO
				AlarmWairing[bitIndex] :=ReadWordBit(ReadData.object1,i);
				IF AlarmWairing[bitIndex] THEN 
					uiActiveAlarmsTmp					:= uiActiveAlarmsTmp+1;   
					AlarmActiveCode[uiActiveAlarmsTmp MOD MAX_ALARM_CODE] 	:= 299 + bitIndex;
				END_IF;
				bitIndex:=bitIndex+1;
			END_FOR;
			xLocalIsWiringFaultTmp	:=	xLocalIsWiringFaultTmp OR ReadData.object1>0;
			
			FOR i := 0 to 15 DO
				AlarmWairing[bitIndex] :=ReadWordBit(ReadData.object2,i);
				IF AlarmWairing[bitIndex] THEN 
					uiActiveAlarmsTmp					:= uiActiveAlarmsTmp+1;    
					AlarmActiveCode[uiActiveAlarmsTmp MOD MAX_ALARM_CODE] 	:= 299 + bitIndex;
				END_IF;
				bitIndex:=bitIndex+1;
			END_FOR;
			xLocalIsWiringFaultTmp	:=	xLocalIsWiringFaultTmp OR ReadData.object2>0;
			
			FOR i := 0 to 15 DO
				AlarmWairing[bitIndex] :=ReadWordBit(ReadData.object3,i);
				IF AlarmWairing[bitIndex] THEN 
					uiActiveAlarmsTmp					:= uiActiveAlarmsTmp+1;    
					AlarmActiveCode[uiActiveAlarmsTmp MOD MAX_ALARM_CODE] 	:= 299 + bitIndex;
				END_IF;
				bitIndex:=bitIndex+1;
			END_FOR;
			xLocalIsWiringFaultTmp	:=	xLocalIsWiringFaultTmp OR ReadData.object3>0;
			
			FOR i := 0 to 15 DO
				AlarmWairing[bitIndex] :=ReadWordBit(ReadData.object4,i);
				IF AlarmWairing[bitIndex] THEN 
					uiActiveAlarmsTmp					:= uiActiveAlarmsTmp+1;    
					AlarmActiveCode[uiActiveAlarmsTmp MOD MAX_ALARM_CODE] 	:= 299 + bitIndex;
				END_IF;
				bitIndex:=bitIndex+1;
			END_FOR;
			xLocalIsWiringFaultTmp	:=	xLocalIsWiringFaultTmp OR ReadData.object4>0;
			
			FOR i := 0 to 15 DO
				AlarmWairing[bitIndex] :=ReadWordBit(ReadData.object5,i);
				IF AlarmWairing[bitIndex] THEN 
					uiActiveAlarmsTmp					:= uiActiveAlarmsTmp+1;    
					AlarmActiveCode[uiActiveAlarmsTmp MOD MAX_ALARM_CODE] 	:= 299 + bitIndex;	
				END_IF;
				bitIndex:=bitIndex+1;
			END_FOR;
			xLocalIsWiringFaultTmp	:=	xLocalIsWiringFaultTmp OR ReadData.object5>0;
			uiActiveAlarms 			:= uiActiveAlarmsTmp;
		END_IF;
		
		iAlarmGroup := iAlarmGroup+1;											// INCREMENT GROUP
		IF iAlarmGroup > 3 THEN
			iAlarmGroup := 0;
			xEnableAlarmPolling 	:= FALSE;	
		END_IF;
	END_IF;
	
	
	
	
	(* Global script*)
	timerPollingVars(IN:=TRUE);
	IF timerPollingVars.Q AND xEnableVarsPolling THEN
		timerPollingVars(IN:=FALSE);
		
		(* refresh alarms list *)
		IF getIndex = 1 THEN
			//retUINT := Video_GetParam(1, ADDR_EXP1USED, 0, ?xExp1Used, tyBool);	
		//ELSIF getIndex = 2 THEN
			retUINT := Video_GetParam(1, ADDR_DEFROST_TYPE, 0, 					?usiDefrostType, tyUsint);
		ELSIF getIndex = 2 THEN
			retUINT := Video_GetParam(1, ADDR_UNIT_MODEL, 0, 				 	?usiUnitModel, tyUsint);
		//ELSIF getIndex = 4 THEN
			//retUINT := Video_GetParam(1, ADDR_ENABLE_WATER_AIR_LINK_SET, 0, 	?xWaterAirLinkSet, tyBool );
		ELSIF getIndex = 3 THEN
			retUINT := Video_GetParam(1, 8640, 0, ?Led1Status, tyUSint );
		ELSIF getIndex = 4 THEN
			retUINT := Video_GetParam(1, 8641, 0, ?Led2Status, tyUSint );
		ELSIF getIndex = 5 THEN
			retUINT := Video_GetParam(1, 8642, 0, ?Led3Status, tyUSint );
			xEnableAlarmPolling 	:= TRUE; // enable alarm polling after the first data refresh
		ELSIF getIndex = 6 THEN
			//IF xEnableAlarmPolling = FALSE THEN
				getIndex 			:= 0;
			//END_IF;	
		ELSE
			getIndex 			:= 0;
		END_IF;
		getIndex := getIndex + 1;
		
		
		// Set leds for Remote HMI
		sysLocalLeds[0] := Led1Status;
		sysLocalLeds[1] := Led2Status;
		sysLocalLeds[2] := Led3Status;
		
		IF not sysLCDPressed.is_LCD_pressed AND sysLCDPressed.pressed_event THEN
			sysLCDPressed.pressed_event := FALSE;
			sysBacklight:= 1;
		ELSE
			IF sysBacklight = 1 THEN 
				sysBacklight:= 2;
			END_IF;
		END_IF;
			
		xMainPageScript:= usiHmiPosition = HMI_IN_MAIN_PAGE;
		IF xMainPageScript THEN 
			IF 	  VarIndex = 1 THEN
				retUINT :=Video_GetParam(1,ADDR_DEFROST_STATUS_CIR1, 0, ?usiStatusDefrostCir1, tyUsint );
			ELSIF VarIndex = 2 THEN
				retUINT :=Video_GetParam(1,ADDR_DEFROST_STATUS_CIR2, 0, ?usiStatusDefrostCir2, tyUsint );	
			ELSIF VarIndex = 3 THEN
				retUINT:= Video_GetParam(1, ADDR_WEB_ONOFF, 0, 			?xOnOffRemote, tyBool );
			ELSIF VarIndex = 4 THEN
				retUINT:= Video_GetParam(1, ADDR_PLC_ONOFF, 0, 			?xRemoteStartStop, tyBool );
			ELSIF VarIndex = 5 THEN
				retUINT:= Video_GetParam(1, ADDR_DI_ONOFF, 0, 			?xLocalOnOff, tyBool );	
			ELSIF VarIndex = 6 THEN
				//retUINT := Video_GetParam(1, ADDR_REQCAP_CIR1, 0, 		?rCapacityCir1, tyReal);
				(*LOAD DATA BASE ON CIRCUIT SELECTION *)
				IF CircID = 1 THEN 
					retUINT := Video_GetParam(1,  ADDR_NR_COMP_CIR1, 0, 	?CH_Comp,				tyUint);
					retUINT := Video_GetParam(1,  ADDR_NR_FAN_CIR1 , 0, 	?CH_Fan,				tyUint);
				ELSE
					retUINT := Video_GetParam(1,  ADDR_NR_COMP_CIR2, 0, 	?CH_Comp,				tyUint);
					retUINT := Video_GetParam(1,  ADDR_NR_FAN_CIR2 , 0, 	?CH_Fan,				tyUint);
				END_IF;
				
			ELSIF VarIndex = 7 THEN
				//retUINT := Video_GetParam(1, ADDR_REQCAP_CIR2, 0, 		?rCapacityCir2, tyReal);
			ELSIF VarIndex = 8 THEN
				//retUINT := Video_GetParam(1, ADDR_RCAPMAX_CIR1, 0, 		?rCapMaxCir1, tyReal);
				IF CircID = 1 THEN 
					retUINT := Video_GetParam(1,  ADDR_COMP_CAP_CIR1, 0, 	?iReqCapacityCir,		tyInt);
					retUINT := Video_GetParam(1,  ADDR_FAN_CAP_CIR1 , 0, 	?uiFanControlSignal,	tyUint);
				ELSE
					retUINT := Video_GetParam(1,  ADDR_COMP_CAP_CIR2, 0, 	?iReqCapacityCir,		tyInt);
					retUINT := Video_GetParam(1,  ADDR_FAN_CAP_CIR2 , 0, 	?uiFanControlSignal,	tyUint);
				END_IF;
			ELSIF VarIndex = 9 THEN
				//retUINT := Video_GetParam(1, ADDR_RCAPMAX_CIR2, 0, 		?rCapMaxCir2, tyReal);
			ELSIF VarIndex = 10 THEN	
				retUINT := Video_GetParam(1, ADDR_MODE_HEAT_COOL, 0, 	?isModeHeat, tyBool);
			ELSIF VarIndex = 11 THEN
				retUINT := Video_GetParam(1, ADDR_XREVERSEVALVE_C1, 0, 	?isValveReverse1, tyBool);	
			ELSIF VarIndex = 12 THEN
				//
			ELSIF VarIndex = 13 THEN
				IF(isModeHeat) THEN
					retUINT := Video_GetParam(1,  ADDR_COMP_PB_HEAT , 0, ?tempUINT,tyUInt);
				ELSIF NOT(isModeHeat)THEN
					retUINT := Video_GetParam(1,  ADDR_COMP_PB_COOL , 0, ?tempUINT,tyUInt);
				END_IF;
			ELSIF VarIndex = 14 THEN
				retUINT := Video_GetParam(1,ADDR_XREVERSEVALVE_C2, 0, ?isValveReverse2, tyBool);
			ELSIF VarIndex = 15 THEN
				retVarTest1 := Video_GetParam(1,  ADDR_UNIT_OP_MODE,   	0, ?usiOperatingMode, tyUInt );	
			ELSIF VarIndex = 16 THEN
				(*Check if circuit 2 present*)	
				retVarTest2 := Video_GetParam(1, ADDR_NR_CIRC, 0, ?usiNbCircuits, tyUInt);
				xIsCircuit2 := (usiNbCircuits>1);
			ELSIF VarIndex = 17 THEN	
				retVarTest3 := Video_GetParam(1,  17314,   				0, ?xSuctionSetpointType, tyBool );
			ELSIF VarIndex = 18 THEN
				retVarTest4 := Video_GetParam(1,  17305,   				0, ?xEnPIDCir1, tyBool );
			ELSIF VarIndex = 19 THEN /* MC 15/11/21  Start*/
				retVarTest4 := Video_GetParam(1,  17306,   				0, ?xEnPIDCir2, tyBool );
			ELSIF VarIndex = 20 THEN 
				retVarTest4 := Video_GetParam(1,  10199,   				0, ?usiRegulProbSel, tyUsint );
			ELSIF VarIndex = 21 THEN 
				IF usiRegulProbSel = 0 	THEN
				/* Regulation on waterOutLet */
					retVarTest4 := Video_GetParam(1,  9405,   				0, ?iRegulProbe2, tyInt );	
					/* RegulationProbe 2 on waterInLet */				
					xShowRegulProbe2:= xIsChilleHP;
					strRegulType:= 'Water Oulet - Inlet';
				ELSIF usiRegulProbSel= 1 THEN
				/* Regulation on waterInLet */
					retVarTest4 := Video_GetParam(1,  9404,   				0, ?iRegulProbe2, tyInt );
					/* RegulationProbe 2 on waterOutLet */
					xShowRegulProbe2:= xIsChilleHP;
					strRegulType:= 'Water Inlet - Outlet';
				ELSE 
					xShowRegulProbe2:= FALSE;
				END_IF;
			ELSIF VarIndex = 22 THEN 
				retVarTest6 := Video_GetParam(1,  19160,   				0, ?OutDoorAlloced, tyUInt );
				xShowOutDoor:= (OutDoorAlloced <> 0);
				VarIndex 	:= 0;  /* MC 03/11/21  End */
			ELSE
				VarIndex 	:= 0;
			END_IF;
			VarIndex 		:= VarIndex +1;
		END_IF;
		
		IF getIndex = 2 THEN
			(*CHILLER*)
			IF (usiUnitModel=0) THEN
				IF (xWaterAirLinkSet) THEN
					xIsChillerWaterAir	:= TRUE;
					xIsChiller			:= FALSE;
				ELSIF NOT(xWaterAirLinkSet) THEN
					xIsChillerWaterAir	:= FALSE;
					xIsChiller			:= TRUE;
				END_IF;
				xIsHeatPumpWaterAir	:= FALSE;
				xIsHeatPump			:= FALSE;
				xIsCompressorRack 	:= FALSE;
			END_IF;
			
			(*HEAT PUMP*)
			IF (usiUnitModel=1) THEN
				IF (xWaterAirLinkSet) THEN
					xIsHeatPumpWaterAir:=TRUE;
					xIsHeatPump:=FALSE;
				ELSIF NOT(xWaterAirLinkSet) THEN
					xIsHeatPumpWaterAir:=FALSE;
					xIsHeatPump:=TRUE;
				END_IF;
				xIsChillerWaterAir	:= FALSE;
				xIsChiller			:= FALSE;
				xIsCompressorRack 	:= FALSE;
			END_IF;
			
			(*CHILLER - HEAT PUMP*)
			IF (usiUnitModel=2) THEN
				IF (xWaterAirLinkSet) THEN
					xIsHeatPumpWaterAir	:= TRUE;
					xIsChillerWaterAir	:= TRUE;
					xIsHeatPump			:= FALSE;
					xIsChiller			:= FALSE;
				ELSIF NOT(xWaterAirLinkSet) THEN
					xIsHeatPumpWaterAir	:= FALSE;
					xIsChillerWaterAir	:= FALSE;
					xIsHeatPump			:= TRUE;
					xIsChiller			:= TRUE;
				END_IF;
				xIsCompressorRack 	:= FALSE;
			END_IF;
			
			(*COMPRESSOR RACK *)
			IF (usiUnitModel=3) THEN
				xIsCompressorRack 		:= TRUE;
				xIsHeatPumpWaterAir		:= FALSE;
				xIsChillerWaterAir		:= FALSE;
				xIsHeatPump				:= FALSE;
			END_IF;
			IF xIsCompressorRack THEN
				xIsChilleHP 		:= FALSE;
			ELSE
				xIsChilleHP 		:= TRUE;			
			END_IF;	
		END_IF;
	END_IF;


(**************************************************)
(********* MOVED FROM ON TIMER SCRIPT   END     ***)
(**************************************************)
]]>
	</sourceCode>
</program>