#include "ProtoInnerCan.h"
#include "ProtoC2C.h"

#include "CnCpp.h"
#include "record.h"
#include "Runtime.h"
#include "server.h"

extern "C"{
	#include "uart_key_drv.h"
	#include "uart_linkage_drv.h"
}


void TProtoC2C::SlaveDoAlramOnOff(int aOnOff)
{
	sIm.Splite.Year = DateTime.Date.Split.Year;
	sIm.Splite.Month = DateTime.Date.Split.Month;
	sIm.Splite.Day = DateTime.Date.Split.Day;
	sIm.Splite.Hour = DateTime.Time.Split.Hour;
	sIm.Splite.Minute = DateTime.Time.Split.Minute;
	sIm.Splite.Second = DateTime.Time.Split.Second;	
	sIm.Splite.mType = etLinkageStart;
	sIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum ;	//(0,aRxMsg.Data.D8[1]);
	
	sIm.Splite.P0 = 0xF0;	//Board Index
	sIm.Splite.P1 = 0xF0;
	sIm.Splite.P2 = 0xF0;	
	sIm.Splite.dType = 0;			
	sIm.Splite.Data0 = 0x06;
	sIm.Splite.Data1 = 0;//Is Set=1  Recover=0
	sIm.Splite.iByte0	= CmdActiveDefaultSet[cmVAAlramStart][8];
	
	if(aOnOff == 0){
		FireVaTrenOned = 0;
		AlramVaOnOff = 0;
		sIm.Splite.Data0 = CmdActiveDefaultSet[cmVAAlramStop][8];
		sIm.Splite.iByte0	= CmdActiveDefaultSet[cmVAAlramStop][8];
		Record.OperateAdd_AlramSoundOff(MainCtl.fData.Split.MyNum);
		tData[0] = 0;
		tData[1] = 0;
		tData[2] = 0;
		InnerCan.OBufInsert(1, cmVAAlramStop, tData, 3);
	}else{
		AlramVaOnOff = 1;
		sIm.Splite.Data0 = CmdActiveDefaultSet[cmVAAlramStart][8];
		sIm.Splite.iByte0	= CmdActiveDefaultSet[cmVAAlramStart][8];
		Record.OperateAdd_AlramSoundOn(MainCtl.fData.Split.MyNum);
		tData[0] = 0;
		tData[1] = 0;
		tData[2] = 0;
		InnerCan.OBufInsert(1, cmVAAlramStart, tData, 3);
	}
}

void TProtoC2C::SlaveDoSend2MasterMachine(TRecordCommon aIm, unsigned char aCmd, TMsgType aMsgType)
{
	unsigned int i,aAddr;
	if(MainCtl.fData.Split.NetworkMode == 2){
		oEntry.wBody.Split.Cmd 	= aCmd;
		
		//if( (aCmd == cmManualStart) || (aCmd == cmAutoStart) ){
			if(aIm.Splite.PCtlNum == MainCtl.fData.Split.MyNum){
				if( (aIm.Splite.P0) && (aIm.Splite.P0 <= dPORT_MAX_COUNT) ){
					if(Port[aIm.Splite.P0-1].StateTable[aIm.Splite.P1-1] & dEpStateBitMask)	return;
				}
			}
		//}
		
		oEntry.wBody.Split.CtlNum = aIm.Splite.PCtlNum;
		oEntry.wBody.Split.Year 	= aIm.Splite.Year;
		oEntry.wBody.Split.Mon 	= aIm.Splite.Month;
		oEntry.wBody.Split.Day 	= aIm.Splite.Day;
		oEntry.wBody.Split.Hour 	= aIm.Splite.Hour;
		oEntry.wBody.Split.Min 	= aIm.Splite.Minute;
		oEntry.wBody.Split.Sec 	= aIm.Splite.Second;
		
		oEntry.wBody.Split.P0 = aIm.Splite.P0;
		oEntry.wBody.Split.P1 = aIm.Splite.P1;
		oEntry.wBody.Split.Prm = aIm.Splite.P2;
		oEntry.wBody.Split.dType = aIm.Splite.dType;
		oEntry.wBody.Split.Prm1 = aIm.Splite.Data0;
		oEntry.wBody.Split.Prm2 = aIm.Splite.Data1;
		
		oEntry.wBody.Split.UserCode = aIm.Splite.UserCode;
		oEntry.wBody.Split.AreaNum = aIm.Splite.AreaNum;
		
		switch(aMsgType){
			case mtBasicService	:oBufMsgInsertBasicService();	break;
			case mtFire			:oBufMsgInsertFire();		break;
			case mtSv			:oBufMsgInsertSv();		break;
			case mtLink			:oBufMsgInsertLink();		break;
			case mtFault		:oBufMsgInsertFault();		break;
			case mtNormal		:oBufMsgInsertNormal();		break;
			default:break;
		}
	}
}

void TProtoC2C::SlaveDoUploadMaskList()
{
	volatile int i;
	if(MainCtl.fData.Split.NetworkMode != 2) return;
	for(i=0; i<CtlCenter.ieMsgMask.Count; i++){
		GetMsgViaIndex(i);
		if( MaskMsg.D32[2] ){
			oEntry.wBody.Split.Cmd 	= dNetworkCmd_MaskListSend;
			oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
			oEntry.wBody.Split.Year 	= MaskMsg.Body.Year;
			oEntry.wBody.Split.Mon 	= MaskMsg.Body.Month;
			oEntry.wBody.Split.Day 	= MaskMsg.Body.Day;
			oEntry.wBody.Split.Hour 	= MaskMsg.Body.Hour;
			oEntry.wBody.Split.Min 	= MaskMsg.Body.Minute;
			oEntry.wBody.Split.Sec 	= MaskMsg.Body.Second;
		
			oEntry.wBody.Split.P0 = MaskMsg.Body.P0;
			oEntry.wBody.Split.P1 = MaskMsg.Body.P1;
			oEntry.wBody.Split.Prm = 0;
			oEntry.wBody.Split.dType = MaskMsg.Body.dType;
			
			oEntry.wBody.Split.UserCode = 0;
			if(MaskMsg.Body.P0 && MaskMsg.Body.P1){
				if( (MaskMsg.Body.P0 <= dPORT_MAX_COUNT) && (MaskMsg.Body.P1 <=dEP_MAX_COUNT_PER_PORT) ){
					oEntry.wBody.Split.UserCode = Port[MaskMsg.Body.P0-1].UcList[MaskMsg.Body.P1-1].Full;
				}
			}
			
			oBufMsgInsertNormal();
		}
	}
}


void TProtoC2C::SlaveDoUploadCtlState()
{
	volatile int i;

	for(i=0; i<CtlCenter.ieMsgMask.Count; i++){
		GetMsgViaIndex(i);
		if( MaskMsg.D32[2] ){
			oEntry.wBody.Split.Cmd 	= dNetworkCmd_MaskListSend;
			oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
			oEntry.wBody.Split.Year 	= MaskMsg.Body.Year;
			oEntry.wBody.Split.Mon 	= MaskMsg.Body.Month;
			oEntry.wBody.Split.Day 	= MaskMsg.Body.Day;
			oEntry.wBody.Split.Hour 	= MaskMsg.Body.Hour;
			oEntry.wBody.Split.Min 	= MaskMsg.Body.Minute;
			oEntry.wBody.Split.Sec 	= MaskMsg.Body.Second;
		
			oEntry.wBody.Split.P0 = MaskMsg.Body.P0;
			oEntry.wBody.Split.P1 = MaskMsg.Body.P1;
			oEntry.wBody.Split.Prm = 0;
			oEntry.wBody.Split.dType = MaskMsg.Body.dType;
			
			oEntry.wBody.Split.UserCode = 0;
			if(MaskMsg.Body.P0 && MaskMsg.Body.P1){
				if( (MaskMsg.Body.P0 <= dPORT_MAX_COUNT) && (MaskMsg.Body.P1 <=dEP_MAX_COUNT_PER_PORT) ){
					oEntry.wBody.Split.UserCode = Port[MaskMsg.Body.P0-1].UcList[MaskMsg.Body.P1-1].Full;
				}
			}
			
			oBufMsgInsertNormal();
		}
	}
}

void TProtoC2C::SlaveDoUploadStartDelay()
{
	oEntry.wBody.Split.Cmd 	= dNetworkCmd_DelayStartSend;
	oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
	oEntry.wBody.Split.P0 = IsStartDelay;
	
	oBufMsgInsertNormal();
}

void TProtoC2C::SlaveReportFbMissCount(unsigned char aFbCnt)
{
	if(MainCtl.fData.Split.NetworkMode == 2){
		oEntry.wBody.Split.Cmd 	= dNetworkCmd_SlaveFbMissReport;
		
		oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
		oEntry.wBody.Split.P0 = aFbCnt;
		oEntry.wBody.Split.P1 = 0;
		oEntry.wBody.Split.Prm = 0;
		oEntry.wBody.Split.dType = 0;
		oEntry.wBody.Split.Prm1 = 0;
		oEntry.wBody.Split.Prm2 = 0;
		
		oBufMsgInsertLink();
	}
}

void TProtoC2C::SlaveDoCheckOnlineTask()
{
	int i;
	if(MasterNum == 0)return;
	if(CheckOnlineUpdateTick < 5000){
		if(0 == CheckOnlineUpdateTick){
			if(ReportMasterOffline){
				RemoveSlaveCommFault(ProtoC2C.MasterNum);
				ReportMasterOffline = 0;
			}
		}
		CheckOnlineUpdateTick++;
	}else{ 
		CheckOnlineUpdateTick = 5001;
		if(ReportMasterOffline == 0){
			AddSlaveCommFault(ProtoC2C.MasterNum);
			ReportMasterOffline = 1;
		}
	}
}

void TProtoC2C::SlaveDoRpyCheckNormalData()
{
	int i;
	MainCtl.LoadNormalInf();
	MainCtl.LoadFaultInf();
	MainCtl.LoadMaskInf();
	
	CheckNormalDataSlave[0].Slave.Designed.Smoke 		= DesigePrmData.Count.Smoke;
	CheckNormalDataSlave[0].Slave.Designed.Tempe 		= DesigePrmData.Count.Tempe;
	CheckNormalDataSlave[0].Slave.Designed.HandReport 	= DesigePrmData.Count.HandReport;
	CheckNormalDataSlave[0].Slave.Designed.HydKp 		= DesigePrmData.Count.HydrantKp;
	CheckNormalDataSlave[0].Slave.Designed.ModuleInput	= DesigePrmData.Count.ModuleInput;
	CheckNormalDataSlave[0].Slave.Designed.ModuleInOut	= DesigePrmData.Count.ModuleInOut;
	CheckNormalDataSlave[0].Slave.Designed.ModuleOutput	= DesigePrmData.Count.ModuleOutput;
	CheckNormalDataSlave[0].Slave.Designed.Va			= DesigePrmData.Count.Va;
	CheckNormalDataSlave[0].Slave.Designed.FDisplay		= DesigePrmData.Count.DisplayPad;
	CheckNormalDataSlave[0].Slave.Designed.LnkPs		= DesigePrmData.Count.LnkPs;
	CheckNormalDataSlave[0].Slave.Designed.cOther		= DesigePrmData.Count.Other;
	
	CheckNormalDataSlave[0].Slave.Normal.Smoke 		= MainCtl.CountEpNormal.Smoker;
	CheckNormalDataSlave[0].Slave.Normal.Tempe 		= MainCtl.CountEpNormal.Tempe;
	CheckNormalDataSlave[0].Slave.Normal.HandReport 	= MainCtl.CountEpNormal.HandReport;
	CheckNormalDataSlave[0].Slave.Normal.HydKp 		= MainCtl.CountEpNormal.Fire_hydrant_Kp;
	CheckNormalDataSlave[0].Slave.Normal.ModuleInput	= MainCtl.CountEpNormal.Input;
	CheckNormalDataSlave[0].Slave.Normal.ModuleInOut	= MainCtl.CountEpNormal.InOut;
	CheckNormalDataSlave[0].Slave.Normal.ModuleOutput	= MainCtl.CountEpNormal.Output;
	CheckNormalDataSlave[0].Slave.Normal.Va			= MainCtl.CountEpNormal.VaAlram;
	CheckNormalDataSlave[0].Slave.Normal.FDisplay		= MainCtl.CountEpNormal.DisplayPad;
	CheckNormalDataSlave[0].Slave.Normal.LnkPs		= MainCtl.CountEpNormal.LnkPs;
	CheckNormalDataSlave[0].Slave.Normal.cOther		= MainCtl.CountEpNormal.UnDefine + MainCtl.CountEpNormal.Unknown;

	CheckNormalDataSlave[0].Slave.Fault.Smoke 		= MainCtl.CountEpFault.Smoker;
	CheckNormalDataSlave[0].Slave.Fault.Tempe 		= MainCtl.CountEpFault.Tempe;
	CheckNormalDataSlave[0].Slave.Fault.HandReport 		= MainCtl.CountEpFault.HandReport;
	CheckNormalDataSlave[0].Slave.Fault.HydKp 		= MainCtl.CountEpFault.Fire_hydrant_Kp;
	CheckNormalDataSlave[0].Slave.Fault.ModuleInput		= MainCtl.CountEpFault.Input;
	CheckNormalDataSlave[0].Slave.Fault.ModuleInOut		= MainCtl.CountEpFault.InOut;
	CheckNormalDataSlave[0].Slave.Fault.ModuleOutput	= MainCtl.CountEpFault.Output;
	CheckNormalDataSlave[0].Slave.Fault.Va			= MainCtl.CountEpFault.VaAlram;
	CheckNormalDataSlave[0].Slave.Fault.FDisplay		= MainCtl.CountEpFault.DisplayPad;
	CheckNormalDataSlave[0].Slave.Fault.LnkPs			= MainCtl.CountEpFault.LnkPs;
	CheckNormalDataSlave[0].Slave.Fault.cOther		= MainCtl.CountEpFault.UnDefine + MainCtl.CountEpFault.Unknown;
	
	CheckNormalDataSlave[0].Slave.Mask.Smoke 			= MainCtl.CountEpMask.Smoker;
	CheckNormalDataSlave[0].Slave.Mask.Tempe 			= MainCtl.CountEpMask.Tempe;
	CheckNormalDataSlave[0].Slave.Mask.HandReport 		= MainCtl.CountEpMask.HandReport;
	CheckNormalDataSlave[0].Slave.Mask.HydKp 			= MainCtl.CountEpMask.Fire_hydrant_Kp;
	CheckNormalDataSlave[0].Slave.Mask.ModuleInput		= MainCtl.CountEpMask.Input;
	CheckNormalDataSlave[0].Slave.Mask.ModuleInOut		= MainCtl.CountEpMask.InOut;
	CheckNormalDataSlave[0].Slave.Mask.ModuleOutput		= MainCtl.CountEpMask.Output;
	CheckNormalDataSlave[0].Slave.Mask.Va			= MainCtl.CountEpMask.VaAlram;
	CheckNormalDataSlave[0].Slave.Mask.FDisplay		= MainCtl.CountEpMask.DisplayPad;
	CheckNormalDataSlave[0].Slave.Mask.LnkPs			= MainCtl.CountEpMask.LnkPs;
	CheckNormalDataSlave[0].Slave.Mask.cOther			= MainCtl.CountEpMask.UnDefine + MainCtl.CountEpMask.Unknown;
	
	for(i=0; i<15; i++){
		oEntry.wBody.Split.Cmd 	= dNetworkCmd_CheckNormalData;
		
		oEntry.wBody.Split.Year = i+1;
		oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
		oEntry.wBody.Split.P0 = CheckNormalDataSlave[0].D8[i*6 +0];
		oEntry.wBody.Split.P1 = CheckNormalDataSlave[0].D8[i*6 +1];
		oEntry.wBody.Split.Prm = CheckNormalDataSlave[0].D8[i*6 +2];
		oEntry.wBody.Split.dType = CheckNormalDataSlave[0].D8[i*6 +3];
		oEntry.wBody.Split.Prm1 = CheckNormalDataSlave[0].D8[i*6 +4];
		oEntry.wBody.Split.Prm2 = CheckNormalDataSlave[0].D8[i*6 +5];
		
		oBufMsgInsertNormal();
	}
}

void TProtoC2C::SlaveDoGetRemoteStartStop(unsigned char CtlNum, unsigned char aP0, unsigned char aP1, unsigned char IsStart)
{
	tData[0] = 0;
	tData[1] = CtlNum;
	tData[2] = aP0;
	tData[3] = aP1;
	tData[4] = 0;
	tData[5] = 0;
	tData[6] = 0;
	
	if(IsStart)
		InnerCan.OBufInsert(1, cmAutoStart, tData, 6);
	else
		InnerCan.OBufInsert(1, cmAutoStop, tData, 6);
	
}

void TProtoC2C::SlaveDoGetRemoteStartStopUserCode(unsigned int aUc, unsigned char aDType, unsigned short aDelaySec, unsigned char aSta)
{
	unsigned int i, x, HadPrc, Result, HasMatch, tType;
	TUserCodeStr DevUCode, aWUC;

	aWUC.Full = aUc;
	if(aSta){
		for(i=0; i<dPORT_MAX_COUNT; i++){
			if(MainCtl.fData.Split.ExistTablePort[i]){
				for(x=0; x<256; x++){
					if(  (Port[i].ExistTable[x]) && (Port[i].UcList[x].Full != 0) ){
						if(Port[i].AssignType[x] != 0) tType = Port[i].AssignType[x];
						else tType = Port[i].dTypeTable[x];	
						//if(tType == aDType){
						if( (tType == aDType) || (aDType == 0) ){
							if(aWUC.Spl.BcdText_0 == 10)DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = Port[i].UcList[x].Full / 10000000 % 10; 
							if(aWUC.Spl.BcdText_1 == 10)DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = Port[i].UcList[x].Full / 1000000 % 10;
							if(aWUC.Spl.BcdText_2 == 10)DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = Port[i].UcList[x].Full / 100000 % 10;
							if(aWUC.Spl.BcdText_3 == 10)DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = Port[i].UcList[x].Full / 10000 % 10;
							if(aWUC.Spl.BcdText_4 == 10)DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = Port[i].UcList[x].Full / 1000 % 10;
							if(aWUC.Spl.BcdText_5 == 10)DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = Port[i].UcList[x].Full / 100 % 10;
							if(aWUC.Spl.BcdText_6 == 10)DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = Port[i].UcList[x].Full / 10 % 10;
							if(aWUC.Spl.BcdText_7 == 10)DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = Port[i].UcList[x].Full / 1 % 10;
							if(DevUCode.Full == aUc){
								if((Port[i].StateTable[x] & (dEpStateBitStart + dEpStateBitMask) ) == 0){
									tData[0] = 0;
									tData[1] = MainCtl.fData.Split.MyNum;
									tData[2] = i+1;
									tData[3] = x+1;
									tData[4] = 0;
									tData[5] = Port[i].dTypeTable[x];
									tData[6] = 0;
									//[0:Stop   1:Match	2:Start   3:DelayStart]
									if(aSta == 3){
										if(Port[i].DelayToDoEventType[x] == 0){
											if(Port[i].DelayTick[x] == 0){
												if(aDelaySec <600)
													Port[i].DelayTick[x] = aDelaySec +2;
												else
													Port[i].DelayTick[x] = 600 +2;
												Port[i].AddRemoveMsgDelayStart(1, i+1, x+1, aDType);
											}
											Port[i].LnkRdyGo[x] = 0xA1;
										}else{
											if(aDelaySec <600)
												Port[i].DelayTick[x] = aDelaySec +2;
											else
												Port[i].DelayTick[x] = 600 +2;
											Port[i].AddRemoveMsgDelayStart(1, i+1, x+1, aDType);
											Port[i].LnkRdyGo[x] = 0xA1;
										}
									}else
									if(aSta == 2){
										InnerCan.OBufInsert(1, cmAutoStart, tData, 6);
									}else
									if(aSta == 1){
										Port[i].LnkRdyGo[x] = 0xA1;
									}
								}
							}
						}
					}
				}
			}
		}
		for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
			if(DirectCtlBoard[i].PadIsProhibit) continue;
			for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
				if(DirectCtlBoard[i].IsRegisted[x] == 0)continue;
				if(DirectCtlBoard[i].UcList[x].Full == 0)continue;
				//if(DirectCtlBoard[i].AssignType[x] == aDType){
				if( (DirectCtlBoard[i].AssignType[x] == aDType) || (aDType == 0) ){
					if(aWUC.Spl.BcdText_0 == 10)DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = DirectCtlBoard[i].UcList[x].Full / 10000000 % 10; 
					if(aWUC.Spl.BcdText_1 == 10)DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = DirectCtlBoard[i].UcList[x].Full / 1000000 % 10;
					if(aWUC.Spl.BcdText_2 == 10)DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = DirectCtlBoard[i].UcList[x].Full / 100000 % 10;
					if(aWUC.Spl.BcdText_3 == 10)DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = DirectCtlBoard[i].UcList[x].Full / 10000 % 10;
					if(aWUC.Spl.BcdText_4 == 10)DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = DirectCtlBoard[i].UcList[x].Full / 1000 % 10;
					if(aWUC.Spl.BcdText_5 == 10)DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = DirectCtlBoard[i].UcList[x].Full / 100 % 10;
					if(aWUC.Spl.BcdText_6 == 10)DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = DirectCtlBoard[i].UcList[x].Full / 10 % 10;
					if(aWUC.Spl.BcdText_7 == 10)DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = DirectCtlBoard[i].UcList[x].Full / 1 % 10;
					if(aUc == DevUCode.Full){
						if( DirectCtlBoard[i].IsRegisted[x] ){
							if(DirectCtlBoard[i].OutputState[x] == 0){
								tData[0] = 0;
								tData[1] = MainCtl.fData.Split.MyNum;
								tData[2] = i+dADDR_DIRECTPAD_START;
								tData[3] = x+1;
								tData[4] = 2;
								tData[5] = 0;
								//[0:Stop   1:Match	2:Start   3:DelayStart]
								if(aSta == 3){
									if(Port[i].DelayToDoEventType[x] == 0){
										if(DirectCtlBoard[i].DelayTick[x] == 0){
											DirectCtlBoard[i].DelayTick[x] = aDelaySec +2;
											DirectCtlBoard[i].AddRemoveMsgDelayStart(1, i +dADDR_DIRECTPAD_START, x +1);
											DirectCtlBoard[i].LnkRdyGo[x] = 0xA1;
											if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
												DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
												tData[4] = 1;
												InnerCan.OBufInsert(1, cmDirectCtlPadLinkageState, tData, 6);
											}
										}
									}else{
										if(DirectCtlBoard[i].DelayTick[x] == 0){
											DirectCtlBoard[i].DelayTick[x] = aDelaySec +2;
											DirectCtlBoard[i].AddRemoveMsgDelayStart(1, i +dADDR_DIRECTPAD_START, x +1);
											DirectCtlBoard[i].LnkRdyGo[x] = 0xA1;
											if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
												DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
												tData[4] = 1;
												InnerCan.OBufInsert(1, cmDirectCtlPadLinkageState, tData, 6);
											}
										}
									}
								}else
								if(aSta == 2){
									tData[4] = 2;
									InnerCan.OBufInsert(1, cmDirectCtlPadLinkageState, tData, 6);
								}else
								if(aSta == 1){
									DirectCtlBoard[i].LnkRdyGo[x] = 0xA1;
									if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
										DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
										tData[4] = 1;
										InnerCan.OBufInsert(1, cmDirectCtlPadLinkageState, tData, 6);
									}
								}
							}
						}
					}
				}
			}
		}
	}else{
		//Here Stop
		for(i=0; i<dPORT_MAX_COUNT; i++){
			if(MainCtl.fData.Split.ExistTablePort[i]){
				for(x=0; x<256; x++){
					if(  (Port[i].ExistTable[x]) && (Port[i].UcList[x].Full != 0) ){
						if(Port[i].AssignType[x] != 0) tType = Port[i].AssignType[x];
						else tType = Port[i].dTypeTable[x];	
						//if(tType == aDType){
						if( (tType == aDType) || (aDType == 0) ){
							if(aWUC.Spl.BcdText_0 == 10)DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = Port[i].UcList[x].Full / 10000000 % 10; 
							if(aWUC.Spl.BcdText_1 == 10)DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = Port[i].UcList[x].Full / 1000000 % 10;
							if(aWUC.Spl.BcdText_2 == 10)DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = Port[i].UcList[x].Full / 100000 % 10;
							if(aWUC.Spl.BcdText_3 == 10)DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = Port[i].UcList[x].Full / 10000 % 10;
							if(aWUC.Spl.BcdText_4 == 10)DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = Port[i].UcList[x].Full / 1000 % 10;
							if(aWUC.Spl.BcdText_5 == 10)DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = Port[i].UcList[x].Full / 100 % 10;
							if(aWUC.Spl.BcdText_6 == 10)DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = Port[i].UcList[x].Full / 10 % 10;
							if(aWUC.Spl.BcdText_7 == 10)DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = Port[i].UcList[x].Full / 1 % 10;
							if(DevUCode.Full == aUc){
								if((Port[i].StateTable[x] & (dEpStateBitStart + dEpStateBitMask) ) == 0){
									tData[0] = 0;
									tData[1] = MainCtl.fData.Split.MyNum;
									tData[2] = i+1;
									tData[3] = x+1;
									tData[4] = 0;
									tData[5] = Port[i].dTypeTable[x];
									tData[6] = 0;
									//[0:Stop   1:Match	2:Start   3:DelayStart] Here Is Stop

									InnerCan.OBufInsert(1, cmAutoStop, tData, 6);
								}
							}
						}
					}
				}
			}
		}
		for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
			if(DirectCtlBoard[i].PadIsProhibit) continue;
			for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
				if(DirectCtlBoard[i].IsRegisted[x] == 0)continue;
				if(DirectCtlBoard[i].UcList[x].Full == 0)continue;
				//if(DirectCtlBoard[i].AssignType[x] == aDType){
				if( (DirectCtlBoard[i].AssignType[x] == aDType) || (aDType == 0) ){
					if(aWUC.Spl.BcdText_0 == 10)DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = DirectCtlBoard[i].UcList[x].Full / 10000000 % 10; 
					if(aWUC.Spl.BcdText_1 == 10)DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = DirectCtlBoard[i].UcList[x].Full / 1000000 % 10;
					if(aWUC.Spl.BcdText_2 == 10)DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = DirectCtlBoard[i].UcList[x].Full / 100000 % 10;
					if(aWUC.Spl.BcdText_3 == 10)DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = DirectCtlBoard[i].UcList[x].Full / 10000 % 10;
					if(aWUC.Spl.BcdText_4 == 10)DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = DirectCtlBoard[i].UcList[x].Full / 1000 % 10;
					if(aWUC.Spl.BcdText_5 == 10)DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = DirectCtlBoard[i].UcList[x].Full / 100 % 10;
					if(aWUC.Spl.BcdText_6 == 10)DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = DirectCtlBoard[i].UcList[x].Full / 10 % 10;
					if(aWUC.Spl.BcdText_7 == 10)DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = DirectCtlBoard[i].UcList[x].Full / 1 % 10;
					if(aUc == DevUCode.Full){
						if( DirectCtlBoard[i].IsRegisted[x] ){
							if(DirectCtlBoard[i].OutputState[x] == 1){
								tData[0] = 0;
								tData[1] = MainCtl.fData.Split.MyNum;
								tData[2] = i+dADDR_DIRECTPAD_START;
								tData[3] = x+1;
								tData[4] = 2;
								tData[5] = 0;
								//[0:Stop   1:Match	2:Start   3:DelayStart] Here Is Stop
								tData[4] = 0;
								InnerCan.OBufInsert(1, cmDirectCtlPadLinkageState, tData, 6);
							}
						}
					}
				}
			}
		}		
	}
}

void TProtoC2C::SlaveDoGetRemoteStartStopUserCodeSimulate(unsigned int aUc, unsigned char aDType, unsigned short aDelaySec, unsigned char aSta)
{
	unsigned int i, x, HadPrc, Result, HasMatch, tType;
	TUserCodeStr DevUCode, aWUC;

	aWUC.Full = aUc;
	if(aSta){
		for(i=0; i<dPORT_MAX_COUNT; i++){
			if(MainCtl.fData.Split.ExistTablePort[i]){
				for(x=0; x<256; x++){
					if(  (Port[i].ExistTable[x]) && (Port[i].UcList[x].Full != 0) ){
						if(Port[i].AssignType[x] != 0) tType = Port[i].AssignType[x];
						else tType = Port[i].dTypeTable[x];	
						//if(tType == aDType){
						if( (tType == aDType) || (aDType == 0) ){
							if(aWUC.Spl.BcdText_0 == 10)DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = Port[i].UcList[x].Full / 10000000 % 10; 
							if(aWUC.Spl.BcdText_1 == 10)DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = Port[i].UcList[x].Full / 1000000 % 10;
							if(aWUC.Spl.BcdText_2 == 10)DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = Port[i].UcList[x].Full / 100000 % 10;
							if(aWUC.Spl.BcdText_3 == 10)DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = Port[i].UcList[x].Full / 10000 % 10;
							if(aWUC.Spl.BcdText_4 == 10)DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = Port[i].UcList[x].Full / 1000 % 10;
							if(aWUC.Spl.BcdText_5 == 10)DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = Port[i].UcList[x].Full / 100 % 10;
							if(aWUC.Spl.BcdText_6 == 10)DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = Port[i].UcList[x].Full / 10 % 10;
							if(aWUC.Spl.BcdText_7 == 10)DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = Port[i].UcList[x].Full / 1 % 10;
							if(DevUCode.Full == aUc){
								if((Port[i].StateTable[x] & (dEpStateBitStart + dEpStateBitMask) ) == 0){
									//[0:Stop   1:Match	2:Start   3:DelayStart]
									if(aSta == 3){
										if(Port[i].DelayToDoEventType[x] == 0){
											if(Port[i].DelayTick[x] == 0){
												if(aDelaySec <600)
													Port[i].DelayTick[x] = aDelaySec +2;
												else
													Port[i].DelayTick[x] = 600 +2;
												Port[i].DelayToDoEventType[x] = 1;
												Port[i].AddRemoveMsgDelayStartSimulate(1, i+1, x+1, aDType);
											}
										}else{
											if(Port[i].DelayTick[x] == 0){
												if(aDelaySec <600)
													Port[i].DelayTick[x] = aDelaySec +2;
												else
													Port[i].DelayTick[x] = 600 +2;
												Port[i].DelayToDoEventType[x] = 1;
												Port[i].AddRemoveMsgDelayStartSimulate(1, i+1, x+1, aDType);
											}
										}
									}else
									if(aSta == 2){
										sIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
										sIm.Splite.P0 = i+1;
										sIm.Splite.P1 = x+1;
										sIm.Splite.P2 = 0;
										if(Port[i].AssignType[x] != 0){
											sIm.Splite.dType = Port[i].AssignType[x];
										}else{
											sIm.Splite.dType = Port[i].dTypeTable[x]; 
										}
										sIm.Splite.UserCode = Port[i].UcList[x].Full;
										TControlCenter::sSimStartGot(&sIm);
									}else
									if(aSta == 1){
										//Port[i].LnkRdyGo[x] = 0xA1;
									}
								}
							}
						}
					}
				}
			}
		}
		for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
			if(DirectCtlBoard[i].PadIsProhibit) continue;
			for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
				if(DirectCtlBoard[i].IsRegisted[x] == 0)continue;
				if(DirectCtlBoard[i].UcList[x].Full == 0)continue;
				//if(DirectCtlBoard[i].AssignType[x] == aDType){
				if( (DirectCtlBoard[i].AssignType[x] == aDType) || (aDType == 0) ){
					if(aWUC.Spl.BcdText_0 == 10)DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = DirectCtlBoard[i].UcList[x].Full / 10000000 % 10; 
					if(aWUC.Spl.BcdText_1 == 10)DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = DirectCtlBoard[i].UcList[x].Full / 1000000 % 10;
					if(aWUC.Spl.BcdText_2 == 10)DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = DirectCtlBoard[i].UcList[x].Full / 100000 % 10;
					if(aWUC.Spl.BcdText_3 == 10)DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = DirectCtlBoard[i].UcList[x].Full / 10000 % 10;
					if(aWUC.Spl.BcdText_4 == 10)DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = DirectCtlBoard[i].UcList[x].Full / 1000 % 10;
					if(aWUC.Spl.BcdText_5 == 10)DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = DirectCtlBoard[i].UcList[x].Full / 100 % 10;
					if(aWUC.Spl.BcdText_6 == 10)DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = DirectCtlBoard[i].UcList[x].Full / 10 % 10;
					if(aWUC.Spl.BcdText_7 == 10)DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = DirectCtlBoard[i].UcList[x].Full / 1 % 10;
					if(aUc == DevUCode.Full){
						if( DirectCtlBoard[i].IsRegisted[x] ){
							if(DirectCtlBoard[i].OutputState[x] == 0){
								//tData[0] = 0;
								//tData[1] = MainCtl.fData.Split.MyNum;
								//tData[2] = i+dADDR_DIRECTPAD_START;
								//tData[3] = x+1;
								//tData[4] = 2;
								//tData[5] = 0;
								//[0:Stop   1:Match	2:Start   3:DelayStart]
								if(aSta == 3){
									if(Port[i].DelayToDoEventType[x] == 0){
										if(DirectCtlBoard[i].DelayTick[x] == 0){
											Port[i].DelayToDoEventType[x] = 1;
											DirectCtlBoard[i].DelayTick[x] = aDelaySec +2;
											DirectCtlBoard[i].AddRemoveMsgDelayStartSimulate(1, i +dADDR_DIRECTPAD_START, x +1);
											//DirectCtlBoard[i].LnkRdyGo[x] = 0xA1;
											//if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
												//DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
												//tData[4] = 1;
												//InnerCan.OBufInsert(1, cmDirectCtlPadLinkageState, tData, 6);
											//}
										}
									}else{
										if(DirectCtlBoard[i].DelayTick[x] == 0){
											Port[i].DelayToDoEventType[x] = 1;
											DirectCtlBoard[i].DelayTick[x] = aDelaySec +2;
											DirectCtlBoard[i].AddRemoveMsgDelayStartSimulate(1, i +dADDR_DIRECTPAD_START, x +1);
											//DirectCtlBoard[i].LnkRdyGo[x] = 0xA1;
											//if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
												//DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
												//tData[4] = 1;
												//InnerCan.OBufInsert(1, cmDirectCtlPadLinkageState, tData, 6);
											//}
										}
									}
								}else
								if(aSta == 2){
									DirectCtlBoard[i].AddStartStopSimulate(1, i+dADDR_DIRECTPAD_START, x+1);
								}else
								if(aSta == 1){
									//DirectCtlBoard[i].LnkRdyGo[x] = 0xA1;
									//if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
									//	DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
									//	tData[4] = 1;
									//	InnerCan.OBufInsert(1, cmDirectCtlPadLinkageState, tData, 6);
									//}
								}
							}
						}
					}
				}
			}
		}
	}else{
		//Here Stop
		for(i=0; i<dPORT_MAX_COUNT; i++){
			if(MainCtl.fData.Split.ExistTablePort[i]){
				for(x=0; x<256; x++){
					if(  (Port[i].ExistTable[x]) && (Port[i].UcList[x].Full != 0) ){
						if(Port[i].AssignType[x] != 0) tType = Port[i].AssignType[x];
						else tType = Port[i].dTypeTable[x];	
						//if(tType == aDType){
						if( (tType == aDType) || (aDType == 0) ){
							if(aWUC.Spl.BcdText_0 == 10)DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = Port[i].UcList[x].Full / 10000000 % 10; 
							if(aWUC.Spl.BcdText_1 == 10)DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = Port[i].UcList[x].Full / 1000000 % 10;
							if(aWUC.Spl.BcdText_2 == 10)DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = Port[i].UcList[x].Full / 100000 % 10;
							if(aWUC.Spl.BcdText_3 == 10)DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = Port[i].UcList[x].Full / 10000 % 10;
							if(aWUC.Spl.BcdText_4 == 10)DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = Port[i].UcList[x].Full / 1000 % 10;
							if(aWUC.Spl.BcdText_5 == 10)DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = Port[i].UcList[x].Full / 100 % 10;
							if(aWUC.Spl.BcdText_6 == 10)DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = Port[i].UcList[x].Full / 10 % 10;
							if(aWUC.Spl.BcdText_7 == 10)DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = Port[i].UcList[x].Full / 1 % 10;
							if(DevUCode.Full == aUc){
								if((Port[i].StateTable[x] & (dEpStateBitStart + dEpStateBitMask) ) == 0){
									//[0:Stop   1:Match	2:Start   3:DelayStart] Here Is Stop
									sIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
									sIm.Splite.P0 = i+1;
									sIm.Splite.P1 = x+1;
									sIm.Splite.P2 = 0;
									if(Port[i].AssignType[x] != 0){
										sIm.Splite.dType = Port[i].AssignType[x];
									}else{
										sIm.Splite.dType = Port[i].dTypeTable[x]; 
									}	
									sIm.Splite.UserCode = Port[i].UcList[x].Full;									
									TControlCenter::sSimStartCancelGot(&sIm);
								}
							}
						}
					}
				}
			}
		}
		for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
			if(DirectCtlBoard[i].PadIsProhibit) continue;
			for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
				if(DirectCtlBoard[i].IsRegisted[x] == 0)continue;
				if(DirectCtlBoard[i].UcList[x].Full == 0)continue;
				//if(DirectCtlBoard[i].AssignType[x] == aDType){
				if( (DirectCtlBoard[i].AssignType[x] == aDType) || (aDType == 0) ){
					if(aWUC.Spl.BcdText_0 == 10)DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = DirectCtlBoard[i].UcList[x].Full / 10000000 % 10; 
					if(aWUC.Spl.BcdText_1 == 10)DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = DirectCtlBoard[i].UcList[x].Full / 1000000 % 10;
					if(aWUC.Spl.BcdText_2 == 10)DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = DirectCtlBoard[i].UcList[x].Full / 100000 % 10;
					if(aWUC.Spl.BcdText_3 == 10)DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = DirectCtlBoard[i].UcList[x].Full / 10000 % 10;
					if(aWUC.Spl.BcdText_4 == 10)DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = DirectCtlBoard[i].UcList[x].Full / 1000 % 10;
					if(aWUC.Spl.BcdText_5 == 10)DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = DirectCtlBoard[i].UcList[x].Full / 100 % 10;
					if(aWUC.Spl.BcdText_6 == 10)DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = DirectCtlBoard[i].UcList[x].Full / 10 % 10;
					if(aWUC.Spl.BcdText_7 == 10)DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = DirectCtlBoard[i].UcList[x].Full / 1 % 10;
					if(aUc == DevUCode.Full){
						if(DirectCtlBoard[i].IsRegisted[x]){
							if(DirectCtlBoard[i].OutputState[x] == 0){
								//[0:Stop   1:Match	2:Start   3:DelayStart] Here Is Stop
								DirectCtlBoard[i].AddStartStopSimulate(1, i+dADDR_DIRECTPAD_START, x+1);
							}
						}
					}
				}
			}
		}		
	}
}

void TProtoC2C::SlaveDoNetDescpRpy(unsigned char aCtlNum, unsigned char aP0)
{
	if(aP0 == 0)return;
	if(aP0 <= dPORT_MAX_COUNT){
		DescpOnePortRpy(aCtlNum, aP0);
	}else
	if(aP0 == (dPORT_MAX_COUNT +1)){
		DescpAllDirectPadRpy(aCtlNum);
	}
}

void TProtoC2C::SlaveDoSend2MasterDelayStart(TRecordCommon aIm, unsigned char aCmd)
{
	unsigned int i,aAddr;
	if(MainCtl.fData.Split.NetworkMode == 2){
		oEntry.wBody.Split.Cmd 	= dNetworkCmd_AutoStartDelay;
		
		oEntry.wBody.Split.CtlNum  = aIm.Splite.PCtlNum;
		oEntry.wBody.Split.Year 	= aIm.Splite.Year;
		oEntry.wBody.Split.Mon 	= aIm.Splite.Month;
		oEntry.wBody.Split.Day 	= aIm.Splite.Day;
		oEntry.wBody.Split.Hour 	= aIm.Splite.Hour;
		oEntry.wBody.Split.Min 	= aIm.Splite.Minute;
		oEntry.wBody.Split.Sec 	= aIm.Splite.Second;
		
		oEntry.wBody.Split.P0 = aIm.Splite.P0;
		oEntry.wBody.Split.P1 = aIm.Splite.P1;
		oEntry.wBody.Split.Prm = aIm.Splite.P2;
		oEntry.wBody.Split.dType = aIm.Splite.dType;
		oEntry.wBody.Split.Prm1 = aIm.Splite.iByte0;
		oEntry.wBody.Split.Prm2 = aIm.Splite.Data1;
		
		oEntry.wBody.Split.UserCode = aIm.Splite.UserCode;
		
		oBufMsgInsertLink();
	}
}

void TProtoC2C::SlaveDoSend2MasterDelayStartSimulate(TRecordCommon aIm, unsigned char aCmd)
{
	unsigned int i,aAddr;
	if(MainCtl.fData.Split.NetworkMode == 2){
		oEntry.wBody.Split.Cmd 	= dNetworkCmd_AutoStartDelaySimulate;
		
		oEntry.wBody.Split.CtlNum  = aIm.Splite.PCtlNum;
		oEntry.wBody.Split.Year 	= aIm.Splite.Year;
		oEntry.wBody.Split.Mon 	= aIm.Splite.Month;
		oEntry.wBody.Split.Day 	= aIm.Splite.Day;
		oEntry.wBody.Split.Hour 	= aIm.Splite.Hour;
		oEntry.wBody.Split.Min 	= aIm.Splite.Minute;
		oEntry.wBody.Split.Sec 	= aIm.Splite.Second;
		
		oEntry.wBody.Split.P0 = aIm.Splite.P0;
		oEntry.wBody.Split.P1 = aIm.Splite.P1;
		oEntry.wBody.Split.Prm = aIm.Splite.P2;
		oEntry.wBody.Split.dType = aIm.Splite.dType;
		oEntry.wBody.Split.Prm1 = aIm.Splite.iByte0;
		oEntry.wBody.Split.Prm2 = aIm.Splite.Data1;
		
		oEntry.wBody.Split.UserCode = aIm.Splite.UserCode;
		
		oBufMsgInsertLink();
	}
}



unsigned int TProtoC2C::GetThisCtlCountDownTime(unsigned int aNum, unsigned int aP0, unsigned int aP1)
{
	unsigned int DelaySec; 
	unsigned char P0, P1;
	if(aNum > dNETWORK_MACHINE_COUNT)return 0;
	
	if((aP0 >0) && (aP1 >0)){
		if(aP0 <= dPORT_MAX_COUNT){
			P0 = aP0-1;
			P1 = aP1-1;
			if( Port[P0].DelayTick[P1] > 2){
				DelaySec = Port[P0].DelayTick[P1] -2;
			}else{
				DelaySec = 0;
			}
		}else
		if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
			P0 = aP0-dADDR_DIRECTPAD_START;
			P1 = aP1-1;
			if(DirectCtlBoard[P0].DelayTick[P1] > 2){
				DelaySec = DirectCtlBoard[P0].DelayTick[P1] -2;
			}else{
				DelaySec = 0;
			}
		}else{
			DelaySec = 0;
		}
		return DelaySec;
	}else{
		return 0;
	}
	return 0;
}

void TProtoC2C::SlaveSetAutoState(unsigned char aSta)
{
	if(aSta == 0){
		AllowAuto = 0;
		if(AllowManual == 0){
			AllowManual = 1;
			ProcessUserSetManualState();
		}
	}else{
		AllowAuto = 1;
	}
	Allow.ExtChange(AllowAuto);
	ProcessUserSetAutoState();
}

void TProtoC2C::SlaveSetManualState(unsigned char aSta)
{
	if(aSta == 0){
		AllowManual = 0;
		if(AllowAuto == 0){
			AllowAuto = 1;
			ProcessUserSetAutoState();
		}
	}else{
		AllowManual = 1;
	}
	Allow.ExtChange(AllowAuto);
	ProcessUserSetManualState();	
}

void TProtoC2C::SlaveSetAllowState(unsigned char aSta)
{
	if(aSta == 0){
		AllowAuto = 1;
		AllowManual = 2;
	}else
	if(aSta == 1){
		AllowAuto = 1;
		AllowManual = 0;
	}else
	if(aSta == 2){
		AllowAuto = 0;
		AllowManual = 2;
	}else
	if(aSta == 3){
		AllowAuto = 1;
		AllowManual = 2;
	}
	Allow.ExtChange(AllowAuto);
	ProcessUserSetAutoState();	
}

void TProtoC2C::SlaveSetAllowStateBySpc(unsigned char CtlNum, unsigned char aSta)
{
	if(CtlNum == MainCtl.fData.Split.MyNum){
		if(aSta)
			SlaveSetAllowState(1);
		else
			SlaveSetAllowState(2);
	}
}

int TProtoC2C::CheckIsInSlaveList(unsigned char aCn)
{
	int Found;
	if( aCn && (aCn <= dNETWORK_MACHINE_COUNT) ){
		Found =0;
		for(int i=0; (i<SlaveCount) && (i<dNETWORK_MACHINE_COUNT); i++){
			if(aCn == SlaveList[i]){
				Found =1;
				break;
			}
		}
		if(Found)return 1;else return 0;
	}else{
		return 0;
	}
}




