#include "CanNetworkRegister.h"
#include "ProtoC2C.h"
#include "Runtime.h"
#include "server.h"

#define dDescpOffSet	0x1000


#define dAddrSdRam_TypeList		(dAddrSdRam_Commom)
#define dAddrSdRam_DescpGotList	(dAddrSdRam_Commom	+ 256)
#define dAddrSdRam_DescpContent	(dAddrSdRam_Commom	+ 1024)
#define dAddrSdRam_ExistTable		(dAddrSdRam_Commom	+ 0x80000)

#define dAddrSdRam_LinkMaster		(dAddrSdRam_Commom	+ 0x90000)

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

#define dWWidth	24
#define dWHeigh	24
#define dRoundR	7

const int xSingle = 160;
const int xSlave = 340;
const int xMaster = 474;

const int xPst0 = 20;
const int xPst1 = 202;
const int xPst2 = 384;
const int xPst3 = 566;

const int ListItemsX = 6;
const int ListItemsY = 6;

extern "C"{
	#include "user_norflash.h"
	#include "user_eeprom.h"
	#include "uart_key_drv.h"
}

void TCanNetworkRegister::Init()
{
	IsShowing = 0;
	IsOnSearching = 0;
	LoadingIndex = 0;
	CtlIndex = 0;
	ListX =0;
	ListY =0;
	ListWhat = TCanNetworkRegister::TListWhat::Idle;
	Done = 0;
	GroupSelected = 0;
	
	IsPermission =0;
	
	this->MyNum = MainCtl.fData.Split.MyNum;
	
	SearchStartNum = 0;
	SearchEndNum = 0;
}

void TCanNetworkRegister::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 TCanNetworkRegister::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

void TCanNetworkRegister::RenderBackGround(unsigned int aClr)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, aClr);
}

void DrawIdfText()
{
	
}

void TCanNetworkRegister::DrawFixText()
{
	unsigned int tColor;
	if(MainCtl.fData.Split.NetworkMode == 1)tColor = clNearBlack; else tColor=clSilver; 
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "局域网->本机模式与注册");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "本机号:");
		TextRender_string24(Lv.Grid.Left[0] + 146, Lv.Grid.Top[0] +6, clNearWhite, "选中后按确定键保存");
	
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "组网模式:");
		TextRender_string24(xSingle+30, Lv.Grid.Top[1] + 6, clNearWhite, "单机");
		TextRender_string24(xSlave+30, Lv.Grid.Top[1] + 6, clNearWhite, "区域机");
		TextRender_string24(xMaster+30, Lv.Grid.Top[1] + 6, clNearWhite, "集中机");
	
		//TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "网络注册与清除");
		//TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +40, clNearWhite, "机器号");
		
		TRoundRect::sDrawBySize(xPst0, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "搜索在线机器", 14, clNearBlack);
		TRoundRect::sDrawBySize(xPst1, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "清集中机列表", 14, clNearBlack);
		TRoundRect::sDrawBySize(xPst2, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "清区域机列表", 14, clNearBlack);
		
		TRoundRect::sDrawBySize(xPst0, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, "查集中机列表", 14, tColor);
		TRoundRect::sDrawBySize(xPst1, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, "查区域机列表", 14, tColor);
		TRoundRect::sDrawBySize(xPst2, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, "存集中机列表", 14, tColor);
		TRoundRect::sDrawBySize(xPst3, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, "存区域机列表", 14, tColor);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Local Network->Mode Setting & Register");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "CtrlID:");
		TextRender_string24(Lv.Grid.Left[0] + 146, Lv.Grid.Top[0] +6, clNearWhite, "Selected Then Press OK to Save");
	
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "Network Mode");
		TextRender_string24(xSingle+30, Lv.Grid.Top[1] + 6, clNearWhite, "Stand Alone");
		TextRender_string24(xSlave+30, Lv.Grid.Top[1] + 6, clNearWhite, "Regional");
		TextRender_string24(xMaster+30, Lv.Grid.Top[1] + 6, clNearWhite, "Central");
	
		//TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "Network Register,Clear");
		//TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +40, clNearWhite, "Ctl ID");
		
		TRoundRect::sDrawBySize(xPst0, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "Search Online", 8, clNearBlack);
		TRoundRect::sDrawBySize(xPst1, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "Clear Masters", 8, clNearBlack);
		TRoundRect::sDrawBySize(xPst2, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "Clear Slavers", 8, clNearBlack);
		
		TRoundRect::sDrawBySize(xPst0, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, " Master List ", 8, tColor);
		TRoundRect::sDrawBySize(xPst1, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, " Slaver List ", 8, tColor);
		TRoundRect::sDrawBySize(xPst2, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, " Save Masters", 8, tColor);
		TRoundRect::sDrawBySize(xPst3, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, " Save Slavers", 8, tColor);		
	}
}

void TCanNetworkRegister::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);


	IList->Edit[0].Init(Lv.Grid.Left[0] + 100, Lv.Grid.Top[0] +2, 36, 32, 0,0, 1, clNearWhite);
	IList->Edit[0].SetMaxLen(2);
	IList->Edit[0].TextClear();
	IList->Edit[0].SetPstTail();

	TextRender_string24(Lv.Grid.Left[2] + 124, Lv.Grid.Top[2] +40, clNearWhite, "-");
	
	IList->EditCtlNum.Init(Lv.Grid.Left[2] + 84, Lv.Grid.Top[2] +36, 36, 32, 0,0, 1, clNearWhite);
	IList->EditCtlNum.SetMaxLen(2);
	IList->EditCtlNum.TextClear();
	IList->EditCtlNum.SetPstTail();
	IList->EditAddrNum.Init(Lv.Grid.Left[2] + 136, Lv.Grid.Top[2] +36, 36, 32, 0,0, 1, clNearWhite);
	IList->EditAddrNum.SetMaxLen(2);
	IList->EditAddrNum.TextClear();
	IList->EditAddrNum.SetPstTail();
	
	
	//IList->EditCtlNum.Show();
	//IList->EditAddrNum.Show();

	DrawFixText();
	
	IList->Edit[0].Str.FromUInt2Dg(this->MyNum);
	if( (MyNum) && (MyNum <65) ){
		MyNumValid = 1;
		IList->Edit[0].SelectedColor = clBlue;
	}else{
		MyNumValid = 0;
		IList->Edit[0].SelectedColor = clRed;
	}
	IList->Edit[0].SetPstTail();
	IList->Edit[0].Show();
	
	Start2EndNumValid = 1;
}

void TCanNetworkRegister::Show(void)
{	
	IsShowing = 1;
	RenderBackGround();
	DrawSelf();
	DrawNetworkMode();
	Check4Selected();
}

void TCanNetworkRegister::DrawCtlNum()
{
	IList->Edit[0].Str.FromUInt2Dg(MainCtl.fData.Split.MyNum);
	IList->Edit[0].Show();
}

void TCanNetworkRegister::DrawNetworkMode()
{
	if(MainCtl.fData.Split.NetworkMode == 1){
		TRoundSquare::sDrawBySize(xSingle, Lv.Grid.Top[1] + 8, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(xSlave,  Lv.Grid.Top[1] + 8, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(xMaster, Lv.Grid.Top[1] + 8, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else
	if(MainCtl.fData.Split.NetworkMode == 2){
		TRoundSquare::sDrawBySize(xSingle, Lv.Grid.Top[1] + 8, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(xSlave,  Lv.Grid.Top[1] + 8, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(xMaster, Lv.Grid.Top[1] + 8, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}else{
		TRoundSquare::sDrawBySize(xSingle, Lv.Grid.Top[1] + 8, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(xSlave,  Lv.Grid.Top[1] + 8, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(xMaster, Lv.Grid.Top[1] + 8, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
}

void TCanNetworkRegister::RedrawList()
{
	if(ListWhat != TCanNetworkRegister::TListWhat::Idle){
		DrawListTitle();
		if(ListWhat == TCanNetworkRegister::TListWhat::Searching){
			DrawSearchingList(ListDrewIndex);
		}else
		if(ListWhat == TCanNetworkRegister::TListWhat::QueMaster){
			DrawMasterList();
		}else
		if(ListWhat == TCanNetworkRegister::TListWhat::QueSlaver){
			DrawSlaverList();
		}
		DrawListSelected();
	}
}

void TCanNetworkRegister::FullRedraw(int Prm)
{	
	if(Prm == 0){
		IsShowing = 1;
		RenderBackGround();
		DrawSelf();
		//DrawListStatic();
	}else	
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		for(int i=0; i<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		TextRender_string24(Lv.Grid.Left[2] + 124, Lv.Grid.Top[2] +40, clNearWhite, "-");
		IList->EditCtlNum.Show();
		IList->EditAddrNum.Show();
		DrawFixText();
		IList->Edit[0].Show();
		DrawNetworkMode();
		RedrawList();
		if(IsPermission){
			PmsIntervene4Gui.ReDraw4GuiRecall();
		}else{
			Check4Selected();
		}
	}
}

//_____________________________________________________________________________________________________________________________
void TCanNetworkRegister::PermissionCmdReturnDraw()
{
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	Check4Selected();
	RedrawList();
}

void TCanNetworkRegister::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		//Go Register
		ClearSelected();
		ClearData();
		SearchStartNum = 1;
		SearchEndNum = dNETWORK_MACHINE_COUNT;
		if( (SearchStartNum) && (SearchEndNum) ){
			if( (SearchStartNum <=dNETWORK_MACHINE_COUNT) && (SearchEndNum <=dNETWORK_MACHINE_COUNT) && (SearchStartNum <= SearchEndNum) ){
				GoStartNum = SearchStartNum;
				GoEndNum = SearchEndNum;			
			}else{
				GoStartNum = 1;
				GoEndNum = dNETWORK_MACHINE_COUNT;
			}
		}else
		if( SearchStartNum || SearchEndNum){
			GoStartNum = 1;
			GoEndNum = dNETWORK_MACHINE_COUNT;
		}else{
			GoStartNum = 1;
			GoEndNum = dNETWORK_MACHINE_COUNT;
		}
		SearchingTick = 0;
		IsOnSearching = 1;
		ListWhat = TCanNetworkRegister::TListWhat::Searching;
		DrawListTitle();
	}else
	if(PermissionCmd == 2){
		//Clear Master
		int DoAction =0;
		MasterListCount = 0;
		for(int i=0; i<dNETWORK_MACHINE_MASTER_COUNT; i++)MasterList[i] =0;		
		if(MainCtl.fData.Split.NetworkMode == 1){
			if(DoAction){
				Record.SaveNwM2MCfg(MasterListCount, MasterList);
				Record.ReadNwM2MCfg();
				Record.OperateAdd_RegCanNetwork(3, MainCtl.fData.Split.MyNum, MasterListCount);
				unsigned int Failed = 0;
				if(ProtoC2C.M2MList.Cnt != MasterListCount){
					Failed=1;
				}
				for(int i=0; (i<ProtoC2C.M2MList.Cnt) and (i<dNETWORK_MACHINE_MASTER_COUNT); i++){
					if(ProtoC2C.M2MList.Items[i] != MasterList[i]){
						Failed=1;
						break;
					}
				}
				if( Failed == 0 ){
					if(LanguageEnCn==0)TextRender_string24	(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"清除成功        ");
					else TextRender_string24			(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"Cleared         ");
					SendClearMasterList(MasterListCount, MasterList);
				}else{
					if(LanguageEnCn==0)TextRender_string24	(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"清除失败        ");
					else TextRender_string24			(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor,	"Failed To Clear ");
				}
			}
		}
	}else
	if(PermissionCmd == 3){
		//Clear Slaver
		for(int i=0; i<dNETWORK_MACHINE_COUNT; i++)CtlList[i] =0;
		Record.SaveNwCfg(0, 0, CtlList);
		Record.ReadNwCfg();
		Record.OperateAdd_RegCanNetwork(1, MainCtl.fData.Split.MyNum, 0);
		if( (ProtoC2C.MasterNum == 0) && (0 == ProtoC2C.SlaveCount) ){
			if(LanguageEnCn==0)TextRender_string24	(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"清除成功        ");
			else TextRender_string24			(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"Cleared         ");
			if(MainCtl.fData.Split.NetworkMode == 1)SendNwList();
			ProtoC2C.SyncCheckOnline();
		}else{
			if(LanguageEnCn==0)TextRender_string24	(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"清除失败        ");
			else TextRender_string24			(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor,	"Failed To Clear ");
		}
	}else
	if(PermissionCmd == 4){
		//Save Master
		//In File Record.cpp; void SaveNwM2MCfg(unsigned char aCnt, unsigned char *aList);
		//In File Record.cpp; void ReadNwM2MCfg();
		int DoAction =0;
		MasterListCount = 0;
		for(int i=0; i<dNETWORK_MACHINE_MASTER_COUNT; i++)MasterList[i] =0;		
		if(MainCtl.fData.Split.NetworkMode == 1){
			if(ListWhat == Searching){
				for(int i=0; i<dNETWORK_MACHINE_COUNT; i++){
					if(MasterListCount>=dNETWORK_MACHINE_MASTER_COUNT)break;
					if(CtlSelectedList[i]){
						unsigned char tType = GetRecType(i+1);
						if(MainCtl.fData.Split.MyNum == (i+1))
							tType = dNetworkTypeMaster;
						if(tType == dNetworkTypeMaster){
							MasterList[MasterListCount]=i+1;
							MasterListCount++;
						}
					}
				}
				//Save And Send Master List Here
				DoAction =1;
			}else
			if(ListWhat == QueMaster){
				for(int i=0; i<dNETWORK_MACHINE_COUNT; i++){
					if(MasterListCount>=dNETWORK_MACHINE_MASTER_COUNT)break;
					if(CtlSelectedList[i]){
						MasterList[MasterListCount]=i+1;
						MasterListCount++;
					}
				}
				DoAction =2;
				//Save And Send Master List Here
			}
			if(DoAction){
				Record.SaveNwM2MCfg(MasterListCount, MasterList);
				SendMasterList(MasterListCount, MasterList);
				Record.ReadNwM2MCfg();
				Record.OperateAdd_RegCanNetwork(3, MainCtl.fData.Split.MyNum, MasterListCount);
				unsigned int Failed = 0;
				unsigned int tCnt =0;
				if(ProtoC2C.M2MList.Cnt != MasterListCount){
					Failed=1;
				}
				for(int i=0; i<ProtoC2C.M2MList.Cnt; i++){
					if(ProtoC2C.M2MList.Items[i] != MasterList[i]){
						Failed=1;
						break;
					}
				}
				if( Failed == 0 ){
					if(LanguageEnCn==0)TextRender_string24	(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"保存成功        ");
					else TextRender_string24			(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"Save Completed  ");
				}else{
					if(LanguageEnCn==0)TextRender_string24	(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"保存失败        ");
					else TextRender_string24			(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor,	"Failed To Save  ");
				}
			}
		}
	}else
	if(PermissionCmd == 5){
		//Save Slaver
		int DoAction =0;
		Count=0;
		if(MainCtl.fData.Split.NetworkMode == 1){
			for(int i=0; i<dNETWORK_MACHINE_COUNT; i++)CtlList[i] =0;
			if(ListWhat == Searching){
				for(int i=0; i<dNETWORK_MACHINE_COUNT; i++){
					if(CtlSelectedList[i]){
						unsigned char tType = GetRecType(i+1);
						if(tType == dNetworkTypeSlave){
							CtlList[Count]=i+1;
							Count++;
						}
					}
				}
				//Save And Send Slaver List Here
				DoAction =1;
			}else
			if(ListWhat == QueSlaver){
				for(int i=0; i<dNETWORK_MACHINE_COUNT; i++){
					if(CtlSelectedList[i]){
						CtlList[Count]=i+1;
						Count++;
					}
				}
				//Save And Send Slaver List Here
				DoAction =1;
			}
			if(DoAction){
				Record.SaveNwCfg(MainCtl.fData.Split.MyNum, Count, CtlList);
				SendNwList();
				Record.ReadNwCfg();
				Record.OperateAdd_RegCanNetwork(1, MainCtl.fData.Split.MyNum, Count);
				if( (ProtoC2C.MasterNum == MainCtl.fData.Split.MyNum) && (Count == ProtoC2C.SlaveCount) ){
					if(LanguageEnCn==0)TextRender_string24	(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"保存成功        ");
					else TextRender_string24			(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"Save Completed  ");
					SendNwList();
					ProtoC2C.SyncCheckOnline();
				}else{
					if(LanguageEnCn==0)TextRender_string24	(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"保存失败        ");
					else TextRender_string24			(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor,	"Failed To Save  ");
				}
			}
		}
	}else
	if(PermissionCmd == 11){
		Record.WriteMainSetMainCtl(0, MainCtl.fData.Split.MyNum);
		sReadNetMode();
		DrawFixText();
		DrawNetworkMode();
		ShowSaveMyNum(1);
	}else
	if(PermissionCmd == 12){
		Record.WriteMainSetMainCtl(2, MainCtl.fData.Split.MyNum);
		sReadNetMode();
		DrawFixText();
		DrawNetworkMode();
		ShowSaveMyNum(1);
	}else
	if(PermissionCmd == 13){
		Record.WriteMainSetMainCtl(1, MainCtl.fData.Split.MyNum);
		sReadNetMode();
		DrawFixText();
		DrawNetworkMode();
		ShowSaveMyNum(1);
	}else
	if(PermissionCmd == 21){
		Record.WriteMainSetMainCtl(MainCtl.fData.Split.NetworkMode, this->MyNum);
		sReadNetMode();
		this->MyNum = MainCtl.fData.Split.MyNum;
		IList->Edit[0].Str.FromUInt2Dg(this->MyNum);
		if( (MyNum) && (MyNum <65) ){
			MyNumValid = 1;
			IList->Edit[0].SelectedColor = clBlue;
		}else{
			MyNumValid = 0;
			IList->Edit[0].SelectedColor = clRed;
		}
		IList->Edit[0].SetPstTail();
		IList->Edit[0].Show();
		ShowSaveMyNum(1);
	}
}

void TCanNetworkRegister::ShowSaveMyNum(int aPrm)
{
	if(LanguageEnCn==0){
		if(aPrm){
			TextRender_string24(Lv.Grid.Left[0] + 700, Lv.Grid.Top[0] +6, clNearWhite, ItemColor, "已保存");
			JustShowingSaving =1;
			ShowingSavingMyNum =1;
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 700, Lv.Grid.Top[0] +6, clNearWhite, ItemColor, "      ");
			ShowingSavingMyNum =0;
		}
	}else{
		if(aPrm){
			TextRender_string24(Lv.Grid.Left[0] + 700, Lv.Grid.Top[0] +6, clNearWhite, ItemColor, "Saved ");
			JustShowingSaving =1;
			ShowingSavingMyNum =1;
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 700, Lv.Grid.Top[0] +6, clNearWhite, ItemColor, "      ");
			ShowingSavingMyNum =0;
		}
	}
}

void TCanNetworkRegister::sReadNetMode()
{
	unsigned int aAddr;
	int i;
	unsigned char tData[4];
	//Read ExistTable 
	aAddr = dEepAddrMainSet;
	sDelayMs(10);
	eeprom_read_byte(0, aAddr, tData, 2);
	
	MainCtl.fData.Split.MyNum = tData[0];
	MainCtl.fData.Split.NetworkMode = tData[1];
	
	if(  (MainCtl.fData.Split.MyNum == 0) || (MainCtl.fData.Split.MyNum > dNETWORK_MACHINE_COUNT) ) {
		MainCtl.fData.Split.MyNum =1;
		MainCtl.fData.Split.NetworkMode =0;
	}else{
		if(MainCtl.fData.Split.NetworkMode > 2)MainCtl.fData.Split.NetworkMode =0;
	}
	
}

void TCanNetworkRegister::ClearData()
{
	int i,x;
	unsigned int aAddr;
	//Clear
	for(i=0; i<64; i++){
		*(volatile unsigned char *)(dAddrSdRam_TypeList + i) = 0;
		*(volatile unsigned char *)(dAddrSdRam_DescpGotList + i) = 0;
		*(volatile unsigned char *)(dAddrSdRam_ExistTable +i) =0;
		*(volatile unsigned char *)(dAddrSdRam_LinkMaster + i) = 0;
	}
	for(i=0; i<64; i++){
		aAddr = dAddrSdRam_DescpContent  + i*32;
		for(x=0; x<32; x++){
			*(volatile unsigned char *)(aAddr + x) = 0;
		}
	}
	for(i=0; i<dNETWORK_MACHINE_COUNT; i++){
		CtlList[i]=0;
		CtlSelectedList[i] =0;
	}
	Count = 0;
	FoundIndex = 0;
	aNewGot = 0;
	SearchNumExist = 0;
	ListDrewIndex = 0;
	SearchingNum =1;
	SearchingTick =0;
	Done = 0;
}

int TCanNetworkRegister::CheckData(int SaveWhat)
{
	int i,x;
	unsigned int aAddr;
	unsigned char tType, CtlNum;
	SelectedList = 0;
	if(SaveWhat == 0){
		//Save Master List
		x=0;
		for(i=0; i<dNETWORK_MACHINE_COUNT; i++){
			CtlNum = i+1;
			if(CtlSelectedList[i]){
				aAddr = dAddrSdRam_TypeList + x;
				tType = *(volatile unsigned char *)(aAddr);
				if( (tType == dNetworkTypeMaster) && ( (CtlNum >ProtoC2C.CtlMask.EndNum) || (CtlNum < ProtoC2C.CtlMask.StartNum) )  ) {
					CtlList[x] = i;
					x++;
				}else{
					CtlSelectedList[i] =0;
					DrawSearchingItem(i+1);
				}
			}
		}
		SelectedList = x;
	}else{
		//Save Slaver List
		x=0;
		for(i=0; i<dNETWORK_MACHINE_COUNT; i++){
			if(CtlSelectedList[i]){
				aAddr = dAddrSdRam_TypeList + x;
				tType = *(volatile unsigned char *)(aAddr);
				if( (tType == dNetworkTypeSlave) && ( (CtlNum >ProtoC2C.CtlMask.EndNum) || (CtlNum < ProtoC2C.CtlMask.StartNum) )  ) {
					CtlList[x] = i;
					x++;
				}else{
					CtlSelectedList[i] =0;
					DrawSearchingItem(i+1);
				}
			}
		}
		SelectedList = x;
	}
	return SelectedList;
}

void TCanNetworkRegister::ShowSearchCompleted()
{
	int x, pNum;
	unsigned int aAddr;
	
	IncMaster = 0;
	for(x=0; x<Count; x++){
		aAddr = dAddrSdRam_ExistTable + x;
		pNum = *(volatile unsigned char *)(aAddr);
		aAddr = dAddrSdRam_TypeList + x;
		pNum = *(volatile unsigned char *)(aAddr);
		
		if(pNum == dNetworkTypeMaster){
			IncMaster =1;
		}
	}
	
	for(x=0; x<Count; x++){
		aAddr = dAddrSdRam_ExistTable + x;
		CtlList[x] = *(volatile unsigned char *)(aAddr);
	}
						
	if(LanguageEnCn==0){
		if(MainCtl.fData.Split.NetworkMode == 1){
			if(IncMaster == 0){
				TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, "搜索完成,正在保存");
			}else{
				TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, "搜索完成,正在保存");
			}
		}else{
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, "搜索完成,只有集中机可操作保存");
		}
	}else{
		if(MainCtl.fData.Split.NetworkMode == 1){
			if(IncMaster == 0){
				TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, "Scan Completed,Saving");
			}else{
				TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, "Scan Completed,Saving");
			}
		}else{
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, "Scan Completed, No Saving");
		}
	}
}

void TCanNetworkRegister::DrawListDeSelected()
{
	int x1, y1, x2, y2;
	x1 = Lv.Panel.Left + 6 + (ListX * 131 ) -2;
	y1 = Lv.Panel.Top +2 +31 + (ListY * 31  ) -2;
	x2 = x1 + 130;
	y2 = y1 + 28;
	VertLineRender(x1,	y1, 	y2-y1+1,	PanelColor);
	VertLineRender(x1-1,	y1, 	y2-y1+1,	PanelColor);
	VertLineRender(x2,	y1, 	y2-y1+1,	PanelColor);
	VertLineRender(x2+1,	y1, 	y2-y1+1,	PanelColor);
	HorizLineRender(x1,	y1, 	x2-x1-1,	PanelColor);
	HorizLineRender(x1,	y1-1, 	x2-x1-1,	PanelColor);
	HorizLineRender(x1,	y2, 	x2-x1-1,	PanelColor);
	HorizLineRender(x1,	y2+1, 	x2-x1-1,	PanelColor);
}

void TCanNetworkRegister::DrawListSelected()
{
	int x1, y1, x2, y2;
	x1 = Lv.Panel.Left + 6 + (ListX * 131 ) -2;
	y1 = Lv.Panel.Top +2 +31 + (ListY * 31  ) -2;
	x2 = x1 + 130;
	y2 = y1 + 28;
	VertLineRender(x1,	y1, 	y2-y1+1,	clBlue);
	VertLineRender(x1-1,	y1, 	y2-y1+1,	clBlue);
	VertLineRender(x2,	y1, 	y2-y1+1,	clBlue);
	VertLineRender(x2+1,	y1, 	y2-y1+1,	clBlue);
	HorizLineRender(x1,	y1, 	x2-x1-1,	clBlue);
	HorizLineRender(x1,	y1-1, 	x2-x1-1,	clBlue);
	HorizLineRender(x1,	y2, 	x2-x1-1,	clBlue);
	HorizLineRender(x1,	y2+1, 	x2-x1-1,	clBlue);	
}

void TCanNetworkRegister::DrawSearchingItem(int aInx)
{
	int  xPos, yPos, x,y;
	unsigned char aMcType, aLM;
	unsigned int aAddr;
	unsigned int Color, bColor;
	if(aInx==0)return;
	if(aInx>dNETWORK_MACHINE_COUNT)return;
	
	y = (aInx-1) / ListItemsX;
	x = (aInx-1) % ListItemsX;
	
	xPos = Lv.Panel.Left +6 + (x * 131);
	yPos = Lv.Panel.Top +2 + 31 + (y * 31);

	aAddr = dAddrSdRam_TypeList + aInx-1;
	aMcType = *(volatile unsigned char *)(aAddr);
	aAddr = dAddrSdRam_LinkMaster + aInx-1;
	aLM = *(volatile unsigned char *)(aAddr);
	TMyString::sClear(Text);
	TMyString::sFrom2Dg(aInx, Text);
	//TMyString::sAddOnChar(':', Text);
	if(aMcType == dNetworkTypeMaster){
		Color=clNearBlack;
		TMyString::sAddOnStr("F-M C", Text);
		TMyString::sAddOn2Dg(aLM % 100, Text);
	}else
	if(aMcType == dNetworkTypeSlave){
		if(aInx == MainCtl.fData.Split.MyNum){
			Color=clRed;
			TMyString::sAddOnStr("Dual   ", Text);
		}else{
			Color=clNearBlack;
			TMyString::sAddOnStr("F-S M", Text);
			
			if(aLM)
				TMyString::sAddOn2Dg(aLM % 100, Text);
			else 
				TMyString::sAddOnStr("--", Text);
		}
	}else
	if(aMcType == dNetworkTypeDual){
		Color=clRed;
		TMyString::sAddOnStr("Dual   ", Text);
	}else{
		if(aInx == MainCtl.fData.Split.MyNum){
			Color=clNearBlack;
			if(MainCtl.fData.Split.NetworkMode == 1){
				TMyString::sAddOnStr("Self C", Text);
				TMyString::sAddOn2Dg(ProtoC2C.SlaveCount % 100, Text);
			}else
			if(MainCtl.fData.Split.NetworkMode == 2){
				TMyString::sAddOnStr("Self M", Text);
				TMyString::sAddOn2Dg(ProtoC2C.MasterNum % 100, Text);
			}else{
				TMyString::sAddOnStr("Self   ", Text);
			}
		}else{
			Color=clSilver;
			TMyString::sAddOnStr("nil    ", Text);
		}
	}
	if(CtlSelectedList[aInx-1])bColor = clDeepGreen;
	else bColor = PanelColor;
	TextRender_string24(xPos, yPos, Color, bColor, Text);	
}

void TCanNetworkRegister::DrawMasterItem(int aInx)
{
	int xPos, yPos, x,y;
	unsigned char aMcType, aLM;
	unsigned int aAddr;
	unsigned int Color, bColor;
	
	if(aInx==0)return;
	if(aInx>dNETWORK_MACHINE_COUNT)return;

	y = (aInx-1) / ListItemsX;
	x = (aInx-1) % ListItemsX;
	
	xPos = Lv.Panel.Left +6 + (x * 131);
	yPos = Lv.Panel.Top +2 + 31 + (y * 31);

	aAddr = dAddrSdRam_TypeList + aInx-1;
	aMcType = *(volatile unsigned char *)(aAddr);
	TMyString::sClear(Text);
	TMyString::sFrom2Dg(aInx, Text);
	//TMyString::sAddOnChar(':', Text);
	if(aMcType == dNetworkTypeMaster){
		Color=clNearBlack;
		TMyString::sAddOnStr("F-M    ", Text);
	}else{
		Color=clSilver;
		TMyString::sAddOnStr("nil    ", Text);
	}
	if(CtlSelectedList[aInx-1])bColor = clDeepGreen;
	else bColor = PanelColor;
	TextRender_string24(xPos, yPos, Color, bColor, Text);	
}

void TCanNetworkRegister::DrawSlaverItem(int aInx)
{
	int xPos, yPos, x,y;
	unsigned char aMcType, aLM;
	unsigned int aAddr;
	unsigned int Color, bColor;
	
	if(aInx==0)return;
	if(aInx>dNETWORK_MACHINE_COUNT)return;

	y = (aInx-1) / ListItemsX;
	x = (aInx-1) % ListItemsX;
	
	xPos = Lv.Panel.Left +6 + (x * 131);
	yPos = Lv.Panel.Top +2 + 31 + (y * 31);

	aAddr = dAddrSdRam_TypeList + aInx-1;
	aMcType = *(volatile unsigned char *)(aAddr);
	TMyString::sClear(Text);
	TMyString::sFrom2Dg(aInx, Text);
	//TMyString::sAddOnChar(':', Text);
	if(aMcType == dNetworkTypeMaster){
		Color=clBlack;
		TMyString::sAddOnStr("FMaster", Text);
	}else
	if(aMcType == dNetworkTypeSlave){
		Color=clNavy;
		TMyString::sAddOnStr("F-S    ", Text);
	}else{
		Color=clSilver;
		TMyString::sAddOnStr("nil    ", Text);
	}
	if(CtlSelectedList[aInx-1])bColor = clDeepGreen;
	else bColor = PanelColor;
	TextRender_string24(xPos, yPos, Color, bColor, Text);		
}

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

	if(LanguageEnCn==0){
		TextRender_string24(Lv.Panel.Left +6, Lv.Panel.Top +2, clNearWhite, PanelColor, "F:火灾报警控制器   M:集中机   S:区域机");
	}else{
		TextRender_string24(Lv.Panel.Left +6, Lv.Panel.Top +2, clNearWhite, PanelColor, "F:Fire Alram Ctrl  M:Master   S:Slaver");
	}	
}

void TCanNetworkRegister::DrawSearchingList(int MaxInx)
{
	unsigned int i;	
	for(i=1; i<=MaxInx; i++){
		DrawSearchingItem(i);
	}
}

void TCanNetworkRegister::DrawMasterList(int MaxInx)
{
	unsigned int i;	
	for(i=1; i<=MaxInx; i++){
		DrawMasterItem(i);
	}
}

void TCanNetworkRegister::DrawSlaverList(int MaxInx)
{
	unsigned int i;	
	for(i=1; i<=MaxInx; i++){
		DrawSlaverItem(i);
	}
}

void TCanNetworkRegister::ClearSelected()
{
	char *tText;
	switch(CtlIndex){
		case 0:
			IList->Edit[0].Selected =0;
			IList->Edit[0].Show();
			break;
		case 1:
			TRoundSquare::sSelected(xSingle, Lv.Grid.Top[1]+8, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 2:
			TRoundSquare::sSelected(xSlave, Lv.Grid.Top[1]+8, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 3:
			TRoundSquare::sSelected(xMaster, Lv.Grid.Top[1]+8, dWWidth, dWHeigh, 4, ItemColor);
			break;
	}
	if(LanguageEnCn==0){
		switch(CtlIndex){
			case 4:
				TRoundRect::sDrawBySize(xPst0, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "搜索在线机器", 14, clNearBlack);
				break;
			case 5:
				TRoundRect::sDrawBySize(xPst1, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "清集中机列表", 14, clNearBlack);
				break;
			case 6:
				TRoundRect::sDrawBySize(xPst2, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "清区域机列表", 14, clNearBlack);
				break;
			case 7:	
				TRoundRect::sDrawBySize(xPst0, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, "查集中机列表", 14, clNearBlack);
				break;
			case 8:	
				TRoundRect::sDrawBySize(xPst1, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, "查区域机列表", 14, clNearBlack);
				break;
			case 9:	
				TRoundRect::sDrawBySize(xPst2, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, "存集中机列表", 14, clNearBlack);
				break;
			case 10:	
				TRoundRect::sDrawBySize(xPst3, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, "存区域机列表", 14, clNearBlack);
				break;
		}
	}else{
		switch(CtlIndex){
			case 4:
				TRoundRect::sDrawBySize(xPst0, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "Search Online", 8, clNearBlack);
				break;
			case 5:
				TRoundRect::sDrawBySize(xPst1, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "Clear Masters", 8, clNearBlack);
				break;
			case 6:
				TRoundRect::sDrawBySize(xPst2, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clNearWhite, "Clear Salvers", 8, clNearBlack);
				break;
			case 7:	
				TRoundRect::sDrawBySize(xPst0, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, " Master List ", 8, clNearBlack);
				break;
			case 8:	
				TRoundRect::sDrawBySize(xPst1, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, " Slaver List ", 8, clNearBlack);
				break;
			case 9:	
				TRoundRect::sDrawBySize(xPst2, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, " Save Master ", 8, clNearBlack);
				break;
			case 10:	
				TRoundRect::sDrawBySize(xPst3, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clNearWhite, " Save Master ", 8, clNearBlack);
				break;
		}
	}
}

void TCanNetworkRegister::Check4Selected()
{
	char *tText;
	switch(CtlIndex){
		case 0:
			IList->Edit[0].Selected =1;
			IList->Edit[0].SetPstTail();
			IList->Edit[0].Show();
			break;
		case 1:
			TRoundSquare::sSelected(xSingle, Lv.Grid.Top[1]+8, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 2:
			TRoundSquare::sSelected(xSlave, Lv.Grid.Top[1]+8, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 3:
			TRoundSquare::sSelected(xMaster, Lv.Grid.Top[1]+8, dWWidth, dWHeigh, 4, clNearWhite);
			break;
	}
	if(LanguageEnCn==0){
		switch(CtlIndex){
			case 4:
				TRoundRect::sDrawBySize(xPst0, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clBlue, "搜索在线机器", 14, clNearWhite);
				break;
			case 5:
				TRoundRect::sDrawBySize(xPst1, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clBlue, "清集中机列表", 14, clNearWhite);
				break;
			case 6:
				TRoundRect::sDrawBySize(xPst2, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clBlue, "清区域机列表", 14, clNearWhite);
				break;
			case 7:	
				TRoundRect::sDrawBySize(xPst0, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clBlue, "查集中机列表", 14, clNearWhite);
				break;
			case 8:	
				TRoundRect::sDrawBySize(xPst1, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clBlue, "查区域机列表", 14, clNearWhite);
				break;
			case 9:	
				TRoundRect::sDrawBySize(xPst2, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clBlue, "存集中机列表", 14, clNearWhite);
				break;
			case 10:	
				TRoundRect::sDrawBySize(xPst3, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clBlue, "存区域机列表", 14, clNearWhite);
				break;
		}
	}else{
		switch(CtlIndex){
			case 4:
				TRoundRect::sDrawBySize(xPst0, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clBlue, "Search Online", 8, clNearWhite);
				break;
			case 5:
				TRoundRect::sDrawBySize(xPst1, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clBlue, "Clear Masters", 8, clNearWhite);
				break;
			case 6:
				TRoundRect::sDrawBySize(xPst2, 		Lv.Grid.Top[2] +  2, 172, 30, 7, clBlue, "Clear Salvers", 8, clNearWhite);
				break;
			case 7:	
				TRoundRect::sDrawBySize(xPst0, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clBlue, " Masters List", 8, clNearWhite);
				break;
			case 8:	
				TRoundRect::sDrawBySize(xPst1, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clBlue, " Slavers List", 8, clNearWhite);
				break;
			case 9:	
				TRoundRect::sDrawBySize(xPst2, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clBlue, " Save Masters", 8, clNearWhite);
				break;
			case 10:	
				TRoundRect::sDrawBySize(xPst3, 	Lv.Grid.Top[2] + 38, 172, 30, 7, clBlue, " Save Salvers", 8, clNearWhite);
				break;
		}
	}
}

void TCanNetworkRegister::SendNwList()
{
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Priority =1;
	
	//First Pkg 
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 6;
	ProtoC2C.TxMsg.Data.Data[0] =0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = Count;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.CtlMask.StartNum;
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.CtlMask.EndNum;
	
	ProtoC2C.oBufInsert();
	
	//2th Pkg
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =1;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = Count;
	ProtoC2C.TxMsg.Data.Data[4] = CtlList[0];
	ProtoC2C.TxMsg.Data.Data[5] = CtlList[1];
	ProtoC2C.TxMsg.Data.Data[6] = CtlList[2];
	ProtoC2C.TxMsg.Data.Data[7] = CtlList[3];
	
	ProtoC2C.oBufInsert();

	//3th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =2;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = Count;
	ProtoC2C.TxMsg.Data.Data[4] = CtlList[4];
	ProtoC2C.TxMsg.Data.Data[5] = CtlList[5];
	ProtoC2C.TxMsg.Data.Data[6] = CtlList[6];
	ProtoC2C.TxMsg.Data.Data[7] = CtlList[7];
	
	ProtoC2C.oBufInsert();
	
	//4th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =3;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = Count;
	ProtoC2C.TxMsg.Data.Data[4] = CtlList[8];
	ProtoC2C.TxMsg.Data.Data[5] = CtlList[9];
	ProtoC2C.TxMsg.Data.Data[6] = CtlList[10];
	ProtoC2C.TxMsg.Data.Data[7] = CtlList[11];
	
	ProtoC2C.oBufInsert();
	
	//5th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =4;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = Count;
	ProtoC2C.TxMsg.Data.Data[4] = CtlList[12];
	ProtoC2C.TxMsg.Data.Data[5] = CtlList[13];
	ProtoC2C.TxMsg.Data.Data[6] = CtlList[14];
	ProtoC2C.TxMsg.Data.Data[7] = CtlList[15];
	
	ProtoC2C.oBufInsert();
	
	//6th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =5;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = Count;
	ProtoC2C.TxMsg.Data.Data[4] = CtlList[16];
	ProtoC2C.TxMsg.Data.Data[5] = CtlList[17];
	ProtoC2C.TxMsg.Data.Data[6] = CtlList[18];
	ProtoC2C.TxMsg.Data.Data[7] = CtlList[19];
	
	ProtoC2C.oBufInsert();
	
	//7th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =6;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = Count;
	ProtoC2C.TxMsg.Data.Data[4] = CtlList[20];
	ProtoC2C.TxMsg.Data.Data[5] = CtlList[21];
	ProtoC2C.TxMsg.Data.Data[6] = CtlList[22];
	ProtoC2C.TxMsg.Data.Data[7] = CtlList[23];
	
	ProtoC2C.oBufInsert();
	
	//8th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =7;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = Count;
	ProtoC2C.TxMsg.Data.Data[4] = CtlList[24];
	ProtoC2C.TxMsg.Data.Data[5] = CtlList[25];
	ProtoC2C.TxMsg.Data.Data[6] = CtlList[26];
	ProtoC2C.TxMsg.Data.Data[7] = CtlList[27];
	
	ProtoC2C.oBufInsert();
	
	//9th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =8;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = Count;
	ProtoC2C.TxMsg.Data.Data[4] = CtlList[28];
	ProtoC2C.TxMsg.Data.Data[5] = CtlList[29];
	ProtoC2C.TxMsg.Data.Data[6] = CtlList[30];
	ProtoC2C.TxMsg.Data.Data[7] = CtlList[31];
	
	ProtoC2C.oBufInsert();
	
	//10th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =9;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = Count;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.CtlMask.StartNum;
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.CtlMask.EndNum;
	ProtoC2C.TxMsg.Data.Data[6] = 0;
	ProtoC2C.TxMsg.Data.Data[7] = 0;
	
	ProtoC2C.oBufInsert();
}

int TCanNetworkRegister::CheckStart2EndNumValid()
{
	if( (SearchStartNum) && (SearchEndNum) ){
		if( (SearchStartNum <= dNETWORK_MACHINE_COUNT) && (SearchEndNum <= dNETWORK_MACHINE_COUNT) && (SearchStartNum <= SearchEndNum) )
			Start2EndNumValid = 1;
		else
			Start2EndNumValid = 0;
	}else
	if( SearchStartNum || SearchEndNum){
		Start2EndNumValid = 0;
	}else{
		Start2EndNumValid = 1;
	}
	
	if(Start2EndNumValid){
		IList->EditCtlNum.SelectedColor = clBlue;
		IList->EditAddrNum.SelectedColor = clBlue;
		IList->EditCtlNum.Color = clNearWhite;
		IList->EditAddrNum.Color = clNearWhite;
	}else{
		IList->EditCtlNum.SelectedColor = clRed;
		IList->EditAddrNum.SelectedColor = clRed;
		IList->EditCtlNum.Color = clRed;
		IList->EditAddrNum.Color = clRed;
	}
	
	return Start2EndNumValid;
}

TGuiMsgReturn TCanNetworkRegister::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg = guiMsgNone;
	unsigned char aP0,aP1,Err;
	unsigned char x,y,aCh;
	unsigned int aAddr, HadPrc;
	TDMix aFullPath;

	JustShowingSaving =0;
	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;
	}else
	if(IsOnSearching){
		switch(aKey){
			case VK_RETURN:
				IsShowing = 0;
				IsOnSearching = 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(CtlIndex == 0){
					IList->Edit[0].KeyIn(aKey);
					int iVal;
					iVal = IList->Edit[0].Str.ToInteger();
					if( (iVal <1) || (iVal > 64) ){
						MyNumValid = 0;
						IList->Edit[0].SelectedColor = clRed;
					}else{
						MyNumValid = 1;
						IList->Edit[0].SelectedColor = clBlue;
						if(iVal != MyNum){
							MyNum = iVal;
						}
					}
					IList->Edit[0].Show();
				}else{
					if(aKey == VK_LEFT){
						if((CtlIndex==2)||(CtlIndex==3) || (CtlIndex==5)||(CtlIndex==6) || (CtlIndex==8) || (CtlIndex==9) || (CtlIndex==10) ){
							ClearSelected();
							CtlIndex--;
							Check4Selected();
						}else
						if(CtlIndex == 11){
							if(ListX>0){
								DrawListDeSelected();
								ListX--;
								DrawListSelected();
							}
						}
					}else
					if(aKey == VK_RIGHT){
						if(MainCtl.fData.Split.NetworkMode == 1){
							if((CtlIndex==1)||(CtlIndex==2) || (CtlIndex==4)||(CtlIndex==5) || (CtlIndex==7) || (CtlIndex==8) || (CtlIndex==9) ){
								ClearSelected();
								CtlIndex++;
								Check4Selected();
							}else
							if(CtlIndex == 11){
								if(ListY<ListItemsY-1){
									DrawListDeSelected();
									ListX++;
									DrawListSelected();
								}else
								if(ListX<1){
									DrawListDeSelected();
									ListX++;
									DrawListSelected();
								}
							}
						}else{
							if((CtlIndex==1)||(CtlIndex==2) || (CtlIndex==4) || (CtlIndex==5) ){
								ClearSelected();
								CtlIndex++;
								Check4Selected();
							}else
							if(CtlIndex == 11){
								if(ListY<ListItemsY-1){
									DrawListDeSelected();
									ListX++;
									DrawListSelected();
								}else
								if(ListX<1){
									DrawListDeSelected();
									ListX++;
									DrawListSelected();
								}
							}
						}
					}
				}
				break;
			case VK_UP:
				if(MyNumValid){
					if(CtlIndex >0){
						if(CtlIndex<4){
							ClearSelected();
							CtlIndex=0;
							Check4Selected();
						}else
						if(CtlIndex<7){
							ClearSelected();
							CtlIndex-=3;
							Check4Selected();
						}else
						if(CtlIndex<10){
							ClearSelected();
							CtlIndex-=3;
							Check4Selected();
						}else
						if(CtlIndex==10){
							ClearSelected();
							CtlIndex=6;
							Check4Selected();
						}else
						if(CtlIndex==11){
							if(ListWhat != TCanNetworkRegister::TListWhat::Idle){
								if(ListY==0){
									DrawListDeSelected();
									if(ListX<2){
										CtlIndex=7;
									}else{
										CtlIndex=8;
									}
									Check4Selected();
								}else{
									DrawListDeSelected();
									ListY--;
									DrawListSelected();
								}
							}else{
								DrawListDeSelected();
								CtlIndex =8;
								Check4Selected();
							}
						}else{
							DrawListDeSelected();
							ClearSelected();
							CtlIndex =0;
							Check4Selected();
						}
					}else{
						if(ListWhat == TCanNetworkRegister::TListWhat::Idle){
							ClearSelected();
							if(MainCtl.fData.Split.NetworkMode == 1){
								CtlIndex=7;
								Check4Selected();
							}else{
								CtlIndex=4;
								Check4Selected();
							}
						}else{
							ClearSelected();
							CtlIndex =11;
							DrawListDeSelected();
							ListY = ListItemsY-1;
							ListX =0;
							DrawListSelected();
						}
					}
				}else{
					ClearSelected();
					CtlIndex=0;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(MyNumValid){
					if(CtlIndex){
						if(MainCtl.fData.Split.NetworkMode == 1){
							if(CtlIndex<7){
								ClearSelected();
								CtlIndex+=3;
								Check4Selected();
							}else
							if(CtlIndex<11){
								if(ListWhat != this->TListWhat::Idle){
									ClearSelected();
									CtlIndex=11;
									ListY=0;
									ListX=0;
									DrawListSelected();
								}else{
									ClearSelected();
									CtlIndex=0;
									Check4Selected();
								}
							}else
							if(CtlIndex=11){
								if(ListWhat != this->TListWhat::Idle){
									if((ListY<ListItemsY-1) && (ListX<2)){
										DrawListDeSelected();
										ListY++;
										DrawListSelected();
									}else
									if(ListY<ListItemsY-2){
										DrawListDeSelected();
										ListY++;
										DrawListSelected();
									}else{
										DrawListDeSelected();
										CtlIndex=0;
										Check4Selected();
									}
								}else{
									DrawListDeSelected();
									CtlIndex=0;
									Check4Selected();
								}
							}else{
								DrawListDeSelected();
								CtlIndex=0;
								Check4Selected();
							}
						}else{
							if(CtlIndex<4){
								ClearSelected();
								CtlIndex+=3;
								Check4Selected();
							}else
							if(CtlIndex<7){
								if(ListWhat != this->TListWhat::Idle){
									ClearSelected();
									CtlIndex=11;
									ListY=0;
									ListX=0;
									DrawListSelected();
								}else{
									ClearSelected();
									CtlIndex=0;
									Check4Selected();
								}
							}else
							if(CtlIndex==11){
								if(ListWhat != this->TListWhat::Idle){
									if((ListY<ListItemsY-1) && (ListX<2)){
										DrawListDeSelected();
										ListY++;
										DrawListSelected();
									}else
									if(ListY<ListItemsY-2){
										DrawListDeSelected();
										ListY++;
										DrawListSelected();
									}else{
										DrawListDeSelected();
										CtlIndex=0;
										Check4Selected();
									}
								}else{
									ClearSelected();
									DrawListDeSelected();
									CtlIndex=0;
									Check4Selected();
								}
							}else{
								ClearSelected();
								DrawListDeSelected();
								CtlIndex=0;
								Check4Selected();
							}
						}
					}else{
						ClearSelected();
						CtlIndex=1;
						Check4Selected();
					}
				}else{
					ClearSelected();
					CtlIndex=0;
					Check4Selected();
				}
				break;
			case VK_TAB:
			case dfKEY_QUERY:
				DoGroupSelect();
				break;
			case dfKEY_SETUP:
				if(CtlIndex == 0){
					if(MyNumValid){
						PermissionCmd = 21;
						if(SystemPermission > 2){
							PermissionCmdGO();
						}else{
							IsPermission = 1;
							ClearSelected();
							PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
						}
					}
				}else
				if(CtlIndex == 1){
					PermissionCmd = 11;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 2){
					PermissionCmd = 12;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 3){
					PermissionCmd = 13;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}
				break;
			case VK_EXECUTE:
				if(CtlIndex == 0){
					PermissionCmd = 21;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 1){
					PermissionCmd = 11;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 2){
					PermissionCmd = 12;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 3){
					PermissionCmd = 13;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 4){
					//Searching
					PermissionCmd = 1;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 5){
					//Clear Master List
					PermissionCmd = 2;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 6){
					//Clear Slaver List
					PermissionCmd = 3;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 7){
					//Load Master List
					ListWhat = this->TListWhat::QueMaster;
					for(int i=0; i<dNETWORK_MACHINE_COUNT; i++){
						CtlSelectedList[i] =0;
						*(volatile unsigned char *)(dAddrSdRam_TypeList + i) = dNetworkTypeNone;
					}
					for(int i=0; i<dNETWORK_MACHINE_MASTER_COUNT; i++){
						if(i>=ProtoC2C.M2MList.Cnt)break;
						if(ProtoC2C.M2MList.Items[i]){
							if(ProtoC2C.M2MList.Items[i] <= dNETWORK_MACHINE_COUNT){
								*(volatile unsigned char *)(dAddrSdRam_TypeList + (ProtoC2C.M2MList.Items[i]-1)) = dNetworkTypeMaster;
							}
						}
					}
					DrawListTitle();
					DrawMasterList();
					SetGroupSelected(dNetworkTypeMaster);
				}else
				if(CtlIndex == 8){
					//Load Slaver List
					ListWhat = this->TListWhat::QueSlaver;
					for(int i=0; i<dNETWORK_MACHINE_COUNT; i++){
						CtlSelectedList[i] =0;
						*(volatile unsigned char *)(dAddrSdRam_TypeList + i) = dNetworkTypeNone;
					}
					for(int i=0; i<dNETWORK_MACHINE_MASTER_COUNT; i++){
						if(i>=ProtoC2C.SlaveCount)break;
						if(ProtoC2C.SlaveList[i]){
							if(ProtoC2C.SlaveList[i] <= dNETWORK_MACHINE_COUNT){
								*(volatile unsigned char *)(dAddrSdRam_TypeList + (ProtoC2C.SlaveList[i]-1)) = dNetworkTypeSlave;
							}
						}
					}
					if( (ProtoC2C.MasterNum) and (ProtoC2C.MasterNum <=dNETWORK_MACHINE_COUNT) ){
						*(volatile unsigned char *)(dAddrSdRam_TypeList + (ProtoC2C.MasterNum-1)) = dNetworkTypeMaster;
					}
					DrawListTitle();
					DrawSlaverList();
					SetGroupSelected(dNetworkTypeSlave);
				}else
				if(CtlIndex == 9){
					//Save Master List
					PermissionCmd = 4;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 10){
					//Save Slaver List
					PermissionCmd = 5;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 11){
					if(ListWhat == Searching){
						int tInx = ListY*ListItemsX+ListX;
						if(tInx<dNETWORK_MACHINE_COUNT){
							unsigned char pNum = tInx+1; 
							if( (pNum>ProtoC2C.CtlMask.EndNum) || (pNum<ProtoC2C.CtlMask.StartNum) ){
								CtlSelectedList[tInx] = CtlSelectedList[tInx] ? 0 : 1;
								DrawSearchingItem(tInx+1);
							}
						}
					}else
					if(ListWhat == QueMaster){
						int tInx = ListY*ListItemsX+ListX;
						if(tInx<dNETWORK_MACHINE_COUNT){
							if(*(volatile unsigned char *)(dAddrSdRam_TypeList + (tInx)) != dNetworkTypeSlave){
								CtlSelectedList[tInx] = CtlSelectedList[tInx] ? 0 : 1;
								DrawMasterItem(tInx+1);
							}
						}
					}else
					if(ListWhat == QueSlaver){
						int tInx = ListY*ListItemsX+ListX;
						if(tInx<dNETWORK_MACHINE_COUNT){
							if(*(volatile unsigned char *)(dAddrSdRam_TypeList + (tInx)) != dNetworkTypeMaster){
								CtlSelectedList[tInx] = CtlSelectedList[tInx] ? 0 : 1;
								DrawSlaverItem(tInx+1);
							}
						}
					}
				}
				break;
			case VK_RETURN:
				IsShowing = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}
	if(JustShowingSaving == 0){
		if(ShowingSavingMyNum){
			ShowSaveMyNum(0);
		}
	}

	return aMsg;	
}

void TCanNetworkRegister::SendfNetMsg(unsigned char aSubCmd, unsigned char *data, int Len)
{

}

void TCanNetworkRegister::PushfNetMsg(unsigned char aSubCmd, unsigned char *data, int Len)
{
	int i,x, aLen;
	unsigned int aAddr;
	unsigned char ada;
	unsigned char aP0;
	unsigned char Dulp;
	unsigned char MasterOrSlaverCnt;
	
	RecBlock = 1;
	Path = data[1];
	ada = data[3];
	MasterOrSlaverCnt = data[4];
	switch(aSubCmd){
		case CmdC2C_RpyExist:
			break;
		case CmdC2C_RpyType:
			if( (Path) && (Path <= dNETWORK_MACHINE_COUNT) ){
				if( (ada == dNetworkTypeMaster) || (ada == dNetworkTypeSlave) ){
					aP0 = Path-1;
					if( (Path) && (Path <=dNETWORK_MACHINE_COUNT) ){
						if( (Path <ProtoC2C.CtlMask.StartNum) || (Path >ProtoC2C.CtlMask.EndNum) ){
							const unsigned char PrvType = *(volatile unsigned char *)(dAddrSdRam_TypeList + aP0);
							if( (PrvType) || (Path == MainCtl.fData.Split.MyNum) ){
								//Dual
								*(volatile unsigned char *)(dAddrSdRam_TypeList + aP0) = dNetworkTypeDual;
								*(volatile unsigned char *)(dAddrSdRam_LinkMaster + aP0) = MasterOrSlaverCnt;
							}else{
								*(volatile unsigned char *)(dAddrSdRam_TypeList + aP0) = ada;
								*(volatile unsigned char *)(dAddrSdRam_LinkMaster + aP0) = MasterOrSlaverCnt;
							}
						}
					}
				}
			}
			break;
		case CmdC2C_RpyNameLen:
			
			break;
		case CmdC2C_RpyNameContent:
			if(ada == 0xFF){
				aP0 = Path -1;
				if(aP0 <64){
					if( (Path <ProtoC2C.CtlMask.StartNum) || (Path >ProtoC2C.CtlMask.EndNum) ){
						aNewGot = 1;
						aAddr = dAddrSdRam_DescpGotList + aP0;
						*(volatile unsigned char *)(aAddr)= 1;
					}
				}
			}else{
				if(ada < 8){
					x = ada * 4;
					aLen = data[4];
					aP0 = Path -1;
					if( (Path <ProtoC2C.CtlMask.StartNum) || (Path >ProtoC2C.CtlMask.EndNum) ){
						if(  (aLen < 5) && (aP0 < 64) ){
							aAddr = dAddrSdRam_DescpContent + aP0 * 32;
							for(i=0; i<aLen; i++){
								*(volatile unsigned char *)(aAddr + i)= data[5 + i];
							}
						}
					}
				}
			}
			break;
	}	
}

unsigned char TCanNetworkRegister::GetRecType(unsigned char aNum)
{
	unsigned char aType;
	unsigned int aP0, aAddr;
	if( (aNum==0) || (aNum>dNETWORK_MACHINE_COUNT) )return dNetworkTypeNone; 
	aP0 = aNum -1;
	aAddr = dAddrSdRam_TypeList + aP0;
	aType = *(volatile unsigned char *)(aAddr);
	return aType;
}

unsigned char TCanNetworkRegister::CheckRecTypeAva(unsigned char aNum)
{
	unsigned char aType;
	unsigned int aP0, aAddr;
	aP0 = aNum -1;
	aAddr = dAddrSdRam_TypeList + aP0;
	aType = *(volatile unsigned char *)(aAddr);
	if( (aType == dNetworkTypeMaster) || (aType == dNetworkTypeSlave) ){
		return 1;
	}else{
		return 0;
	}
}

unsigned char TCanNetworkRegister::CheckDescpAva(unsigned char aNum)
{
	unsigned char aOk;
	unsigned int aP0, aAddr;
	aP0 = aNum -1;
	aAddr = dAddrSdRam_DescpGotList + aP0;
	aOk = *(volatile unsigned char *)(aAddr + aP0);
	return aOk;
}

unsigned char TCanNetworkRegister::GetCtlCount()
{
	unsigned char aType;
	unsigned int i, aAddr;

	Count =0;
	CountMater =0;
	CountSlave =0;
	aAddr = dAddrSdRam_TypeList;
	for(i=0; i<dNETWORK_MACHINE_COUNT; i++){
		aType = *(volatile unsigned char *)(aAddr + i);
		if( (aType == dNetworkTypeMaster) || (aType == dNetworkTypeSlave) ){
			Count ++;
			if(aType == dNetworkTypeMaster) CountMater++;
			else CountSlave++;
		}
	}
	return Count;
}

void TCanNetworkRegister::SendAskType(unsigned char aNum)
{
	//CmdC2C_RpyType		:    	D0=Len, D1=Pri, D2=CMD, 		D3=CTLBYTE D4=SubCmd 		D5=CtlNum  D6=Type	(Master or Slave, See Define)
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 4;
	ProtoC2C.TxMsg.Data.Data[0] =0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_AskType;
	ProtoC2C.TxMsg.Data.Data[3] = aNum;
	
	ProtoC2C.oBufInsert();
}

void TCanNetworkRegister::SendAskDescp(unsigned char aNum)
{
	//CmdC2C_RpyNameContent	:    	D0=Len, D1=Pri, D2=CMD, 		D3=CTLBYTE D4=SubCmd 		D5=CtlNum  D6=Index	(Index X, D4=ThisPkgByteLen, D7..10=Char[0..3])
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 4;
	ProtoC2C.TxMsg.Data.Data[0] =0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_AskNameContent;	
	ProtoC2C.TxMsg.Data.Data[3] = aNum ;
	
	ProtoC2C.oBufInsert();
}

void TCanNetworkRegister::LoadData2Last()
{
	int i,x;
	unsigned int aAddr;
	//Load Here
	Count = 0;
	for(i=0; i<dNETWORK_MACHINE_COUNT; i++){
		aAddr = dAddrSdRam_ExistTable + i;
		if(*(volatile unsigned char *)(aAddr)){
			Count++;
		}
	}
}

void TCanNetworkRegister::sRecoverSetDefault()
{
	unsigned char tCtlList[dNETWORK_MACHINE_COUNT];
	Record.WriteMainSetMainCtl(0, 1);
	sReadNetMode();
	
	for(int i=0; i<dNETWORK_MACHINE_COUNT; i++)tCtlList[i] =0;
	Record.SaveNwCfg(0, 0, tCtlList);
	Record.ReadNwCfg();
	ProtoC2C.SyncCheckOnline();
}

void TCanNetworkRegister::SaveMasterList(unsigned char aCnt, unsigned char *aList)
{
	Record.SaveNwM2MCfg(aCnt, aList);
}

void TCanNetworkRegister::SendMasterList(unsigned char aCnt, unsigned char *aList)
{
	//MasterList Max = dNETWORK_MACHINE_MASTER_COUNT = 8;
	//First Pkg 
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Priority = 1;
	
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 6;
	ProtoC2C.TxMsg.Data.Data[0] =0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveMasterList;
	ProtoC2C.TxMsg.Data.Data[3] = aCnt;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.CtlMask.StartNum;
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.CtlMask.EndNum;
	
	ProtoC2C.oBufInsert();
	
	//2th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =1;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveMasterList;
	ProtoC2C.TxMsg.Data.Data[3] = aCnt;
	ProtoC2C.TxMsg.Data.Data[4] = aList[0];
	ProtoC2C.TxMsg.Data.Data[5] = aList[1];
	ProtoC2C.TxMsg.Data.Data[6] = aList[2];
	ProtoC2C.TxMsg.Data.Data[7] = aList[3];
	
	ProtoC2C.oBufInsert();

	//3th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =2;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveMasterList;
	ProtoC2C.TxMsg.Data.Data[3] = aCnt;
	ProtoC2C.TxMsg.Data.Data[4] = aList[4];
	ProtoC2C.TxMsg.Data.Data[5] = aList[5];
	ProtoC2C.TxMsg.Data.Data[6] = aList[6];
	ProtoC2C.TxMsg.Data.Data[7] = aList[7];
	
	ProtoC2C.oBufInsert();
}

void TCanNetworkRegister::SendClearMasterList(unsigned char aCnt, unsigned char *aList)
{
	//MasterList Max = dNETWORK_MACHINE_MASTER_COUNT = 8;
	//First Pkg 
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 6;
	ProtoC2C.TxMsg.Data.Data[0] =0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_ClearMasterList;
	ProtoC2C.TxMsg.Data.Data[3] = 0;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.CtlMask.StartNum;
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.CtlMask.EndNum;
	
	ProtoC2C.oBufInsert();
	
	//2th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =1;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_ClearMasterList;
	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();

	//3th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =2;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_ClearMasterList;
	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 TCanNetworkRegister::SaveSlaverList()
{
	unsigned char x,y;
	volatile unsigned int aAddr;
	//Do Save Here
	if( (MainCtl.fData.Split.NetworkMode == 1) && (IncMaster == 0) ){
		Count = GetCtlCount();
		for(x=0; x<dNETWORK_MACHINE_COUNT; x++)CtlList[x] = 0;
		for(x=0; x<Count; x++){
			aAddr = dAddrSdRam_ExistTable + x;
			CtlList[x] = *(volatile unsigned char *)(aAddr);
		}
		Record.SaveNwCfg(MainCtl.fData.Split.MyNum, Count, CtlList);
		Record.ReadNwCfg();
		if( (ProtoC2C.MasterNum == MainCtl.fData.Split.MyNum) && (Count == ProtoC2C.SlaveCount) ){
			if(LanguageEnCn==0)TextRender_string24	(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"保存成功        ");
			else TextRender_string24			(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"Save Completed  ");
			SendNwList();
			Record.OperateAdd_RegCanNetwork(1, MainCtl.fData.Split.MyNum, Count);
			ProtoC2C.SyncCheckOnline();
		}else{
			if(LanguageEnCn==0)TextRender_string24	(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"保存失败        ");
			else TextRender_string24			(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor,	"Failed To Save  ");
		}
	}	
}

void TCanNetworkRegister::DoListSelectAll(unsigned char aCtlType)
{
	volatile unsigned int aAddr;
	unsigned char tType;
	aAddr = dAddrSdRam_TypeList;
	if(aCtlType == 0){
		for(int i=0; i<dNETWORK_MACHINE_COUNT; i++){
			CtlSelectedList[i] = 0;
		}
	}else
	if(aCtlType == dNetworkTypeSlave){
		for(int i=0; i<dNETWORK_MACHINE_COUNT; i++){
			tType = *(volatile unsigned char *)(aAddr +i);
			if(tType == dNetworkTypeSlave){
				CtlSelectedList[i] = 1;
			}else{
				CtlSelectedList[i] = 0;
			}
		}
	}else
	if(aCtlType == dNetworkTypeMaster){
		for(int i=0; i<dNETWORK_MACHINE_COUNT; i++){
			tType = *(volatile unsigned char *)(aAddr +i);
			if(tType == dNetworkTypeMaster){
				CtlSelectedList[i] = 1;
			}else{
				CtlSelectedList[i] = 0;
			}
		}
	}
}

void TCanNetworkRegister::SetGroupSelected(unsigned char aCtlType )
{
	unsigned char aGroup;
	GroupSelected = aGroup;
	
	if(GroupSelected>2)GroupSelected=0;
	
	if(aCtlType ==dNetworkTypeMaster) 		GroupSelected=1;
	else if(aCtlType ==dNetworkTypeSlave) 	GroupSelected =2;
	else GroupSelected = 0;
	
	DoListSelectAll(aCtlType);
	if(ListWhat == this->Searching){
		DrawSearchingList();
	}else
	if(ListWhat == this->QueMaster){
		DrawMasterList();
	}else
	if(ListWhat == this->QueSlaver){
		DrawSlaverList();
	}
}

void TCanNetworkRegister::DoGroupSelect()
{
	unsigned char tType;
	if(ListWhat == this->Idle)return;
	
	if(ListWhat == this->Searching){
		switch(GroupSelected){
			case 0:GroupSelected=1;tType =dNetworkTypeMaster; break;
			case 1:GroupSelected=2;tType =dNetworkTypeSlave; break;
			case 2:GroupSelected=0;tType =0; break;
			default:GroupSelected=0;tType =0; break;
		}
		DoListSelectAll(tType);
		DrawSearchingList();
	}else
	if(ListWhat == this->QueMaster){
		switch(GroupSelected){
			case 0:GroupSelected=1;tType =dNetworkTypeMaster; break;
			case 1:GroupSelected=0;tType =0; break;
			default:GroupSelected=0;tType =0; break;
		}
		DoListSelectAll(tType);
		DrawMasterList();
	}else
	if(ListWhat == this->QueSlaver){
		switch(GroupSelected){
			case 0:GroupSelected=2;tType =dNetworkTypeSlave; break;
			case 2:GroupSelected=0;tType =0; break;
			default:GroupSelected=0;tType =0; break;
		}
		DoListSelectAll(tType);
		DrawSlaverList();
	}
	DrawListSelected();
}

void TCanNetworkRegister::Task1000Ms()
{
	unsigned char x,y;
	volatile unsigned int aAddr;
	if(IsShowing){
		if(IsOnSearching){
			PreventPermissionDown();
			PreventEventShow();
			if(Done){
				IsOnSearching =0;
				ClearSelected();
				CtlIndex=11;
				ListY=0;
				ListX=0;
				SetGroupSelected(dNetworkTypeSlave);
				DrawListSelected();
			}else{
				if(SearchingTick == 0)SearchingTick = GoStartNum;
				if(SearchingTick >(GoEndNum +2) ){
					SearchingTick = GoEndNum +2 +1;
					Count = GetCtlCount();
					//LoadData2Last();
					DrawListTitle();
					DrawSearchingList();
					//ShowSearchCompleted();
					Done = 1;
				}else
				if( (SearchingTick) && (SearchingTick <= GoEndNum) ){
					SearchingNum = SearchingTick;
					SendAskType(SearchingNum);
					if(SearchingNum>1){
						DrawSearchingItem(SearchingNum-1);
						ListDrewIndex = SearchingNum-1;
					}
					if(LanguageEnCn==0)TextRender_string24	(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 		"正在搜索:");
					else TextRender_string24			(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 		"Scanning:        ");
					TextDigitRender2_24				(Lv.Panel.Left +708, Lv.Panel.Top +2, clNearBlack, PanelColor,  SearchingNum % 100);
					
					Count = GetCtlCount();
					//LoadData2Last();
				}else{
					Count = GetCtlCount();
					//LoadData2Last();
					DrawSearchingList();
					if(LanguageEnCn==0)	TextRender_string24(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"搜索完成        ");
					else 				TextRender_string24(Lv.Panel.Left +600, Lv.Panel.Top +2, clNearBlack, PanelColor, 	"Scan Completed  ");
				}
				
				SearchingTick++;
			}
		}
	}
}

int TCanNetworkRegister::ExtRequst(unsigned char Prm)
{
	IsShowing =0;
	IsOnSearching =0;
	return 0;
}

