FUNCTION_BLOCK PMC_WaitUntilCmdLb
	CASE CmdSta OF
		0:  (* IDLING*)
			(*check if the rising edge of execute*)
			IF (PreExecute = FALSE) AND (Execute = TRUE) THEN
				(*command being triggered*)
				(*Sending Command to PMC*)
				(* read current heart beat of field bus*)
				memcpy(pDest := ADR(cmdHB),pSrc := ADR(FBin_pointer) + P2H_START + P2H_CMDC_OFFSET,length := 1);
				(*update command heart beat*)
				cmdHB := cmdHB + 1;
				cmdHB := cmdHB AND 255;
				(*--------------write command info*)
				(*command ID*)
				memcpy(pDest := ADR(FBout_pointer) + H2P_START + H2P_CMDID_OFFSET,pSrc := ADR(cmdID),length := 2);
				(*command label*)
				memcpy(pDest := ADR(FBout_pointer) + H2P_START + H2P_CMDLB_OFFSET,pSrc := ADR(cmdLB),length := 2);
				(*XBOT ID:*)
				memcpy(pDest := ADR(FBout_pointer) + H2P_START + H2P_DATA_OFFSET + 4,pSrc := ADR(XbotID),length := 1);
				(*Trigger option: *)
				memcpy(pDest := ADR(FBout_pointer) + H2P_START + H2P_DATA_OFFSET + 3,pSrc := ADR(option),length := 1);
				(*Trigger Type: *)
				memcpy(pDest := ADR(FBout_pointer) + H2P_START + H2P_DATA_OFFSET + 5,pSrc := ADR(type_),length := 1);
				(*Trigger count: *)
				memcpy(pDest := ADR(FBout_pointer) + H2P_START + H2P_DATA_OFFSET + 6,pSrc := ADR(count),length := 1);
				(*Trigger Xbot ID*)
				memcpy(pDest := ADR(FBout_pointer) + H2P_START + H2P_DATA_OFFSET + 7,pSrc := ADR(TriggerXID),length := 1);
				(*Trigger Type*)
				memcpy(pDest := ADR(FBout_pointer) + H2P_START + H2P_DATA_OFFSET + 8,pSrc := ADR(TriggerType),length := 1);
				(*Trigger Cmd Label*)
				memcpy(pDest := ADR(FBout_pointer) + H2P_START + H2P_DATA_OFFSET + 9,pSrc := ADR(TriggerCmdLb),length := 2);
				(*Command label type*)
				memcpy(pDest := ADR(FBout_pointer) + H2P_START + H2P_DATA_OFFSET + 11,pSrc := ADR(LabelType),length := 1);
				(*write command heart beat*)
				memcpy(pDest := ADR(FBout_pointer) + H2P_START + H2P_CMDC_OFFSET,pSrc := ADR(cmdHB),length := 1);
				(*reset output*)
				Error := FALSE;
				ErrorID := 0;
				Done := FALSE;
				Busy := TRUE;
				Aborted := FALSE;
				timeout := 0;
				(*change state to BUSY*)
				CmdSta := 1;
			END_IF;
		1:  (* Waiting for Command return*)
			memcpy(pDest := ADR(cmdHBRtn),pSrc := ADR(FBin_pointer) + P2H_START + P2H_CMDC_OFFSET,length := 1);
			cmdHBRtn := cmdHBRtn AND 255;
			dd := cmdHBRtn - cmdHB;
			IF dd <> 0 THEN
				(*no reply from PMC*)
				timeout := timeout + 1;
				IF timeout > PMC_TIMEOUT THEN
					(* Statement section IF*)
					Error := TRUE;
					ErrorID := 8201;
					CmdSta := 0;
				END_IF;
			ELSE
				(*PMC reply*)
				memcpy(pDest := ADR(cmdIDRtn),pSrc := ADR(FBin_pointer) + P2H_START + P2H_CMDID_OFFSET,length := 2);
				IF cmdIDRtn <> cmdID THEN
					(* Statement section IF*)
					Aborted := TRUE;
				ELSE
					Done := TRUE;
				END_IF;
				memcpy(pDest := ADR(ErrorID),pSrc := ADR(FBin_pointer) + P2H_START + P2H_RTN_OFFSET,length := 2);
				IF ErrorID <> 0 THEN
					(* Statement section IF*)
					Error := TRUE;
				END_IF;
				Busy := FALSE;
				(*change state to IDLE*)
				CmdSta := 0;
			END_IF;
	END_CASE;
	
	(*save execute singnal to local*)
	PreExecute := Execute;
END_FUNCTION_BLOCK
