#include "RuntimeData.h"
#include "Register.h"
#include "DevMask.h"
#include "server.h"

extern "C"{
	#include "user_norflash.h"
	//#include "user_eeprom.h"
	#include "fdcan_task.h"
	#include "uart_key_drv.h"
	#include "HW_config.h"
}
#define dOffSetTransmite_Flag	0x10000

#define 	dclSearch	clBlue
#define	dclUpLoad	clTeal

class TItemList *TRegister::sIList;
void TRegister::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "回路->自动注册");
		TextRender_string24(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, "按\"确认\"键开始本机注册,若需注册单一回路可设置");
		TRoundRect::sDrawBySize(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] + 36, 120, 30, 7, clBlue, "开始注册", 14, clNearWhite);
	
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 4, clNearWhite, ItemColor, "分回路注册");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 38, clNearWhite, ItemColor, "回路号");
		TRoundRect::sDrawBySize(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 76, 120, 30, 7, clNearWhite, "回路注册", 14, clNearBlack);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Loop->Auto Register");
		TextRender_string24(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, "All loops register");
		TRoundRect::sDrawBySize(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] + 36, 120, 30, 7, clBlue, "Register", 14, clNearWhite);
	
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 4, clNearWhite, ItemColor, "Single Loop Register");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 38, clNearWhite, ItemColor, "LoopID");
		TRoundRect::sDrawBySize(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 76, 120, 30, 7, clNearWhite, "Register", 14, clNearBlack);
	}
}

void TRegister::Init()
{
	IsOnShow = 0;
	IsOnGo = 0;
	IsPermission = 0;

	InitDataBuf();
	IsDevRegisting = 0;
	IsTimeOutReturn = 0;

	Tag.Reset();
	if(MainCtl.fData.Split.NetworkMode== 1){
		Tag.InxMax = 6;
	}else{
		Tag.InxMax = 2;
	}
	
	IsOverWrite =1;
}

void TRegister::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

void TRegister::DrawSelf(void)
{
	RenderBackGround();

	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	
	for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	//if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	DrawFixText();

	IList->Edit[0].Init(Lv.Grid.Left[1] +100, Lv.Grid.Top[1] + 36, 100,30,0,0,1,clNearBlack);
	IList->Edit[0].SetMaxLen(2);
	IList->Edit[0].Str.Clear();
	IList->Edit[0].SetText("01", 24);
	IList->Edit[0].SetPstTail();
	IList->Edit[0].Show(); 
	
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);

	if(Tag.InxMax > 2){
		if(LanguageEnCn==0){
			TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 4, clNearWhite, ItemColor, "远程注册");
			TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 38, clNearWhite, ItemColor, "机器号");
		}else{
			TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 4, clNearWhite, ItemColor, "Register Via Remoting");
			TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 38, clNearWhite, ItemColor, "CtlID");
		}
		IList->Edit[1].Init(Lv.Grid.Left[2] +100, Lv.Grid.Top[2] + 36, 100,30,0,0,1,clNearBlack);
		IList->Edit[1].SetMaxLen(2);
		IList->Edit[1].Str.Clear();
		IList->Edit[1].SetText("01", 24);
		IList->Edit[1].SetPstTail();
		IList->Edit[1].Show();	
		if(LanguageEnCn==0){
			TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 80, clNearWhite, ItemColor, "回路号");
		}else{
			TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 80, clNearWhite, ItemColor, "LoopID");
		}
		IList->Edit[2].Init(Lv.Grid.Left[2] +100, Lv.Grid.Top[2] + 80, 100,30,0,0,1,clNearBlack);
		IList->Edit[2].SetMaxLen(2);
		IList->Edit[2].Str.Clear();
		IList->Edit[2].SetText("01", 24);
		IList->Edit[2].SetPstTail();
		IList->Edit[2].Show(); 
		if(LanguageEnCn==0){
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 120, 128, 30, 5, clNearWhite, "全回路注册", 4, clNearBlack);
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +160, Lv.Grid.Top[2] + 120, 128, 30, 5, clNearWhite, "分回路注册", 4, clNearBlack);
		}else{
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 120, 128, 30, 5, clNearWhite,  "RegAllLoop", 4, clNearBlack);
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +160, Lv.Grid.Top[2] + 120, 128, 30, 5, clNearWhite, "Reg Single", 4, clNearBlack);
		}
	}
}

void TRegister::DrawCommonByExReturn()
{
	for(int i=0; i<2; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	//if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	
	DrawFixText();
	IList->Edit[0].Show(); 
	
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
}

void TRegister::Show()
{
	IsOnShow = 1;
	DrawSelf();
}

void TRegister::Show4Slave()
{
	IsOnShow = 1;
	IsOnNetwork = 2;
	RenderBackGround();
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "回路注册,集中机呼叫执行,请稍后");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Registering,Call By Master,Wait");
	}
	
	for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	if(LanguageEnCn==0){
		if(RegWhat == 0){
			TextRender_string24(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, "全回路注册");
		}else{
			TextRender_string24(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, "单回路注册");
		}
	}else{
		if(RegWhat == 0){
			TextRender_string24(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, "RegAllLoop");
		}else{
			TextRender_string24(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, "Reg Single");
		}
	}
}

void TRegister::FullRedraw(int Prm)
{
	if(Prm == 0){
		IsOnShow = 1;
		DrawSelf();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		DrawCommonByExReturn();
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

void TRegister::sDelayMs(unsigned int aMs)
{
	volatile unsigned int mTick;
	volatile unsigned int Tick;
	for(Tick=0; Tick<aMs; Tick++){
		for(mTick=0; mTick<160000; mTick++);
		sys_error_led_task();
	}
}

void TRegister::ClearSelected(void)
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			if(LanguageEnCn==0)tText="开始注册"; else tText="Register";
			TRoundRect::sDrawBySize(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 1:
			IList->Edit[0].Selected = 0;
			IList->Edit[0].Show();
			break;
		case 2:
			if(LanguageEnCn==0)tText="回路注册"; else tText="Register";
			TRoundRect::sDrawBySize(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 76, 120, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 3:
			IList->Edit[1].Selected = 0;
			IList->Edit[1].Show();
			break;
		case 4:
			IList->Edit[2].Selected = 0;
			IList->Edit[2].Show();
			break;
		case 5:
			if(LanguageEnCn==0)tText="全回路注册"; else tText="RegAllLoop";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 120, 128, 30, 5, clNearWhite, tText, 4, clNearBlack);
			break;
		case 6:
			if(LanguageEnCn==0)tText="分回路注册"; else tText="Reg Single";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +160, Lv.Grid.Top[2] + 120, 128, 30, 5, clNearWhite, tText, 4, clNearBlack);
			break;
	}		
}

void TRegister::Check4Selected(void)
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			if(LanguageEnCn==0)tText="开始注册"; else tText="Register";
			TRoundRect::sDrawBySize(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] + 36, 120, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		case 1:
			IList->Edit[0].Selected = 1;
			IList->Edit[0].Show();
			IList->Edit[0].ShowCursor();
			break;
		case 2:
			if(LanguageEnCn==0)tText="回路注册"; else tText="Register";
			TRoundRect::sDrawBySize(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 76, 120, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		case 3:
			IList->Edit[1].Selected = 1;
			IList->Edit[1].Show();
			IList->Edit[1].ShowCursor();
			break;
		case 4:
			IList->Edit[2].Selected = 1;
			IList->Edit[2].Show();
			IList->Edit[2].ShowCursor();
			break;
		case 5:
			if(LanguageEnCn==0)tText="全回路注册"; else tText="RegAllLoop";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 120, 128, 30, 5, clBlue, tText, 4, clNearWhite);
			break;
		case 6:
			if(LanguageEnCn==0)tText="分回路注册"; else tText="Reg Single";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +160, Lv.Grid.Top[2] + 120, 128, 30, 5, clBlue, tText, 4, clNearWhite);
			break;
	}		
}

void TRegister::ShowPortCount()
{
	int PortCount =0;
	for(int i=0; i<dPORT_MAX_COUNT; i++){
		if(HasRpy[i]){
			PortCount++;
		}
	}
	if(LanguageEnCn==0)TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, "回路数量");
	else TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, "Loop(s)");
	TextDigitRender2Right24(Lv.Panel.Left+ 104, Lv.Panel.Top + 4, clNearWhite, PanelColor, PortCount);

	if(IsOnNetwork == 2){
		SlaveReplyPortCount(PortCount);
	}
}

void TRegister::ShowDetail(int aForce)
{
	int x1,x2,x3, y, Inv;
	if( (HasDrawDetailOnFinish == 0) || (aForce) ){
		x1 = Lv.Panel.Left+6;
		x2 = Lv.Panel.Left+170;
		x3 = Lv.Panel.Left+340;
		y = Lv.Panel.Top + 4;
		Inv = 96;
		VertLineRender(x2 -4,		y + 30, 			160,			clNearWhite);
		VertLineRender(x3 -4,		y + 30, 			160,			clNearWhite);
	
		HasDrawDetailOnFinish = 1;
		if(LanguageEnCn==0)TextRender_string24(600, y, clNearWhite, PanelColor, "总数量:"); 
		else TextRender_string24(600, y, clNearWhite, PanelColor, "Total:");
			TextDigitRender4Right24(600+84, y, clNearWhite, PanelColor, Count.All % 10000);
		y+=30;
		if(LanguageEnCn==0)TextRender_string24(x1, y, clNearWhite, PanelColor, "烟感");
		else TextRender_string24(x1, y, clNearWhite, PanelColor, "Smoke");
			TextDigitRender4Right24(x1+100, y, clNearWhite, PanelColor, Count.Smoke % 10000);
		if(LanguageEnCn==0)TextRender_string24(x2, y, clNearWhite, PanelColor, "温感");
		else TextRender_string24(x2, y, clNearWhite, PanelColor, "Tempe");
			TextDigitRender4Right24(x2+100, y, clNearWhite, PanelColor, Count.Tempe % 10000);
		if(LanguageEnCn==0)TextRender_string24(x3, y, clNearWhite, PanelColor, "手报");
		else TextRender_string24(x3, y, clNearWhite, PanelColor, "HandPad");
			TextDigitRender4Right24(x3+100, y, clNearWhite, PanelColor, Count.HandReport % 10000);
		y+=30;
		if(LanguageEnCn==0)TextRender_string24(x1, y, clNearWhite, PanelColor, "消钮");
		else TextRender_string24(x1, y, clNearWhite, PanelColor, "Hydrant");
			TextDigitRender4Right24(x1+100, y, clNearWhite, PanelColor, Count.HydKp % 10000);
		if(LanguageEnCn==0)TextRender_string24(x2, y, clNearWhite, PanelColor, "输入");
		else TextRender_string24(x2, y, clNearWhite, PanelColor, "Input");
			TextDigitRender4Right24(x2+100, y, clNearWhite, PanelColor, Count.ModuleInput % 10000);
		if(LanguageEnCn==0)TextRender_string24(x3, y, clNearWhite, PanelColor, "输入输出");
		else TextRender_string24(x3, y, clNearWhite, PanelColor, "In&Out");
			TextDigitRender4Right24(x3+100, y, clNearWhite, PanelColor, Count.ModuleInOut % 10000);
		y+=30;
		if(LanguageEnCn==0)TextRender_string24(x1, y, clNearWhite, PanelColor, "输出");
		else TextRender_string24(x1, y, clNearWhite, PanelColor, "Output");
			TextDigitRender4Right24(x1+100, y, clNearWhite, PanelColor, Count.ModuleOutput % 10000);
		if(LanguageEnCn==0)TextRender_string24(x2, y, clNearWhite, PanelColor, "声光");
		else TextRender_string24(x2, y, clNearWhite, PanelColor, "Acousto-");
			TextDigitRender4Right24(x2+100, y, clNearWhite, PanelColor, Count.Va % 10000);
		if(LanguageEnCn==0)TextRender_string24(x3, y, clNearWhite, PanelColor, "联动电源");
		else TextRender_string24(x3, y, clNearWhite, PanelColor, "Power");
			TextDigitRender4Right24(x3+100, y, clNearWhite, PanelColor, Count.LINKAGE_POWERSUPLY % 10000);
		y+=30;
		if(LanguageEnCn==0)TextRender_string24(x1, y, clNearWhite, PanelColor, "烟温感");
		else TextRender_string24(x1, y, clNearWhite, PanelColor, "SmokeTempe");
			TextDigitRender4Right24(x1+100, y, clNearWhite, PanelColor, Count.SmokeTempe % 10000);
		if(LanguageEnCn==0)TextRender_string24(x2, y, clNearWhite, PanelColor, "层显");
		else TextRender_string24(x2, y, clNearWhite, PanelColor, "DispPad");
			TextDigitRender4Right24(x2+100, y, clNearWhite, PanelColor, Count.FDisplay % 10000);
		if(LanguageEnCn==0)TextRender_string24(x3, y, clNearWhite, PanelColor, "其他");
		else (x3, y, clNearWhite, PanelColor, "Others");
			TextDigitRender4Right24(x3+100, y, clNearWhite, PanelColor, Count.cOther % 10000);
	}
}

void TRegister::DrawProgressAll()
{
	int i, x, y, aPrg, aDiv, sVal, tVal;
	unsigned int aAddr;
	
	aDiv = 0;
	for(i=0; i<40; i++){
		if(HasRpy[i])aDiv++;
	}
	if(aDiv == 0)return;
	
	sVal = 0;
	for(i=0; i<40; i++){
		if(HasRpy[i]){
			sVal += ProgressVal[i];
		}
	}
	aPrg = sVal / aDiv /2;
	
	tVal = 0;
	for(i=0; i<40; i++){
		if(SearchCompleted[i] == 0)continue;
		for(x=0; x<50; x++){
			aAddr = dAddrSdRam_Register + dOffSetTransmite_Flag + (i * 50) + x;	
			if(*(volatile unsigned char *)(aAddr)){
				tVal ++;
			}
		}
	}
	tVal = tVal / aDiv;
	aPrg += tVal;
	TotalProgress = aPrg;
	
	x = Lv.PanelSmall.Left; y = Lv.PanelSmall.Top +6;
	TProgressBar::sDrawBySize(x,y,Lv.PanelSmall.Width-100, 32,aPrg,100,dclSearch,clGray,clBlack);
	y += 4;
	x = Lv.PanelSmall.Right - 80;
	TextDigitRender3Right24(x,y ,clNearWhite, ContentColor, aPrg);
	x = Lv.PanelSmall.Right - 80 + 36;
	TextRender_string24(x, y ,clNearWhite, "%");
	
	if(IsOnNetwork == 2){
		SlaveReplyProgress(TotalProgress);
	}
}

void TRegister::DrawProgress100()
{
	int i, x, y, aPrg;
	
	aPrg = 100;
	x = Lv.PanelSmall.Left; y = Lv.PanelSmall.Top +6;
	TProgressBar::sDrawBySize(x,y,Lv.PanelSmall.Width - 100,32,aPrg,100,dclSearch,clGray,clBlack);
	y += 4;
	x = Lv.PanelSmall.Right - 80;
	TextDigitRender3Right24(x,y ,clNearWhite, ContentColor, aPrg);
	x = Lv.PanelSmall.Right - 80 + 36;
	TextRender_string24(x, y ,clNearWhite, "%");
}

void TRegister::InitDataBuf()
{
	int x, y;
	unsigned int aAddr;
	aAddr = dAddrSdRam_Register;
	for(x=0; x<40; x++){
		for(y=0; y<256; y++) *(volatile unsigned char *)(aAddr + (x*256) + y) = 0;
	}
	aAddr = dAddrSdRam_Register + dOffSetTransmite_Flag;
	for(x=0; x<40; x++){	
		for(y=0; y<50; y++) *(volatile unsigned char *)(aAddr + (x * 50) + y) = 0;
	}
}

void TRegister::iePushOneMsgReport(TRecordCommon aIm, unsigned char aCmd)
{
	unsigned char aP0, aPgs;
	
	if(aCmd == cmPortRegistFinished){
		aP0 = aIm.Splite.P0;
		if(aP0 == 0)return;
		if(aP0 > 40)return;
		if(RegWhat){
			if(RegWhat == aP0)SearchCompleted[aP0-1] = 1;
		}else{
			SearchCompleted[aP0-1] = 1;
		}
	}else
	if(aCmd == cmPortRegistProgress){
		aP0 = aIm.Splite.P0;
		if(aP0 == 0)return;
		if(aP0 > 40)return;
		aPgs = aIm.Splite.P1;
		if(RegWhat){
			if(RegWhat == aP0){
				HasRpy[aP0-1] = 1;
				ProgressVal[aP0 -1] = aPgs;
			}
		}else{
			HasRpy[aP0-1] = 1;
			ProgressVal[aP0 -1] = aPgs;
		}
	}else
	if(aCmd == cmQueryPortReg){
		//D0 D1=PortNum D2=Index  D3-7 dType
		aP0 = aIm.Splite.PCtlNum;
		aPgs = aIm.Splite.P0;
		if(aP0 == 0)return;
		if(aP0 > 40)return;
		if(aPgs == 0)return;
		if(aPgs > 50)return;
		RecData[3] = aIm.Splite.P1;
		RecData[4] = aIm.Splite.P2;
		RecData[5] = aIm.Splite.dType;
		RecData[6] = aIm.Splite.Data0;
		RecData[7] = aIm.Splite.Data1;
		PushRegisterData(aP0, aPgs);		
	}
}

void TRegister::PermissionCmdReturnDraw()
{
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);

}

void TRegister::PermissionCmdGO()
{
	if(PermissionCmd == 0){
		//Local All
		IsOnNetwork = 0;
		HasDrawDetailOnFinish = 0;
		InitDataBuf();
		ClearProgressVal();
		Count.Reset();
		IsTimeOutReturn = 0;
		WastTimeSec = 0;
		TotalProgress = 0;
		QueryTimeOut = 0;
		IsTransmiting = 0;
		IsQuerying = 0;
		if(1){
			IsOverWrite = 1;
		}else{
			IsOverWrite = 0;
		}
		RegWhat = 0;
		SendCmdAll();
		IsOnGo = 1;
		HasDone = 0;
		ResetHasSend = 0;
		RdyToShowTick = 0;
		DelayCheckTick = 0;
		IsDevRegisting = 0x00A5A5A5;
		Record.OperateAdd_Registe(1);
		
		for(int i=0;i<4;i++)Psv.Exist[i] =0;
	}else
	if(PermissionCmd == 1){
		//Local One Port
		IsOnNetwork = 0;
		HasDrawDetailOnFinish = 0;
		InitDataBuf();
		ClearProgressVal();
		Count.Reset();
		IsTimeOutReturn = 0;
		WastTimeSec = 0;
		TotalProgress = 0;
		QueryTimeOut = 0;
		IsTransmiting = 0;
		IsQuerying = 0;
		if(1){
			IsOverWrite = 1;
		}else{
			IsOverWrite = 0;
		}
		RegWhat = PortNumOnGoing;
		//SendCmdAll();
		SendCmdOne(PortNumOnGoing);
		IsOnGo = 1;
		HasDone = 0;
		ResetHasSend = 0;
		RdyToShowTick = 0;
		DelayCheckTick = 0;
		IsDevRegisting = 0x00A5A5A5;
		Record.OperateAdd_Registe(1);
	}else
	if(PermissionCmd == 2){
		//Network All
		MasterStartByRemote(Nw.CtlNum, PortNumOnGoing);
	}else
	if(PermissionCmd == 3){
		//Network One Port
		MasterStartByRemote(Nw.CtlNum, PortNumOnGoing);
	}
}

TGuiMsgReturn TRegister::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg;
	unsigned char tCtlNum, aCc, Ow, Err;
	
	if(IsPermission){
		PmsRtnMsg = PmsIntervene4Gui.KeyIn(aKey);
		if(PmsRtnMsg == guiMsgOkAndReturn){
			//Enter And Run Go
			IsPermission = 0;
			PermissionCmdReturnDraw();
			PermissionCmdGO();
		}else
		if(PmsRtnMsg == guiMsgReturn){
			//Not Enter Redraw
			IsPermission = 0;
			PermissionCmdReturnDraw();
		}
		return aMsg;
	}
	
	if(IsOnGo){
		switch(aKey){
			case VK_UP:

				break;
			case VK_DOWN:

				break;
			case VK_TAB:

				break;
			case VK_EXECUTE:

				break;
			case VK_RETURN:
				//IsOnShow = 0;
				//IsOnGo =0;
				//aMsg = guiMsgReturn;
				break;
		}		
	}else{
		switch(aKey){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
			case VK_DELETE:
				if(Tag.Inx ==1){
					IList->Edit[0].KeyIn(aKey);
					aCc = IList->Edit[0].Str.ToInteger();
					if( (aCc == 0) || (aCc > 24) ){
						IList->Edit[0].SelectedColor = clRed;
					}else{
						IList->Edit[0].SelectedColor = clBlue;
					}						
					IList->Edit[0].Show();
					IList->Edit[0].ShowCursor();
				}else
				if(Tag.Inx ==3){
					IList->Edit[1].KeyIn(aKey);
					aCc = IList->Edit[1].Str.ToInteger();
					if( (aCc == 0) || (aCc > dNETWORK_MACHINE_COUNT) ){
						IList->Edit[1].SelectedColor = clRed;
					}else{
						IList->Edit[1].SelectedColor = clBlue;
					}						
					IList->Edit[1].Show();
					IList->Edit[1].ShowCursor();
				}else
				if(Tag.Inx ==4){
					IList->Edit[2].KeyIn(aKey);
					aCc = IList->Edit[2].Str.ToInteger();
					if( (aCc == 0) || (aCc > 24) ){
						IList->Edit[2].SelectedColor = clRed;
					}else{
						IList->Edit[2].SelectedColor = clBlue;
					}						
					IList->Edit[2].Show();
					IList->Edit[2].ShowCursor();
				}else
				if(Tag.Inx == 5){
					if(aKey == VK_RIGHT){
						ClearSelected();
						Tag.Inx++;
						Check4Selected();
					}
				}else
				if(Tag.Inx == 6){
					if(aKey == VK_LEFT){
						ClearSelected();
						Tag.Inx--;
						Check4Selected();
					}
				}
				break;
			case VK_UP:
				if(Tag.Inx >0){
					ClearSelected();
					Tag.Inx--;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(Tag.Inx <Tag.InxMax){
					ClearSelected();
					Tag.Inx++;
					Check4Selected();
				}
				break;
			case VK_TAB:

				break;
			case dfKEY_SETUP:

				break;
			case dfKEY_QUERY:

				break;
			case VK_EXECUTE:
				if(Tag.Inx == 0){
					if(SystemPermission > 2){
						PermissionCmd = 0;
						PermissionCmdGO();	
					}else{
						IsPermission = 1;
						PermissionCmd = 0;
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 3);
					}
				}else
				if(Tag.Inx == 2){
					aCc = IList->Edit[0].Str.ToInteger();
					if( (aCc >0) && (aCc < 25) ){
						PortNumOnGoing = aCc;
						if(SystemPermission > 2){
							PermissionCmd = 1;
							PermissionCmdGO();
						}else{
							IsPermission = 1;
							PermissionCmd = 1;
							PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 3);
						}
					}
				}else
				if(Tag.Inx == 5){
					//NetWork all Port
					tCtlNum = IList->Edit[1].Str.ToInteger();
					if( (tCtlNum) && (tCtlNum<=dNETWORK_MACHINE_COUNT) ){
						if( (tCtlNum < ProtoC2C.CtlMask.StartNum) || (tCtlNum > ProtoC2C.CtlMask.EndNum) ){
							if(ProtoC2C.CheckIsInSlaveList(tCtlNum)){
								IsOnNetwork = 1;
								Nw.CtlNum = tCtlNum;
								PortNumOnGoing =0; 
								PermissionCmd = 2;
								if(SystemPermission > 2){
									PermissionCmdGO();	
								}else{
									IsPermission = 1;
									PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 3);
								}
							}
						}
					}
				}else
				if(Tag.Inx == 6){
					//NetWork a Port
					tCtlNum = IList->Edit[1].Str.ToInteger();
					if( (tCtlNum) && (tCtlNum<=dNETWORK_MACHINE_COUNT) ){
						if( (tCtlNum < ProtoC2C.CtlMask.StartNum) || (tCtlNum > ProtoC2C.CtlMask.EndNum) ){
							if(ProtoC2C.CheckIsInSlaveList(tCtlNum)){
								IsOnNetwork = 1;
								aCc = IList->Edit[2].Str.ToInteger();
								if( (aCc >0) && (aCc < 25) ){
									Nw.CtlNum = tCtlNum;
									PortNumOnGoing = aCc;
									PermissionCmd = 3;
									if(SystemPermission > 2){
										PermissionCmdGO();
									}else{
										IsPermission = 1;
										PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 3);
									}
								}
							}
						}
					}
				}
				break;
			case VK_RETURN:
				IsOnShow = 0;
				IsOnGo =0;
				IsDevRegisting = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}
	
	return aMsg;	
}

void TRegister::ClearProgressVal()
{
	int i;
	IsQuerying =0;
	QueryNum =0;
	for(i=0; i<40; i++){
		ProgressVal[i] = 0;
		SearchCompleted[i] = 0;
		HasRpy[i] =0;
		HasQuery[i] =0;
		TransmiteCompleted[i] = 0;
		ItemsCount[i] =0;
	}
}

void TRegister::SendCmdAll()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =1;
	
	aIde.BitF.Cmd = cmAllPortAutoRegist;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);
}

void TRegister::SendCmdOne(unsigned char aCc)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =4;
	
	aIde.BitF.Cmd = cmSinglePortAutoRegist;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = aCc;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);
}

void TRegister::SendCmdQuery(unsigned char aCc)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =3;
	
	aIde.BitF.Cmd = cmQueryPortReg;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = aCc;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);
}

void TRegister::SendCmdAbort()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =3;
	
	aIde.BitF.Cmd = cmPortRegistAbort;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = ReqPath;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);
}

unsigned char TRegister::GetItemsCount(unsigned char aCc)
{
	unsigned int aAddr,x;
	int i;
	x = aCc;
	x = x * 256;
	aAddr = dAddrSdRam_Register + x;
	x = 0;
	for(i=0; i<250; i++){
		if(*(volatile unsigned char *)(aAddr)){
			x++;
		}
	}
	return x;
}

int TRegister::CheckIsDone()
{
	int i, done;
	done = 1;
	if(WastTimeSec > 20){
		if(TotalProgress > 60){
			for(i=0; i<dPORT_MAX_COUNT; i++){
				if(HasRpy[i]){
					if(TransmiteCompleted[i] == 0){
						//NotDone
						done = 0;
					}
				}
			}
			if(done){
				GetEpDevCount();
				ShowDetail();
				DrawProgress100();
				if(IsOnNetwork == 2){
					Nw.Wait4SendFinishedTick =0;
					SlaveReplyProgress(100);
					SlaveReplyAllEpCount();
				}
				if(LanguageEnCn==0)
					TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, "注册完成,准备复位");
				else 
					TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, "Registered Completed");
				LoadToMainCtl();
				SaveToFlash();
				HasDone = 1;
				return 1;
			}
		}
	}
	if(WastTimeSec > 300){
		IsOnGo = 0;
		if(LanguageEnCn==0)
			TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, "注册超时,返回    ");
		else 
			TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, "Time Out,Return  ");
		//SaveToFlash();
		IsTimeOutReturn = 1;
		return 1;
	}
	return 0;
}
	
int TRegister::Task1000Ms()
{
	int i, dn;
	if(IsOnNetwork == 0){
		if(IsOnShow == 0)return 0;
		if(IsOnGo == 0)return 0;
		PreventPermissionDown();
		PreventEventShow();
		
		if(WastTimeSec < 400)WastTimeSec++;
		if(RdyToShowTick <2){
			RdyToShowTick++;
			return 0;
		}
		
		ShowPortCount();
		
		if(HasDone){
			if(ResetHasSend == 0){
				if(get_can_tx_complete()){
					ResetHasSend = 1;
					IsOnGo = 0;
					//DevMask.ClearAllMask();
					//CtlCenter.ReadMaskFromFlash();
					//ExRecord.SetStateCodeMask();
					//FecBusCRT.SetStateCodeMask();
					Record.ReadDevUserSet();
					ProtoC2C.ResetCoomm();
					FecBusCRT.ResetCallBack();
					FecStd.ResetCallBack();
					Record.OperateAdd_Registe(2);
					Record.OperateAdd_Reset(MainCtl.fData.Split.MyNum);
					ResetCallBack();
					IsDevRegisting = 0;
					MainMenu.Hide();
					return 100;
				}
			}
			return 0;
		}else
		if(IsTimeOutReturn){
			if(ResetHasSend == 0){
				if(get_can_tx_complete()){
					SendCmdAbort();
					ResetHasSend = 1;
					IsOnGo = 0;
					ProtoC2C.ResetCoomm();
					FecBusCRT.ResetCallBack();
					ResetCallBack();
					IsDevRegisting = 0;
					MainMenu.Hide();
					return 100;
				}
			}
			return 0;
		}
		
		if(CheckIsDone())return 0;
	
		if(RegWhat == 0){
			if(DelayCheckTick < 15){
				DelayCheckTick++;
			}else{
				if(IsQuerying == 0){
					dn = 1;
					for(i=0; i<dPORT_MAX_COUNT; i++){
						if(HasRpy[i]){
							if(SearchCompleted[i] == 0){
								dn = 0;
							}
						}
					}
					if(dn){
						QueryTimeOut = 0;
						QueryNum = 0;
						IsTransmiting = 0;
						IsQuerying = 1;
					}
				}else{
					if(IsTransmiting == 0){
						for(i=0; i<dPORT_MAX_COUNT; i++){
							if(SearchCompleted[QueryNum]){
								if(TransmiteCompleted[QueryNum] == 0){
									ClearCircuitTranmiteData(QueryNum);
									SendCmdQuery(QueryNum+1);
									QueryTimeOut = 0;
									IsTransmiting = 1;
									break;
								}
							}
							if(QueryNum >= (dPORT_MAX_COUNT-1)){
								QueryNum = 0;
							}else{
								QueryNum++;
							}
						}
					}else{
						QueryTimeOut++;
						if(QueryTimeOut > 20){
							QueryTimeOut = 0;
							IsTransmiting = 0;
						}	
						if(TransmiteCompleted[QueryNum]){
							ItemsCount[QueryNum] = GetItemsCount(QueryNum);
							IsTransmiting = 0;
						}
					}

				}		
			}
		}else{
			const unsigned char aP0 = RegWhat-1;
			if(DelayCheckTick < 15){
				DelayCheckTick++;
			}else{
				if(IsQuerying == 0){
					dn = 1;
					if(HasRpy[aP0]){
						if(SearchCompleted[aP0] == 0){
							dn = 0;
						}
					}
					if(dn){
						QueryTimeOut = 0;
						IsTransmiting = 0;
						IsQuerying = 1;
					}
				}else{
					if(IsTransmiting == 0){
						if(SearchCompleted[aP0]){
							if(TransmiteCompleted[aP0] == 0){
								ClearCircuitTranmiteData(aP0);
								SendCmdQuery(RegWhat);
								QueryTimeOut = 0;
								IsTransmiting = 1;
							}
						}
					}else{
						QueryTimeOut++;
						if(QueryTimeOut > 20){
							QueryTimeOut = 0;
							IsTransmiting = 0;
						}	
						if(TransmiteCompleted[aP0]){
							ItemsCount[aP0] = GetItemsCount(aP0);
							IsTransmiting = 0;
						}
					}

				}		
			}
		}
		DrawProgressAll();
	}else
	if(IsOnNetwork == 1){
		if(IsOnShow == 0)return 0;
		if(IsOnGo == 0)return 0;
		PreventPermissionDown();
		PreventEventShow();
		
		if(WastTimeSec < 600)WastTimeSec++;
		if(Nw.SlaveState == 0){
			if(LanguageEnCn==0)TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 		"等待区域机响应     ");
			else TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 				"Wait for Slaver Rpy");
			if(WastTimeSec > 16){
				if(LanguageEnCn==0)TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 	"区域机无响应,已退回");
				else TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 			"Slaver Offline,Back");
				IsOnNetwork =0;
				IsOnGo = 0;
				IsDevRegisting =0;
			}else{
				TextDigitRender2_24(Lv.Panel.Left+ 186, Lv.Panel.Top + 4, clNearWhite, PanelColor, 16- WastTimeSec);
			}
		}
		if(RdyToShowTick <2){
			RdyToShowTick++;
			return 0;
		}
		
		if(Nw.SlaveState == 2){
			if(LanguageEnCn==0)TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 		"区域机已拒绝执行   ");
			else	TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 				"Slaver Rejected    "); 
			IsOnNetwork =0;
			IsOnGo = 0;
			IsDevRegisting =0;
		}if(Nw.SlaveState == 3){
			if(LanguageEnCn==0)TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 		"区域机远程任务繁忙 ");
			else TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 				"Slaver Busy        ");
			IsOnNetwork =0;
			IsOnGo = 0;
			IsDevRegisting =0;
		}else
		if(Nw.SlaveState == 1){
			if(Nw.Completed){
				if(Nw.MasterShowDetailTick == 0){
					ShowDetail(1);
				}else
				if(Nw.MasterShowDetailTick >4){
					if(LanguageEnCn==0)
						TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, "已完成");
					else
						TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, "Completed");
					IsOnNetwork =0;
					IsOnGo = 0;
					IsDevRegisting =0;
				}else{
					
				}
				Nw.MasterShowDetailTick++;
			}else{
				if(Nw.HadTransited2Master){
					if(CompCount()){
						Nw.Completed = 1;
						MasterSendCmdEpCountCorrect();
					}else{
						MasterSendCmdReqEpCount();
					}
					Nw.HadTransited2Master = 0;
				}
				if(Nw.EpCountHasUpdate){
					ShowDetail(1);
					Nw.EpCountHasUpdate =0;
				}
				if(Nw.PortCountHasUpdate){
					//Show Port Count
					if(LanguageEnCn==0)TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 	"区域机       ");
					else TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 	"Slaver       ");
					TextDigitRender2_24(Lv.Panel.Left+ 88, Lv.Panel.Top + 4, clNearWhite, PanelColor, Nw.CtlNum);
					
					if(LanguageEnCn==0)TextRender_string24(Lv.Panel.Left+130, Lv.Panel.Top + 4, clNearWhite, PanelColor, 	"回路数量     ");
					else TextRender_string24(Lv.Panel.Left+130, Lv.Panel.Top + 4, clNearWhite, PanelColor, 	"Loop(s)      ");
					TextDigitRender2_24(Lv.Panel.Left+ 226, Lv.Panel.Top + 4, clNearWhite, PanelColor, Nw.PortCount);
					Nw.PortCountHasUpdate =0;
				}
				if(Nw.ProgressHasUpdate){
					int x,y;
					x = Lv.PanelSmall.Left; y = Lv.PanelSmall.Top +6;
					TProgressBar::sDrawBySize(x,y,Lv.PanelSmall.Width-100, 32,Nw.ProgressVal,100,dclSearch,clGray,clBlack);
					y += 4;
					x = Lv.PanelSmall.Right - 80;
					TextDigitRender3Right24(x,y ,clNearWhite, ContentColor, Nw.ProgressVal);
					x = Lv.PanelSmall.Right - 80 + 36;
					TextRender_string24(x, y ,clNearWhite, "%");
					if(Nw.ProgressVal ==100){
						Nw.HasDone =1;
					}
					Nw.ProgressHasUpdate =0;
				}
			}
		}
		if(WastTimeSec > 400){
			IsOnGo = 0;
			if(LanguageEnCn==0)TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, 	"注册超时,返回    ");
			else TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, 			"Time Out         ");
			//SaveToFlash();
			IsOnGo = 0;
			IsDevRegisting =0;
			return 1;
		}	
	}else
	if(IsOnNetwork == 2){
		//Move to Server.cpp
		//SlaveModeTask1000Ms();
	}
	return 0;
}


void TRegister::SlaveModeTask1000Ms()
{
	int i, dn;
	
	if(IsOnShow == 0)return;
	if(IsOnGo == 0)return;
	PreventPermissionDown();
	PreventEventShow();
	
	if(WastTimeSec < 410)WastTimeSec++;
	if(WastTimeSec > 400){
		if(ResetHasSend == 0){
			if(get_can_tx_complete()){
				SendCmdAbort();
				ResetHasSend = 1;
				IsOnGo = 0;
				Record.ReadDevUserSet();
				ProtoC2C.ResetCoomm();
				FecBusCRT.ResetCallBack();
				ResetCallBack();
				IsDevRegisting = 0;
				MainMenu.Hide();
				return;
			}
		}
		return;
	}
	if(RdyToShowTick <2){
		RdyToShowTick++;
		return;
	}
	
	ShowPortCount();
	
	if(HasDone){
		if(ResetHasSend == 0){
			if(Nw.Completed){	
				if(Nw.Wait4SendFinishedTick<11)Nw.Wait4SendFinishedTick++;
				if(Nw.Wait4SendFinishedTick > 10){
					if(get_can_tx_complete()){
						ResetHasSend = 1;
						IsOnShow =0;
						IsOnGo = 0;
						//DevMask.ClearAllMask();
						//CtlCenter.ReadMaskFromFlash();
						//ExRecord.SetStateCodeMask();
						//FecBusCRT.SetStateCodeMask();
						ProtoC2C.ResetCoomm();
						FecBusCRT.ResetCallBack();
						FecStd.ResetCallBack();
						Record.OperateAdd_Registe(2);
						Record.OperateAdd_Reset(MainCtl.fData.Split.MyNum);
						ResetCallBack();
						IsDevRegisting = 0;
						MainMenu.Hide();
						return;
					}
				}
				return;
			}else{
				if(Nw.ReSendEpCountTick > 12){
					Nw.ReSendEpCountTick = 0;
					SlaveReplyAllEpCount();
				}
				Nw.ReSendEpCountTick ++;
			}
		}
		return;
	}
	
	if(CheckIsDone())return;

	if(RegWhat == 0){
		if(DelayCheckTick < 15){
			DelayCheckTick++;
		}else{
			if(IsQuerying == 0){
				dn = 1;
				for(i=0; i<dPORT_MAX_COUNT; i++){
					if(HasRpy[i]){
						if(SearchCompleted[i] == 0){
							dn = 0;
						}
					}
				}
				if(dn){
					QueryTimeOut = 0;
					QueryNum = 0;
					IsTransmiting = 0;
					IsQuerying = 1;
				}
			}else{
				if(IsTransmiting == 0){
					for(i=0; i<dPORT_MAX_COUNT; i++){
						if(SearchCompleted[QueryNum]){
							if(TransmiteCompleted[QueryNum] == 0){
								ClearCircuitTranmiteData(QueryNum);
								SendCmdQuery(QueryNum+1);
								QueryTimeOut = 0;
								IsTransmiting = 1;
								break;
							}
						}
						if(QueryNum >= (dPORT_MAX_COUNT-1)){
							QueryNum = 0;
						}else{
							QueryNum++;
						}
					}
				}else{
					QueryTimeOut++;
					if(QueryTimeOut > 20){
						QueryTimeOut = 0;
						IsTransmiting = 0;
					}	
					if(TransmiteCompleted[QueryNum]){
						ItemsCount[QueryNum] = GetItemsCount(QueryNum);
						IsTransmiting = 0;
					}
				}

			}		
		}
	}else{
		const unsigned char aP0 = RegWhat-1;
		if(DelayCheckTick < 15){
			DelayCheckTick++;
		}else{
			if(IsQuerying == 0){
				dn = 1;
				if(HasRpy[aP0]){
					if(SearchCompleted[aP0] == 0){
						dn = 0;
					}
				}
				if(dn){
					QueryTimeOut = 0;
					IsTransmiting = 0;
					IsQuerying = 1;
				}
			}else{
				if(IsTransmiting == 0){
					if(SearchCompleted[aP0]){
						if(TransmiteCompleted[aP0] == 0){
							ClearCircuitTranmiteData(aP0);
							SendCmdQuery(RegWhat);
							QueryTimeOut = 0;
							IsTransmiting = 1;
						}
					}
				}else{
					QueryTimeOut++;
					if(QueryTimeOut > 20){
						QueryTimeOut = 0;
						IsTransmiting = 0;
					}	
					if(TransmiteCompleted[aP0]){
						ItemsCount[aP0] = GetItemsCount(aP0);
						IsTransmiting = 0;
					}
				}

			}		
		}
	}
	DrawProgressAll();
}

void TRegister::ClearCircuitTranmiteData(unsigned char aCc)
{
	int y, iCc;
	unsigned int aAddr;
	
	iCc = aCc;

	aAddr = dAddrSdRam_Register;
	for(y=0; y<64; y++){
		*(volatile unsigned int *)(aAddr + (iCc*256) + y * 4) = 0;
	}
	aAddr = dAddrSdRam_Register + dOffSetTransmite_Flag;
	for(y=0; y<50; y++){
		*(volatile unsigned char *)(aAddr + (iCc * 50) + y) =0;
	}
}

void TRegister::PushRegisterData(unsigned char aCc, unsigned char aInx)
{
	int x, y, aCount;
	unsigned int aAddr;
	
	if(RegWhat){
		if(aCc != RegWhat)return;
	}
	
	x = aCc -1;
	y = (aInx-1) * 5;
	aAddr = dAddrSdRam_Register;
	*(volatile unsigned char *)(aAddr + (x*256) + y + 0) = RecData[3];
	*(volatile unsigned char *)(aAddr + (x*256) + y + 1) = RecData[4];
	*(volatile unsigned char *)(aAddr + (x*256) + y + 2) = RecData[5];
	*(volatile unsigned char *)(aAddr + (x*256) + y + 3) = RecData[6];
	*(volatile unsigned char *)(aAddr + (x*256) + y + 4) = RecData[7];

	y = (aInx -1);
	aAddr = dAddrSdRam_Register + dOffSetTransmite_Flag;
	*(volatile unsigned char *)(aAddr + (x * 50) + y) = 1;
	
	aCount =0;
	for(y=0; y<50; y++){
		if(*(volatile unsigned char *)(aAddr + (x * 50) + y) )aCount++;
	}
	if(aCount >= 50)TransmiteCompleted[x] = 1;
	
	QueryNumRpy = x;
}

void TRegister::KeyPadUartPushData(unsigned char aCmd, unsigned char *pData)
{
	unsigned char tHwType, tNum, tLen;
	int i,x;
	
	if(IsOnShow == 0)return;
	if(IsOnGo == 0)return;
	tHwType = pData[0];
	tNum = pData[1];
	tLen = pData[2];
	if(aCmd == cmQueryBoardModel){
		if(tHwType == btPowerSuply){
			if( tNum < 4 ){
			}
		}
	}else
	if(aCmd == cmQueryVer){
		if(tHwType == btPowerSuply){
			if( tNum < 4 ){
				Psv.Exist[tNum] =1;
			}					
		}
	}	
}

void TRegister::GetEpDevCount()
{
	unsigned int aAddr;
	unsigned int i,m;
	int aPc, aCount;
	unsigned char aType;
	unsigned int aP0;
	//if(IsOnNetwork == 0){
	if(1){
		if(RegWhat == 0){
			//Show Data
			Count.Reset();
			aAddr = dAddrSdRam_Register;
			for(m=0; m<dPORT_MAX_COUNT; m++){
				if(TransmiteCompleted[m]){
					for(i=0; i<250; i++){
						aType = *(volatile unsigned char *)(aAddr + (m*256) + i);
						if(aType){
							Count.All++;
							if(  (aType == dgTYPE_SMOKE_DETECTOR)  ){
								     Count.Smoke++;
							}else
							if(  aType ==  dgTYPE_TEMPE_DETECTOR ){
								     Count.Tempe++;
							}else
							if(  (aType == dgTYPE_HAND_REPORT ) ){
								     Count.HandReport++;
							}else
							if(  (aType == dgTYPE_FIRE_HydrantKp ) ){
								     Count.HydKp++;
							}else
							if(  (aType == dgTYPE_IN_MODULE ) ){
								     Count.ModuleInput++;
							}else
							if(   (aType == dgTYPE_INOUT_MODULE_NonSource) ||
								(aType == dgTYPE_INOUT_MODULE_Source) ){
								     Count.ModuleInOut++;
							}else
							if(  (aType == dgTYPE_OUT_MODULE) ){
								     Count.ModuleOutput++;
							}else
							if(  (aType == dgType_VAALRAM )  ){
								     Count.Va++;
							}else
							if(  aType == dgTYPE_DISPLAY_PAD ){
								     Count.FDisplay++;
							}else
							if(  aType == dgTYPE_SMOKE_TEMPE_DETECTOR ){
								     Count.SmokeTempe++;
							}else
							if(  aType == dgTYPE_LINKAGE_POWERSUPLY){
									Count.LINKAGE_POWERSUPLY++;
							}else{
								Count.cOther++;
							}
						}
					}

				}
			}
		}else
		if(RegWhat){
			aP0 = RegWhat -1;
			if(aP0 >= dPORT_MAX_COUNT)return;
			Count.Reset();
			aAddr = dAddrSdRam_Register;
			if(TransmiteCompleted[aP0]){
				for(i=0; i<250; i++){
					aType = *(volatile unsigned char *)(aAddr + (aP0*256) + i);
					if(aType){
						Count.All++;
						if(  (aType == dgTYPE_SMOKE_DETECTOR)  ){
							     Count.Smoke++;
						}else
						if(  aType ==  dgTYPE_TEMPE_DETECTOR ){
							     Count.Tempe++;
						}else
						if(  (aType == dgTYPE_HAND_REPORT ) ){
							     Count.HandReport++;
						}else
						if(  (aType == dgTYPE_FIRE_HydrantKp ) ){
							     Count.HydKp++;
						}else
						if(  (aType == dgTYPE_IN_MODULE ) ){
							     Count.ModuleInput++;
						}else
						if(   (aType == dgTYPE_INOUT_MODULE_NonSource) ||
							(aType == dgTYPE_INOUT_MODULE_Source) ){
							     Count.ModuleInOut++;
						}else
						if(  (aType == dgTYPE_OUT_MODULE) ){
							     Count.ModuleOutput++;
						}else
						if(  (aType == dgType_VAALRAM )  ){
							     Count.Va++;
						}else
						if(  aType == dgTYPE_DISPLAY_PAD ){
							     Count.FDisplay++;
						}else
						if(  aType == dgTYPE_SMOKE_TEMPE_DETECTOR ){
							     Count.SmokeTempe++;
						}else
						if(  aType == dgTYPE_LINKAGE_POWERSUPLY){
								Count.LINKAGE_POWERSUPLY++;
						}else{
							Count.cOther++;
						}
					}
				}
			}
		}		
	}else{
		if(RegWhat == 0){
			
		}else
		if(RegWhat){
			
		}	
	}
}

int TRegister::CompCount()
{
	int tAll =0;
	tAll += Count.Smoke;
	tAll += Count.Tempe;
	tAll += Count.HandReport;
	tAll += Count.HydKp;
	tAll += Count.ModuleInput;
	tAll += Count.ModuleInOut;
	tAll += Count.ModuleOutput;
	tAll += Count.Va;
	tAll += Count.FDisplay;
	tAll += Count.SmokeTempe;
	tAll += Count.LINKAGE_POWERSUPLY;
	tAll += Count.cOther;
	
	if(Count.All == tAll){
		return 1;
	}else{
		return 0;
	}
}

void TRegister::LoadToMainCtl()
{
	unsigned int aAddr;
	unsigned int i,m;
	int aPc, aCount;
	unsigned char aType;
	unsigned int aP0;
	if( (IsOnNetwork == 0) || (IsOnNetwork == 2) ){
		if(RegWhat == 0){
			if(IsOverWrite){
				for(m=0; m<dPORT_MAX_COUNT; m++){
					for(i=0; i<256; i++){
						Port[m].ExistTable[i] = 0;
						Port[m].dTypeTable[i] = 0;
						//EpDev[m][i].fData.Split.dType = 0;
						//EpDev[m][i].fData.Split.Id = 0;
					}
					MainCtl.fData.Split.ExistTablePort[m] = 0;
				}
				MainCtl.fData.Split.PortCount = 0;
			}

			for(m=0; m<dPORT_MAX_COUNT; m++){
				if(TransmiteCompleted[m]){
					MainCtl.fData.Split.ExistTablePort[m] = 0xA5;
					Port[m].fData.Port.dType = dgTYPE_PORT;
					Port[m].fData.Port.Id = m+1;
					aAddr = dAddrSdRam_Register;
					
					for(i=0; i<250; i++){
						aType = *(volatile unsigned char *)(aAddr + (m*256) + i);
						if(aType){
							Port[m].ExistTable[i] = 0xA5;
							Port[m].dTypeTable[i] = aType;
							//EpDev[m][i].fData.Split.dType = aType;
							//EpDev[m][i].fData.Split.Id = i+1;
						}
					}
					if(IsOverWrite){
						Port[m].fData.Port.ResistedCount = aCount;
					}else{
						
					}
				}
			}
			
			aPc = 0;
			for(m=0; m<dPORT_MAX_COUNT; m++){
				aCount = 0;
				for(i=0; i<250; i++){
					if(Port[m].ExistTable[i] && (Port[m].dTypeTable[i] != 0)){
						aCount++;
					}
				}
				Port[m].fData.Port.ResistedCount = aCount;
				
				if(MainCtl.fData.Split.ExistTablePort[m]){
					aPc ++;
				}
			}
			MainCtl.fData.Split.PortCount = aPc;
		}else
		if(RegWhat){
			aP0 = RegWhat -1;
			if(aP0 >= dPORT_MAX_COUNT)return;
			if(IsOverWrite){
				for(i=0; i<256; i++){
					Port[aP0].ExistTable[i] = 0;
					Port[aP0].dTypeTable[i] = 0;
					//EpDev[aP0][i].fData.Split.dType = 0;
					//EpDev[aP0][i].fData.Split.Id = 0;
				}
				MainCtl.fData.Split.ExistTablePort[aP0] = 0;
			}

			if(TransmiteCompleted[aP0]){
				MainCtl.fData.Split.ExistTablePort[aP0] = 0xA5;
				Port[aP0].fData.Port.dType = dgTYPE_PORT;
				Port[aP0].fData.Port.Id = aP0+1;
				aAddr = dAddrSdRam_Register;
				
				for(i=0; i<250; i++){
					aType = *(volatile unsigned char *)(aAddr + (aP0*256) + i);
					if(aType){
						Port[aP0].ExistTable[i] = 0xA5;
						Port[aP0].dTypeTable[i] = aType;
						//EpDev[aP0][i].fData.Split.dType = aType;
						//EpDev[aP0][i].fData.Split.Id = i+1;
					}
				}
				if(IsOverWrite){
					Port[aP0].fData.Port.ResistedCount = aCount;
				}else{
					
				}
			}

			
			aPc = 0;
			for(m=0; m<dPORT_MAX_COUNT; m++){
				aCount = 0;
				for(i=0; i<250; i++){
					if(Port[m].ExistTable[i] && (Port[m].dTypeTable[i] != 0)){
						aCount++;
					}
				}
				Port[m].fData.Port.ResistedCount = aCount;
				
				if(MainCtl.fData.Split.ExistTablePort[m]){
					aPc ++;
				}
			}
			MainCtl.fData.Split.PortCount = aPc;		
		}
	}else{
		if(RegWhat == 0){
			
		}else
		if(RegWhat){
			
		}
	}
}

void TRegister::sWriteOnePort(unsigned char aPort)
{
	unsigned int i,m;
	volatile unsigned int aFlashState, fAddr;
	TDMix tDm;
	if(sIList==nullptr)return;
	if(aPort==0)return;
	if(aPort>dPORT_MAX_COUNT)return;
	i=aPort-1;
	
	sIList->ReadWriteData.Reset();
	LoopCheck1S();
	for(m=0; m<dEP_MAX_COUNT_PER_PORT; m++){
		sIList->ReadWriteData.D8[m] = Port[i].ExistTable[m];
	}
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * i) + (256 * 0);
	nor_write_page(0, fAddr, sIList->ReadWriteData.D8);
	do{
		sDelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	
	LoopCheck1S();
	//write dType
	for(m=0; m<dEP_MAX_COUNT_PER_PORT; m++){
		sIList->ReadWriteData.D8[m] = Port[i].dTypeTable[m];
	}
	Crc32Reset();
	for(m=0; m<dEP_MAX_COUNT_PER_PORT; m++)Crc32Feed(static_cast<unsigned int>(Port[i].ExistTable[m]));
	for(m=0; m<dEP_MAX_COUNT_PER_PORT; m++)Crc32Feed(static_cast<unsigned int>(Port[i].dTypeTable[m]));
	tDm.D32 = Crc32GetResult();
	sIList->ReadWriteData.D8[252] = tDm.D8[0];
	sIList->ReadWriteData.D8[253] = tDm.D8[1];
	sIList->ReadWriteData.D8[254] = tDm.D8[2];
	sIList->ReadWriteData.D8[255] = tDm.D8[3];
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * i) + (256 * 1);
	nor_write_page(0, fAddr, sIList->ReadWriteData.D8);
	do{
		sDelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	
	LoopCheck1S();
	//write DisplaySetting
	Port[i].SortDisplayPad();
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * i) + (256 * 2);
	nor_write_byte_via_page(0, fAddr, &Port[i].DisplayPadSet[0].MemBytes[0], 256);
	do{
		sDelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	LoopCheck1S();
	Crc32Reset();
	for(m=0; m<16; m++){
		for(int x=0; x<4; x++)Crc32Feed(Port[i].DisplayPadSet[m].MemD32[x]);
	}
	tDm.D32 = Crc32GetResult();
	sIList->ReadWriteData.D8[0] = tDm.D8[0];
	sIList->ReadWriteData.D8[1] = tDm.D8[1];
	sIList->ReadWriteData.D8[2] = tDm.D8[2];
	sIList->ReadWriteData.D8[3] = tDm.D8[3];
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * i) + (256 * 3);
	nor_write_byte_via_page(0, fAddr, sIList->ReadWriteData.D8, 256);
	do{
		sDelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
}

void TRegister::sWritePortList()
{
	int m;
	volatile unsigned int fAddr, aFlashState;
	TDMix tDm;
	if(sIList==nullptr)return;
	sIList->ReadWriteData.Reset();
	
	Crc32Reset();
	for(m=0; m<dPORT_MAX_COUNT; m++){
		if(MainCtl.fData.Split.ExistTablePort[m]){
			sIList->ReadWriteData.D8[m] = 0xA5;
		}
		Crc32Feed(static_cast<unsigned int>(sIList->ReadWriteData.D8[m]));
	}
	tDm.D32 = Crc32GetResult();
	sIList->ReadWriteData.D8[dPORT_MAX_COUNT +0] = tDm.D8[0];
	sIList->ReadWriteData.D8[dPORT_MAX_COUNT +1] = tDm.D8[1];
	sIList->ReadWriteData.D8[dPORT_MAX_COUNT +2] = tDm.D8[2];
	sIList->ReadWriteData.D8[dPORT_MAX_COUNT +3] = tDm.D8[3];
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * dPORT_MAX_COUNT ) + (256 * 0);
	nor_write_page(0, fAddr, sIList->ReadWriteData.D8);
	do{
		sDelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
}

void TRegister::SaveToFlash()
{
	unsigned int i,m;
	volatile unsigned int aFlashState;
	TDMix tDm;
	if( (IsOnNetwork != 0) && (IsOnNetwork != 2) )return;
	
	//Save Power Supply Inf
	if(RegWhat == 0){
		if(Psv.Exist[0]){
			//Record.WritePowerSuplyCount(Psv.Exist[1], Psv.Exist[2]);
			//Record.ReadPowerSuplyCount();
		}
	}
	
	do{
		sDelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	for(i=0; i<8; i++){
		fAddr = dFlashRegisterDataAddr + (0x000010000 * i);
		nor_erase_sector_64k(0,fAddr);
		sDelayMs(250);
		do{
			sDelayMs(30);
			aFlashState = nor_get_busy_state(0);
		}while(aFlashState);
	}

	for(i=0; i<dPORT_MAX_COUNT; i++){
		//write ExistTable 
		if( (IsOnNetwork == 0) || (IsOnNetwork == 2) ){
			if(LanguageEnCn==0)TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, 	"注册完成,正在写入");
			else TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, 			"Completed,Writing");
		}
		sIList = IList;
		sWriteOnePort(i+1);
	}

	//Main Ctl Inf
	//Write Port Exist Table
	if( (IsOnNetwork == 0) || (IsOnNetwork == 2) ){
		if(LanguageEnCn==0)TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, 	"注册完成,写入主机");
		else TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, 			"Completed,Writing");
	}
	sIList = IList;
	sWritePortList();
	
	Record.ReadRegisterData();
}

void TRegister::MasterCanNetGetProgressVal(unsigned char aVal)
{
	Register.Nw.ProgressVal = aVal;
	Register.Nw.ProgressHasUpdate =1;
}

void TRegister::MasterCanNetGetPortCount(unsigned char aCnt)
{
	Register.Nw.PortCount = aCnt;
	Register.Nw.PortCountHasUpdate =1;
}

void TRegister::MasterCanNetGetEpCount(unsigned char aDType, unsigned char LowByte, unsigned char HiByte)
{
	unsigned short tCnt;
	
	tCnt = HiByte;
	tCnt <<= 8;
	tCnt |= LowByte;
	if(  (aDType == dgTYPE_SMOKE_DETECTOR)  ){
		     Count.Smoke = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if(  aDType ==  dgTYPE_TEMPE_DETECTOR ){
		     Count.Tempe = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if(  (aDType == dgTYPE_HAND_REPORT ) ){
		     Count.HandReport = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if(  (aDType == dgTYPE_FIRE_HydrantKp ) ){
		     Count.HydKp = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if(  (aDType == dgTYPE_IN_MODULE ) ){
		     Count.ModuleInput = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if(   (aDType == dgTYPE_INOUT_MODULE_NonSource) ||
		(aDType == dgTYPE_INOUT_MODULE_Source) ){
		     Count.ModuleInOut = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if(  (aDType == dgTYPE_OUT_MODULE) ){
		     Count.ModuleOutput = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if(  (aDType == dgType_VAALRAM )  ){
		     Count.Va = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if(  aDType == dgTYPE_DISPLAY_PAD ){
		     Count.FDisplay = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if(  aDType == dgTYPE_SMOKE_TEMPE_DETECTOR ){
		     Count.SmokeTempe = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if(  aDType == dgTYPE_LINKAGE_POWERSUPLY){
			Count.LINKAGE_POWERSUPLY = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if(  aDType == 0xFE){
			Count.cOther = tCnt;
			Nw.EpCountHasUpdate =1;
	}else
	if( aDType == 0xFF){
			Count.All = tCnt;
			Nw.EpCountHasUpdate =1;
	}
}

void TRegister::MasterCanNetGetEpCountFinished()
{
	Nw.HadTransited2Master = 1;
}

void TRegister::MasterCanNetShowTryStart()
{
	//RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	//TextRender_string24(Lv.Panel.Left, Lv.Panel.Top + 4 ,clNearWhite, PanelColor, 		"正在等待区域机回复     ");
}

void TRegister::MasterCanNetShowStart()
{
	if(Nw.RegWhat == 0){
		if(LanguageEnCn==0)TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, PanelColor, 	"区域机正在执行注册,全部回路 ");
		else TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, PanelColor, 				"Slaver Registering,All Loops"); 
	}else
	if(Nw.RegWhat <= dPORT_MAX_COUNT){
		if(LanguageEnCn==0)TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, PanelColor, 	"区域机正在执行注册,单个回路");
		else TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, PanelColor, 				"Slaver Registering,Single  ");
	}
}

void TRegister::MasterCanNetShowProgress()
{
	int x,y;
	x = Lv.PanelSmall.Left; y = Lv.PanelSmall.Top +6;
	TProgressBar::sDrawBySize(x,y,Lv.PanelSmall.Width-100, 32,Nw.ProgressVal,100,dclSearch,clGray,clBlack);
	y += 4;
	x = Lv.PanelSmall.Right - 80;
	TextDigitRender3Right24(x,y ,clNearWhite, ContentColor, Nw.ProgressVal);
	x = Lv.PanelSmall.Right - 80 + 36;
	TextRender_string24(x, y ,clNearWhite, "%");
}

void TRegister::MasterCanNetShowItems()
{
	if(LanguageEnCn==0)TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 	"回路数量");
	else TextRender_string24(Lv.Panel.Left+6, Lv.Panel.Top + 4, clNearWhite, PanelColor, 			"Loop(s) ");
	TextDigitRender2Right24(Lv.Panel.Left+ 104, Lv.Panel.Top + 4, clNearWhite, PanelColor, Nw.PortCount);
}

void TRegister::MasterCanNetSendStartSeq(unsigned char aCtlNum, unsigned char aPath)
{
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Identification =0;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_DevRegStartSeq;
	ProtoC2C.TxMsg.Data.Data[3] = aCtlNum;
	for(int i=0; i<4; i++){
		ProtoC2C.TxMsg.Data.Data[0] = i;
		ProtoC2C.TxMsg.Data.Data[4] = dNetDevRegCmdSeq[i][0];
		ProtoC2C.TxMsg.Data.Data[5] = dNetDevRegCmdSeq[i][1];
		ProtoC2C.TxMsg.Data.Data[6] = dNetDevRegCmdSeq[i][2];
		if(i<3)
			ProtoC2C.TxMsg.Data.Data[7] = dNetDevRegCmdSeq[i][3];
		else
			ProtoC2C.TxMsg.Data.Data[7] = aPath;
		ProtoC2C.oBufInsert();
	}
}

void TRegister::MasterStartByRemote(unsigned char aCtlNum, unsigned char aPath)
{
	Nw.Reset();
	Count.Reset();
	Nw.CtlNum = aCtlNum;
	RegWhat = aPath;
	
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	int x,y;
	x = Lv.PanelSmall.Left; y = Lv.PanelSmall.Top +6;
	TProgressBar::sDrawBySize(x,y,Lv.PanelSmall.Width-100, 32,0,100,dclSearch,clGray,clBlack);
	y += 4;
	x = Lv.PanelSmall.Right - 80;
	TextDigitRender3Right24(x,y ,clNearWhite, ContentColor, 0);
	x = Lv.PanelSmall.Right - 80 + 36;
	TextRender_string24(x, y ,clNearWhite, "%");
	TextRender_string24(Lv.PanelSmall.Left, Lv.PanelSmall.Top + 46 ,clNearWhite, ContentColor, "      ");
	
	Count.Reset();
	if(aPath == 0){
		//Local All
		IsOnNetwork = 1;
		IsTimeOutReturn = 0;
		WastTimeSec = 0;
		if(1){
			IsOverWrite = 1;
		}else{
			IsOverWrite = 0;
		}
		MasterCanNetSendStartSeq(Nw.CtlNum, RegWhat);
		IsOnGo = 1;
		HasDone = 0;
		ResetHasSend = 0;
		RdyToShowTick = 0;
		DelayCheckTick = 0;
		IsDevRegisting = 0x00A5A5A5;
		MasterCanNetShowTryStart();
		Record.OperateAdd_Registe(3);
		
		for(int i=0;i<4;i++)Psv.Exist[i] =0;
	}else
	if(aPath){
		//Local One Port
		IsOnNetwork = 1;
		IsTimeOutReturn = 0;
		WastTimeSec = 0;
		if(1){
			IsOverWrite = 1;
		}else{
			IsOverWrite = 0;
		}
		MasterCanNetSendStartSeq(Nw.CtlNum, RegWhat);
		IsOnGo = 1;
		HasDone = 0;
		ResetHasSend = 0;
		RdyToShowTick = 0;
		DelayCheckTick = 0;
		IsDevRegisting = 0x00A5A5A5;
		MasterCanNetShowTryStart();
		Record.OperateAdd_Registe(3);
		
		Nw.HasDone = 0;
	}	
}

unsigned char TRegister::SlaveGetStartSeq(unsigned char *pData)
{
	if(pData[3] == MainCtl.fData.Split.MyNum){
		if( pData[0] == 0 ){
			if( (pData[4] == dNetDevRegCmdSeq[0][0]) && (pData[5] == dNetDevRegCmdSeq[0][1]) && (pData[6] == dNetDevRegCmdSeq[0][2]) && (pData[7] == dNetDevRegCmdSeq[0][3]) ){
				Nw.StartSeqIdf = 0x01;
			}
		}else
		if( pData[0] == 1 ){
			if( (pData[4] == dNetDevRegCmdSeq[1][0]) && (pData[5] == dNetDevRegCmdSeq[1][1]) && (pData[6] == dNetDevRegCmdSeq[1][2]) && (pData[7] == dNetDevRegCmdSeq[1][3])  ){
				if(Nw.StartSeqIdf == 1){
					Nw.StartSeqIdf |= 0x02;
				}
			}
		}else
		if( pData[0] == 2 ){
			if( (pData[4] == dNetDevRegCmdSeq[2][0]) && (pData[5] == dNetDevRegCmdSeq[2][1]) && (pData[6] == dNetDevRegCmdSeq[2][2]) && (pData[7] == dNetDevRegCmdSeq[2][3])  ){
				if(Nw.StartSeqIdf == 3){
					Nw.StartSeqIdf |= 0x04;
				}
			}
		}else
		if( pData[0] == 3 ){
			if( (pData[4] == dNetDevRegCmdSeq[3][0]) && (pData[5] == dNetDevRegCmdSeq[3][1]) && (pData[6] == dNetDevRegCmdSeq[3][2]) ){
				if(Nw.StartSeqIdf == 7){
					if(pData[7] < dPORT_MAX_COUNT){
						if(CheckIsRemoteRegFeasible()){
							//Start here
							return pData[7];
						}else{
							return 254;
						}
					}else{
						return 254;
					}
				}else{
					return 254;
				}
			}
		}
	}
	return 255;
}

void TRegister::SlaveRegInit(unsigned char aPath)
{
	if(aPath == 0){
		//Local All
		IsOnNetwork = 2;
		InitDataBuf();
		ClearProgressVal();
		Count.Reset();
		IsTimeOutReturn = 0;
		WastTimeSec = 0;
		TotalProgress = 0;
		QueryTimeOut = 0;
		IsTransmiting = 0;
		IsQuerying = 0;
		if(1){
			IsOverWrite = 1;
		}else{
			IsOverWrite = 0;
		}
		RegWhat = 0;
		SendCmdAll();
		IsOnGo = 1;
		HasDone = 0;
		ResetHasSend = 0;
		RdyToShowTick = 0;
		DelayCheckTick = 0;
		IsDevRegisting = 0x00A5A5A5;
		Record.OperateAdd_Registe(4);
		
		for(int i=0;i<4;i++)Psv.Exist[i] =0;
	}else
	if(aPath){
		//Local One Port
		IsOnNetwork = 2;
		InitDataBuf();
		ClearProgressVal();
		Count.Reset();
		IsTimeOutReturn = 0;
		WastTimeSec = 0;
		TotalProgress = 0;
		QueryTimeOut = 0;
		IsTransmiting = 0;
		IsQuerying = 0;
		if(1){
			IsOverWrite = 1;
		}else{
			IsOverWrite = 0;
		}
		RegWhat = aPath;
		//SendCmdAll();
		SendCmdOne(RegWhat);
		IsOnGo = 1;
		HasDone = 0;
		ResetHasSend = 0;
		RdyToShowTick = 0;
		DelayCheckTick = 0;
		IsDevRegisting = 0x00A5A5A5;
		Record.OperateAdd_Registe(4);
	}
}


void TRegister::SlaveReplyStartState(unsigned char aSta)
{
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Order = 0;
	ProtoC2C.TxMsg.Data.Identification = 0;

	ProtoC2C.TxMsg.Data.Data[0] = 0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_DevRegRpyStart;
	ProtoC2C.TxMsg.Data.Data[3] = aSta;
	ProtoC2C.TxMsg.Data.Data[4] = 0;
	ProtoC2C.TxMsg.Data.Data[5] = 0;
	ProtoC2C.TxMsg.Data.Data[6] = 0;
	ProtoC2C.TxMsg.Data.Data[7] = 0;
	ProtoC2C.oBufInsert();
}

void TRegister::SlaveReplyProgress(unsigned char aProg)
{
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Order = 0;
	ProtoC2C.TxMsg.Data.Identification = 0;

	ProtoC2C.TxMsg.Data.Data[0] = 0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_DevRegRpyProgress;
	ProtoC2C.TxMsg.Data.Data[3] = aProg;
	ProtoC2C.TxMsg.Data.Data[4] = 0;
	ProtoC2C.TxMsg.Data.Data[5] = 0;
	ProtoC2C.TxMsg.Data.Data[6] = 0;
	ProtoC2C.TxMsg.Data.Data[7] = 0;
	ProtoC2C.oBufInsert();	
}

void TRegister::SlaveReplyEpCount(unsigned char aDType, unsigned short aCnt)
{
	unsigned char LowByte, HiByte;
	LowByte = static_cast<unsigned char>(aCnt & 0x00FF);
	HiByte = static_cast<unsigned char>(aCnt >> 8);
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Order = 0;
	ProtoC2C.TxMsg.Data.Identification = 0;

	ProtoC2C.TxMsg.Data.Data[0] = 0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_DevRegRpyEpCount;
	ProtoC2C.TxMsg.Data.Data[3] = aDType;
	ProtoC2C.TxMsg.Data.Data[4] = LowByte;
	ProtoC2C.TxMsg.Data.Data[5] = HiByte;
	ProtoC2C.TxMsg.Data.Data[6] = 0;
	ProtoC2C.TxMsg.Data.Data[7] = 0;
	ProtoC2C.oBufInsert();	
}


void TRegister::SlaveReplyPortCount(unsigned char aCnt)
{
	unsigned char LowByte, HiByte;
	LowByte = static_cast<unsigned char>(aCnt & 0x00FF);
	HiByte = static_cast<unsigned char>(aCnt >> 8);
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Order = 0;
	ProtoC2C.TxMsg.Data.Identification = 0;

	ProtoC2C.TxMsg.Data.Data[0] = 0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_DevRegRpyPortCnt;
	ProtoC2C.TxMsg.Data.Data[3] = aCnt;
	ProtoC2C.TxMsg.Data.Data[4] = 0;
	ProtoC2C.TxMsg.Data.Data[5] = 0;
	ProtoC2C.TxMsg.Data.Data[6] = 0;
	ProtoC2C.TxMsg.Data.Data[7] = 0;
	ProtoC2C.oBufInsert();	
}

void TRegister::SlaveReplyEpCountTransmited()
{
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Order = 0;
	ProtoC2C.TxMsg.Data.Identification = 0;

	ProtoC2C.TxMsg.Data.Data[0] = 0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_DevRegRpyEpCompleted;
	ProtoC2C.TxMsg.Data.Data[3] = 0;
	ProtoC2C.TxMsg.Data.Data[4] = 0;
	ProtoC2C.TxMsg.Data.Data[5] = 0;
	ProtoC2C.TxMsg.Data.Data[6] = 0;
	ProtoC2C.TxMsg.Data.Data[7] = 0;
	ProtoC2C.oBufInsert();	
}

void TRegister::MasterSendCmdReqEpCount()
{
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Order = 0;
	ProtoC2C.TxMsg.Data.Identification = 0;

	ProtoC2C.TxMsg.Data.Data[0] = 0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_DevRegReqEpCount;
	ProtoC2C.TxMsg.Data.Data[3] = Nw.CtlNum;
	ProtoC2C.TxMsg.Data.Data[4] = 0;
	ProtoC2C.TxMsg.Data.Data[5] = 0;
	ProtoC2C.TxMsg.Data.Data[6] = 0;
	ProtoC2C.TxMsg.Data.Data[7] = 0;
	ProtoC2C.oBufInsert();	
}

void TRegister::MasterSendCmdEpCountCorrect()
{
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Order = 0;
	ProtoC2C.TxMsg.Data.Identification = 0;

	ProtoC2C.TxMsg.Data.Data[0] = 0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_DevRegEpCountCorrect;
	ProtoC2C.TxMsg.Data.Data[3] = Nw.CtlNum;
	ProtoC2C.TxMsg.Data.Data[4] = 0;
	ProtoC2C.TxMsg.Data.Data[5] = 0;
	ProtoC2C.TxMsg.Data.Data[6] = 0;
	ProtoC2C.TxMsg.Data.Data[7] = 0;
	ProtoC2C.oBufInsert();	
}

void TRegister::SlaveReplyAllEpCount()
{
	if(Count.Smoke >0)			SlaveReplyEpCount(dgTYPE_SMOKE_DETECTOR, 		Count.Smoke);
	if(Count.Tempe >0)			SlaveReplyEpCount(dgTYPE_TEMPE_DETECTOR, 		Count.Tempe);
	if(Count.HandReport >0)			SlaveReplyEpCount(dgTYPE_HAND_REPORT, 		Count.HandReport);
	if(Count.HydKp >0)			SlaveReplyEpCount(dgTYPE_FIRE_HydrantKp, 		Count.HydKp);
	if(Count.ModuleInput >0)		SlaveReplyEpCount(dgTYPE_IN_MODULE, 		Count.ModuleInput);
	if(Count.ModuleInOut >0)		SlaveReplyEpCount(dgTYPE_INOUT_MODULE_Source, 	Count.ModuleInOut);
	if(Count.ModuleOutput >0)		SlaveReplyEpCount(dgTYPE_OUT_MODULE, 		Count.ModuleOutput);
	if(Count.Va >0)				SlaveReplyEpCount(dgType_VAALRAM, 			Count.Va);
	if(Count.FDisplay >0)			SlaveReplyEpCount(dgTYPE_DISPLAY_PAD, 		Count.FDisplay);
	if(Count.SmokeTempe >0)			SlaveReplyEpCount(dgTYPE_SMOKE_TEMPE_DETECTOR, 	Count.SmokeTempe);
	if(Count.LINKAGE_POWERSUPLY > 0)	SlaveReplyEpCount(dgTYPE_LINKAGE_POWERSUPLY, 	Count.LINKAGE_POWERSUPLY);
	if(Count.cOther >0)			SlaveReplyEpCount(0xFE, 				Count.cOther);
	
	SlaveReplyEpCount(0xFF, 				Count.All);
	
	SlaveReplyEpCountTransmited();
}

void TRegister::SlaveRpyEpCount()
{
	if((Register.IsOnGo) && (Register.IsOnNetwork == 2)){
		SlaveReplyAllEpCount();
	}
}

void TRegister::SlaveRecMasterGotCompleted()
{
	if((Register.IsOnGo) && (Register.IsOnNetwork == 2)){
		Nw.Completed = 1;
	}
}

int TRegister::ExtRequst(unsigned char Prm)
{
	IsOnShow = 0;
	IsOnGo =0;
	SendCmdAbort();
	IsDevRegisting = 0;
	return 0;
}






















