
var epro2_oper = {
	cmd:0,
	ctr_cmd:0,
	comm_port:"",
	comm_baud:"",
	comm_parity:-1,
	comm_stopbit:-1,
	vbus_mod:1,
	//uid:"581900230A00801",
	uid:"AD23581028A00001",
	pwd:"1234",
	delayfire:-1,
	amend_v:0,
	sorftenhance:1,
	commaddr:21,
	mtp_r_bgaddr:0,//0x3C00,
	mtp_r_len:8,
	mtp_w_addr:0,//0x3C00,
	mtp_w_data:"",
	sys_states:"",
	ack_sp:9,
	ack_cur:0,
	firmware:"",
	chg_class:0,
	chg_group:0,
	blast_delay:0,
	auset_addr_fac:400,
	run_cfg_value:"",
	cycle_bgaddr:501,
	cycle_endaddr:900,
	cycle_group_count:50,
	cycle_bgdelay:0,
	cycle_stepdelay:50,
	//cycle_bguid:"581900230A00801",
	cycle_bguid:"AD23581028A00001",
	cycle_rundelay:10,
	cycle_runcount:1,
	cycle_test:0,
	ep2_high_voltage:85,
	ep2_middle_voltage:50,
	ep2_heartbeat_sw:1,
};
var ep2_qthandle = null;
var ep2_excuedcmd;
var ep2_excued_rtv;
var ep2_cmd_excue_busy = 0
var ep2_bus_cur = 0;
var ep2_recive_buf = [];
var EP2_CMM_RATE = 500;
var ep2_log_write_flag = 0;
function Ep2_Start_Comm()
{
	console.log("Ep2_Start_Comm ");
	if( typeof(ep2_qthandle) != "undefined"  && ep2_qthandle != null )
	{
		ep2_qthandle.SearchCommport();
		console.log("SearchCommport ");
	}
}
function Ep2_Open_CommPort(main_serial)
{
	epro2_oper.comm_port =main_serial.comm_port;
	epro2_oper.comm_baud =main_serial.comm_baud;
	epro2_oper.comm_parity =main_serial.comm_parity;
	epro2_oper.comm_stopbit = main_serial.comm_stopbit;
	
	Main_Close_CommPort_Select();
	var json_str = JSON.stringify(epro2_oper);
	console.log(json_str);
	if( typeof(ep2_qthandle) != "undefined"  && ep2_qthandle != null )
	{
		ep2_qthandle.OpenCommPort(json_str);
	}
}
function ep2_getInput()
{
	epro2_oper.uid = $("#ep2_uid").val();
	epro2_oper.pwd = $("#ep2_pwd").val();
	epro2_oper.delayfire =  parseInt($("#ep2_delayfire").val(),10);
	epro2_oper.commaddr =  parseInt($("#ep2_commaddr").val(),10);
	epro2_oper.mtp_r_bgaddr =  parseInt($("#ep2_mtp_r_bgaddr").val(),16);
	epro2_oper.mtp_r_len = parseInt($("#ep2_mtp_r_len").val(),10);
	epro2_oper.mtp_w_addr = parseInt($("#ep2_mtp_w_addr").val(),16);
	epro2_oper.mtp_w_data =  $("#ep2_mtp_w_data").val();
	epro2_oper.auset_addr_fac =  parseInt($("#ep2_auset_addr_fac").val(),10);
	epro2_oper.run_cfg_value =  $("#ep2_run_cfg_value").val();
	epro2_oper.cycle_bgaddr =  parseInt($("#ep2_cycle_bgaddr").val(),10);
	epro2_oper.cycle_endaddr =  parseInt($("#ep2_cycle_endaddr").val(),10);
	epro2_oper.cycle_group_count =  parseInt($("#ep2_cycle_group_count").val(),10);
	epro2_oper.cycle_bgdelay =  parseInt($("#ep2_cycle_bgdelay").val(),10);
	epro2_oper.cycle_stepdelay =  parseInt($("#ep2_cycle_stepdelay").val(),10);
	epro2_oper.sys_states =   $("#ep2_sys_states").val();
	epro2_oper.cycle_bguid =  $("#ep2_cycle_bguid").val();
	epro2_oper.cycle_rundelay =  parseInt($("#ep2_cycle_rundelay").val(),10);
	epro2_oper.cycle_runcount =  parseInt($("#ep2_cycle_runcount").val(),10);
	epro2_oper.ep2_high_voltage =  parseInt($("#ep2_high_voltage").val(),10);
	epro2_oper.ep2_middle_voltage =  parseInt($("#ep2_middle_voltage").val(),10);
	epro2_oper.chg_class =   parseInt($("#ep2_chg_class").val(),10);
	epro2_oper.chg_group =   parseInt($("#ep2_chg_group").val(),10);
	epro2_oper.blast_delay = parseInt($("#ep2_blast_delay").val(),10);
	epro2_oper.ack_cur =   parseInt($("#ep2_ack_cur").val(),10);
	epro2_oper.ack_sp = parseInt($("#ep2_ack_sp").val(),10);
}
async function ep2_cmd(cmd)
{
	if(ep2_cmd_excue_busy > 0)
	{
		ShowCommMes("正在执行命令",MesgType.MESG_TYPE_INFO);
		return;
	}
	ep2_cmd_excue_busy = 1;
	ep2_getInput();
	epro2_oper.cmd = cmd;
	await Ep2_ExcueOperCmd();
	ep2_cmd_excue_busy = 0;
	
}
function Ep2_Show_Def()
{
	$("#ep2_uid").val(epro2_oper.uid);
	$("#ep2_pwd").val(epro2_oper.pwd);
	if(epro2_oper.delayfire > 0 && epro2_oper.delayfire < 30000 )
	{
		$("#ep2_delayfire").val(epro2_oper.delayfire);
	}else{
		$("#ep2_delayfire").val('');
	}
	
	$("#ep2_commaddr").val(epro2_oper.commaddr);
	$("#ep2_mtp_r_bgaddr").val(Int2HexStr(epro2_oper.mtp_r_bgaddr,"",0));
	$("#ep2_mtp_r_len").val(epro2_oper.mtp_r_len);
	$("#ep2_mtp_w_addr").val(Int2HexStr(epro2_oper.mtp_w_addr,"",0));
	$("#ep2_mtp_w_data").val(epro2_oper.mtp_w_data);
	$("#ep2_auset_addr_fac").val(epro2_oper.auset_addr_fac);
	$("#ep2_run_cfg_value").val(epro2_oper.run_cfg_value);
	$("#ep2_cycle_bgaddr").val(epro2_oper.cycle_bgaddr);
	$("#ep2_cycle_endaddr").val(epro2_oper.cycle_endaddr);
	
	$("#ep2_cycle_group_count").val(epro2_oper.cycle_group_count);
	$("#ep2_cycle_bgdelay").val(epro2_oper.cycle_bgdelay);
	$("#ep2_cycle_stepdelay").val(epro2_oper.cycle_stepdelay);
	$("#ep2_cycle_bguid").val(epro2_oper.cycle_bguid);
	$("#ep2_cycle_rundelay").val(epro2_oper.cycle_rundelay);
	$("#ep2_cycle_runcount").val(epro2_oper.cycle_runcount);
	
	$("#ep2_sys_amend").val(epro2_oper.amend_v);
	$("#ep2_sys_states").val(epro2_oper.sys_states);
	$("#ep2_firmware").val(epro2_oper.firmware);
	
	$("#ep2_high_voltage").val(epro2_oper.ep2_high_voltage);
	$("#ep2_middle_voltage").val(epro2_oper.ep2_middle_voltage);
	$("#ep2_chg_group").val(epro2_oper.chg_group);
	$("#ep2_chg_class").val(epro2_oper.chg_class);
	$("#ep2_blast_delay").val(epro2_oper.blast_delay);
	$("#ep2_ack_cur").val(epro2_oper.ack_cur);
	$("#ep2_ack_sp").val(epro2_oper.ack_sp );
	
}
function Ep2_Init()
{
	var form = layui.form;
	form.render();
	form.on('switch', function(data){
	var id = data.elem.id;
	if(id == "mes_save_log")
	{
		 layer.tips('日志记录 '+  (this.checked ? 'true' : 'false'), data.othis);
		 if(this.checked  )
		 {
			 ep2_log_write_flag = 1;
		 }else{
			 ep2_log_write_flag = 0;
		 }
	}
 //   	else if(id == "soft_enhace")
 //   	{
 //   		layer.tips('软件抗震 '+  (this.checked ? 'true' : 'false'), data.othis)
 //   	}
	else if(id == "ep2_cycle_test"){
		layer.tips('启动循环测试 '+  (this.checked ? 'true' : 'false'), data.othis);
		if(this.checked){
			epro2_oper.cycle_test = 1;
		}else{
			epro2_oper.cycle_test = 0;			
		}
	}
	
	 });
	 
	// form.on('radio', function(data){
	// 	var value = data.value;
	// 	if(value == "DMod" )
	// 	{
	// 		layer.tips('差分通讯', data.othis)
	// 	}else if(value == "SMod")
	// 	{
	// 		layer.tips('单端通讯', data.othis)
	// 	}
		
	//  });
	 

	 
	 if(typeof(qchannel) != "undefined" &&  qchannel != null )
	{
		if(ep2_qthandle != null && typeof(ep2_qthandle) != "undefined")
		{
			console.log("Ep2_Inited 1 OK ---");
		}else{
			ep2_qthandle = qchannel.objects.ep2_qthandle;
			if((typeof(ep2_qthandle) != "undefined") && (ep2_qthandle != null) )
			{
				console.log("Ep2_Init  OK ---");
				ep2_qthandle.ShowCommMes.connect(function(mesg,type){
					ShowCommMes(mesg,type);
					
				});
				ep2_qthandle.ShowCommport.connect(function(jsoninfo){
					Main_ShowCommport(jsoninfo);
				});
				ep2_qthandle.EP2Comm_Recive.connect(function(jsoninfo){
					EP2Comm_Recive(jsoninfo);
				});			
			}
		}		   
	}
	 Ep2_Show_Def();
	 PrapareUIDFeatrue(Str2Bytes(epro2_oper.uid));
}
var ep2_heartbeat_head = Str2Bytes("HBT");
function EP2_HeartBeat(frame_buf)
{
	if(frame_buf[6] != 0x02)
	{
		return;
	}
	var bus_volate = frame_buf[10] + (frame_buf[11] << 8);
	bus_volate = bus_volate / 10;
	bus_volate = bus_volate.toFixed(1);
	var bus_mvolate = frame_buf[12] + (frame_buf[13] << 8);
	bus_mvolate = bus_mvolate / 10;
	bus_mvolate = bus_mvolate.toFixed(1);
	var bus_v = frame_buf[14] + (frame_buf[15] << 8);
	bus_v = bus_v / 10;
	bus_v = bus_v.toFixed(1);
	
	var bus_current = frame_buf[16] + (frame_buf[17] << 8)+(frame_buf[18] << 16)+(frame_buf[19] << 24);
	bus_current = bus_current/1000;
	bus_current = bus_current.toFixed(3);
	var mes = "高电平电压 "+bus_volate+"V 中电平"+bus_mvolate+"V  总线输出  "+bus_v+"V 总线电流  "+bus_current+"mA"
	ep2_bus_cur = bus_current;
	ShowCommMes(mes,MesgType.MESG_TYPE_INFO,ep2_log_write_flag);
}
function EP2Comm_Recive(jsoninfo)
{
	var frame = JSON.parse(jsoninfo);
	
	var frame_str = "Rx: "+ Bytes2Str(frame) ;
	
	if(frame[6] == 0x02 && frame.length > 19)
	{
		EP2_HeartBeat(frame);
		return;
	}else{
		ep2_recive_buf = frame;
		ep2_excued_rtv = frame[9];
		ep2_excuedcmd = frame[7] +  (frame[8] << 8);
		ShowCommMes(frame_str,MesgType.MESG_TYPE_INFO);
		BTInfoCount(1);
		if(ep2_excued_rtv != 0)
		{
			BTErrorCount(1);
		}
	}
	
	
}
function Ep2_CmdData(addr,func,param,read_len,wr_delay,rate)
{
	var cmdbuf = [];
	var w_len = param.length+2;//参数+包头
	epro2_oper.ctr_cmd = 3;
	cmdbuf[0] = read_len & 0xFF;
	cmdbuf[1] = (read_len >> 8) & 0xFF;
	cmdbuf[2] = w_len & 0xFF;
	cmdbuf[3] = (w_len >> 8) & 0xFF;
	cmdbuf[4] = rate & 0xFF;
	cmdbuf[5] = (rate >> 8) & 0xFF;
	cmdbuf[6] = wr_delay;	
	addr <<= 6;
	addr &= 0xFFFC;
	addr |= (func & 0x3F);
	cmdbuf[7] = addr & 0xFF;
	cmdbuf[8] = (addr >> 8) & 0xFF;
	for(var index = 0; index < param.length; index++)
	{
		cmdbuf[9+index] = param[index];
	}
	return cmdbuf;
	
}
var Ep2_Cmd_Head = "Ew";
function Ep2_SendCmd(cmd_buf)
{
	
	var buf = Str2Bytes(Ep2_Cmd_Head);
	
	buf[2] = 0; //数据长度
	buf[3] = 0; //数据长度
	buf[4] = 0; //源地址
	buf[5] = 0; //目标地址
	buf[6] =epro2_oper.ctr_cmd;
	buf[7] =epro2_oper.cmd & 0x0FF;
	buf[8] =(epro2_oper.cmd >> 8) & 0xFF;
	for(var index = 0; index < cmd_buf.length;index++)
	{
		buf[9+index] = cmd_buf[index];
	}
	var send_len = buf.length + 1;
	buf[2] = send_len & 0x0FF;
	buf[3] = (send_len >> 8) & 0x0FF;
	buf[buf.length] = Epj_CalcCrc8(buf,buf.length);
	var json_str = JSON.stringify(buf);
	console.log(json_str);
	if( typeof(ep2_qthandle) != "undefined"  && ep2_qthandle != null )
	{
		ep2_qthandle.Ep2_SendData(json_str);
	}
	var mes = "Tx:" + Bytes2Str(buf);
	ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
}
//同步非阻塞延时
async function Ep2_WaitCmdExcu( cmd,  time_out)
{
    if(time_out == 0)
        return 0;
    time_out = parseInt ( (time_out+19)/20);
    ep2_excued_rtv = 0;
    while((time_out > 0) && (ep2_excued_rtv < 1))
    {
        if(ep2_excuedcmd == cmd)
        {
            break;
        }
        await  TransSleep(20);
        time_out--;
    }
   if(ep2_excued_rtv != 0)
    {
    	 ShowCommMes("命令执行失败",MesgType.MESG_TYPE_ERROR);
		// epro2_oper.cycle_test = 0;//停止循环测试
		 //epro2_oper.cycle_runcount = 1;
    	 return 2;
    }
    if(time_out > 0 )
    {
    	
        return 0;
    }  
   else
    {
       ShowCommMes("通信超时",MesgType.MESG_TYPE_ERROR);
	   //epro2_oper.cycle_test = 0;//停止循环测试
	   epro2_oper.cycle_runcount = 1;
       return  1;

    }
    
}


async function Ep2_HeartBeat_SW()
{
	var cmd_buf = [];
	epro2_oper.ep2_heartbeat_sw ^= 0x01;
	epro2_oper.ctr_cmd = 2;
	cmd_buf[0] = epro2_oper.ep2_heartbeat_sw;
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0)
	{
		if(epro2_oper.ep2_heartbeat_sw > 0)
		{
			ShowCommMes("开启心跳",MesgType.MESG_TYPE_INFO);
		}else{
			ShowCommMes("关闭心跳",MesgType.MESG_TYPE_INFO);
		}
	}
}

async function Ep2_BusSwitch(sw)
{
	var cmd_buf = [];
	cmd_buf[0] = sw;
	cmd_buf[1] = epro2_oper.ep2_high_voltage & 0x0FF;
	cmd_buf[2] = (epro2_oper.ep2_high_voltage >> 8) & 0x0FF;
	cmd_buf[3] = epro2_oper.ep2_middle_voltage & 0x0FF;
	cmd_buf[4] = (epro2_oper.ep2_middle_voltage >> 8) & 0x0FF;
	ep2_excuedcmd = 0;
	epro2_oper.ctr_cmd = 1;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,5000);
	if(rtv == 0)
	{
		if(sw > 0)
		{
			ShowCommMes("开启总线",MesgType.MESG_TYPE_INFO);
		}else{
			ShowCommMes("关闭总线",MesgType.MESG_TYPE_INFO);
		}
	}
}

async function Ep2_WriteMTPCmd(comaddr,regaddr,w_len,param)
{
	var cmd_buf = [];
	epro2_oper.ctr_cmd = 3;
	regaddr >>= 2; //地址除以4
	cmd_buf[0] = regaddr & 0xFF; 
	cmd_buf[1] = w_len;
	for(var index = 0; index < param.length;index++)
	{
		cmd_buf[2+index] = param[index];
	}
	cmd_buf = Ep2_CmdData(comaddr,63,cmd_buf,1,(w_len*2+4),EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	return rtv;
	
}
async function Ep2_ReadMTPCmd(comaddr,regaddr,r_len)
{
	var cmd_buf = [];
	regaddr >>= 2; //地址除以4
	cmd_buf[0] = regaddr & 0xFF; 
	cmd_buf[1] = r_len;
	cmd_buf = Ep2_CmdData(comaddr,62,cmd_buf,r_len+1,2,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	var read_buf = [];
	if(rtv == 0)
	{
		read_buf = ArrayCopy(ep2_recive_buf,read_buf,10,r_len);
		var mes = "地址 "+comaddr+ " MTP 数据" +  Bytes2Str(read_buf) ;
		ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
	}
	return read_buf;
}

async function Ep2_SetAddrByUID()
{
	var uid_arr =  Str2Bytes(epro2_oper.uid);
	//var feature = UidCode_YM2JQ(uid_arr);
	var feature = UidCode_YM2JQ_2(uid_arr);
	cmd_buf = Ep2_CmdData(epro2_oper.commaddr,21,feature,1,4,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0)
	{
		var mes =  "UID: "+epro2_oper.uid+ " 设置地址 "+ epro2_oper.commaddr +" 成功";
		ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
		
	}else{
		var mes =  "UID: "+epro2_oper.uid+ " 设置地址失败";
		ShowCommMes(mes,MesgType.MESG_TYPE_ERROR);
		
	}
	
}

async function Ep2_FastCfgCmd(uid,addr,delay)
{
	var uid_arr =  Str2Bytes(uid);  
	//var feature = UidCode_YM2JQ(uid_arr);
	var feature = UidCode_YM2JQ_2(uid_arr);
	var temp_buf = [];
	temp_buf[0] = delay & 0xFF;
	temp_buf[1] = (delay >> 8) & 0xFF;
	ArrayAppend(feature,temp_buf,2,feature.length);
	cmd_buf = Ep2_CmdData(addr,22,temp_buf,2,70,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0)
	{

		var delay_ = (ep2_recive_buf[11] & 0xFF) << 8;
		delay_ += (ep2_recive_buf[10] & 0xFF);
		
		epro2_oper.delayfire = delay_;
		$("#ep2_delayfire").val(delay_);
		
		var mes = "地址 "+ addr+" 延时：" + delay_;
		ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
	}
	return rtv;
}
async function Ep2_CheckSelfCmd(addr)
{
	var param = [];
	switch(epro2_oper.cmd)
	{
		case 4: param[0] = 0 ; break;
		case 5: param[0] = 1 ;break;
		case 6:	param[0] = 2 ;break;
		
	}
	var cmd_buf  = Ep2_CmdData(addr,32,param,0,4,EP2_CMM_RATE);
	
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	return rtv;
	
}

async function Ep2_ReadCapChgT()
{
	var param = [];
	param[0] = 8;
	var cmd_buf = Ep2_CmdData(epro2_oper.commaddr,32,param,5,4,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0)
	{
		var read_buf = [];
		ArrayCopy(ep2_recive_buf,read_buf,10,4);
		var capT = read_buf[3];
		capT <<= 8;
		capT += read_buf[2];
		capT <<= 8;
		capT += read_buf[1];
		capT <<= 8;
		capT += read_buf[0];
		capT >>= 2;
		var mes ;
		mes = "地址 "+epro2_oper.commaddr +" 电容充电时间us "+ capT;		
		ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
		return;
	}
	
}

async function Ep2_ReadFirmwareCmd(addr)
{
	var param = [];
	var cmd_buf = Ep2_CmdData(addr,8,4,param,8);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	param = [];
	if(rtv == 0)
	{
		
		ArrayCopy(ep2_recive_buf,param,11,4);
		epro2_oper.firmware = Bytes2Str(param);
		var mes = "地址 "+ addr+" 固件版本  "+ epro2_oper.firmware;
		ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
		$("#ep2_firmware").val(epro2_oper.firmware);
	}
	return rtv;
}
async function Ep2_ReadSysStatesCmd(addr)
{
	var param = [];
	var cmd_buf = Ep2_CmdData(addr,28,param,5,80,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0)
	{
		var states_buf = []; 
		ArrayCopy(ep2_recive_buf,states_buf,10,4);
		epro2_oper.sys_states = Bytes2Str(states_buf);
		$("#ep2_sys_states").val(epro2_oper.sys_states);
		var mes =" 状态值 "+ epro2_oper.sys_states;
		ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
	}else{
		var mes ="地址 "+ addr +" 读状态失败 ";
		ShowCommMes(mes,MesgType.MESG_TYPE_ERROR);
	}
	return rtv;
}




async function Ep2_VerifiPwdCmd(addr,pwd)
{
	var pwd_buf = Str2Bytes(pwd);
	var cmd_buf = Ep2_CmdData(addr,24,pwd_buf,1,2,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	return rtv;
}

async function Ep2_ClkAmend()
{
	var cycle = 1000;
	var count = 600;
	var w_len = 2;
	var wr_delay = 8;
	var addr = 0;
	var func = 29;
	var cmdbuf = [];
	epro2_oper.ctr_cmd = 4;
	cmdbuf[0] = w_len & 0xFF;
	cmdbuf[1] = (w_len >> 8) & 0xFF;
	cmdbuf[2] = cycle & 0xFF;
	cmdbuf[3] = (cycle >> 8) & 0xFF;
	cmdbuf[4] = count & 0xFF;
	cmdbuf[5] = (count >> 8) & 0xFF;
	cmdbuf[6] = EP2_CMM_RATE & 0xFF;
	cmdbuf[7] = (EP2_CMM_RATE >> 8) & 0xFF;
	cmdbuf[8] = wr_delay;	
	addr <<= 6;
	addr &= 0xFFFC;
	addr |= (func & 0x3F);
	cmdbuf[9] = addr & 0xFF;
	cmdbuf[10] = (addr >> 8) & 0xFF;
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmdbuf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,2000);
	return rtv;
}
/*
 * type 巡检类型 0 起爆巡检 1 在线巡检
 */
async function Ep2_Inspect(bgaddr,endaddr,state_arr)
{
	var cmdbuf = [];
	var cmd;
	var wr_delay = 8;
	var addr = 0;
	var func = 30;
	var w_len = 8;
	var bitcount = endaddr - bgaddr;
	
	epro2_oper.ctr_cmd = 5;
	
	
	
	cmdbuf[0] = EP2_CMM_RATE & 0xFF;
	cmdbuf[1] = (EP2_CMM_RATE >> 8) & 0xFF;
	cmdbuf[2] = w_len;
	cmdbuf[3] = wr_delay;	
	cmdbuf[4] = bitcount & 0xFF;
	cmdbuf[5] = (bitcount >> 8) & 0xFF;
	
	addr <<= 6;
	addr &= 0xFFFC;
	addr |= (func & 0x3F);
	cmdbuf[6] = addr & 0xFF;
	cmdbuf[7] = (addr >> 8) & 0xFF;
	cmdbuf[8] = bgaddr&0xFF;
	cmdbuf[9] = (bgaddr >> 8)&0xFF;
	cmdbuf[10] = endaddr&0xFF;
	cmdbuf[11] = (endaddr >> 8) &0xFF;
	cmdbuf[12] = state_arr[0];
	cmdbuf[13] = state_arr[1];
	var rtd = {
		rtv : 1,
		ok_addr:[],
		ng_addr:[],
	};
	
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmdbuf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,3000);
	
	
	rtd.rtv =  rtv;
	if(rtv != 0)
		return rtd;
    var ok_addr_buf = [];
	var ng_addr_buf = [];
	var index = 10;//返回数据获取
	var inspect_addr = bgaddr;
	var ok_index = 0;
	var ng_index = 0;
	while(inspect_addr < endaddr)
	{
		var data = ep2_recive_buf[index];//？待定
		index++;
		for(var i = 0; i < 8 ;i++)
		{
			if(inspect_addr > endaddr)
			{
				break;
			}
			if((data & 0x01) > 0) 
			{
				ok_addr_buf[ok_index++] = inspect_addr;
			}else{
				ng_addr_buf[ng_index++] = inspect_addr;
			}
			inspect_addr++;
			data >>= 1;
		}
	}
	rtd.ok_addr = ok_addr_buf;
	rtd.ng_addr = ng_addr_buf;
	return rtd;
}
async function Ep2_DoCmd(addr,cmd)
{
	var param = [];
	var cmd_buf = Ep2_CmdData(addr,8,cmd,param,0);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	return rtv;
}
async function Ep2_Charge()
{
	var param = [];
	param[0] = epro2_oper.chg_class;
	param[1] = epro2_oper.chg_group;
	var cmd_buf = Ep2_CmdData(0,26,param,0,0,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("执行充电成功",MesgType.MESG_TYPE_INFO);
	}
}
async function Ep2_ChipReset()
{
	var param = [];
	param[0] =0x55;
	param[1] = 0xAA;
	param[2] = 0x66;
	param[3] = 0x99;
	var cmd_buf = Ep2_CmdData(0,61,param,0,0,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("执行充电成功",MesgType.MESG_TYPE_INFO);
	}
	if(rtv == 0){
		ShowCommMes("芯片复位执行成功",MesgType.MESG_TYPE_INFO);
	}
}



async function Ep2_EnMTPWrite(flag)
{
	var param = [];	
	param[0] = flag;
	var cmd_buf = Ep2_CmdData(0,40,param,0,0,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		if(flag == 1)
		{
			ShowCommMes("MTP写使能成功",MesgType.MESG_TYPE_INFO);
		}else{
			ShowCommMes("MTP写失能成功",MesgType.MESG_TYPE_INFO);
		}
		
	}else{
		ShowCommMes("失/使能操作失败",MesgType.MESG_TYPE_ERROR);
	}
}
async function Ep2_DisCharge()
{
	var param = [];
	var cmd_buf = Ep2_CmdData(0,27,param,0,0,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("执行放电成功",MesgType.MESG_TYPE_INFO);
	}
}
async function Ep2_SaveFirInfo()
{
	var param = [];
	var cmd_buf = Ep2_CmdData(0,33,param,0,0,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("保存延时信息成功",MesgType.MESG_TYPE_INFO);
	}
}


var Ep2_Inspect_OK = [];

async function Ep2_Inspect_Task(type)
{
	var bgaddr = epro2_oper.cycle_bgaddr;
	var endaddr_ = 0;
	var state_arr = [];
	if(type == 0)
	{
		 state_arr = HexString2ByteArray(epro2_oper.sys_states);
	}
	while(bgaddr < epro2_oper.cycle_endaddr)
	{
		endaddr_ = bgaddr+epro2_oper.cycle_group_count;
		if(endaddr_ > epro2_oper.cycle_endaddr )
		{
			endaddr_ = epro2_oper.cycle_endaddr;
		}
		var rtd = await Ep2_Inspect(bgaddr,endaddr_,state_arr);
		if(rtd.rtv != 0)
		{
			bgaddr+= epro2_oper.cycle_group_count;
			continue;
		}
		bgaddr+= epro2_oper.cycle_group_count;
		await TransSleep(1000);
		var mes = "巡检成功： ";
		for(var index = 0; index < rtd.ok_addr.length;index++)
		{
			Ep2_Inspect_OK[Ep2_Inspect_OK.length] = rtd.ok_addr[index];
			mes += rtd.ok_addr[index]+" ";
		}
		if(rtd.ok_addr.length > 0)
			ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
		var mes = "巡检失败： ";
		for(var index = 0; index < rtd.ng_addr.length;index++)
		{
			mes += rtd.ng_addr[index]+" ";
		}
		if(rtd.ng_addr.length > 0)
			ShowCommMes(mes,MesgType.MESG_TYPE_ERROR);
	}
}

async function Ep2_Blast()
{
	var param = [];
	param[0] = epro2_oper.blast_delay;
	var cmd_buf = Ep2_CmdData(0,25,param,0,0,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("执行起爆成功",MesgType.MESG_TYPE_INFO);
	}
}
async function Ep2_CfgAck()
{
	var param = [];
	param[0] = epro2_oper.ack_sp;
	param[1] = epro2_oper.ack_cur;
	var cmd_buf = Ep2_CmdData(0,35,param,0,0,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("返码配置成功",MesgType.MESG_TYPE_INFO);
	}
}
async function Ep2_RunBootLoader()
{
	var param = [];
	param[0] =0x99;
	param[1] = 0x66;
	param[2] = 0xAA;
	param[3] = 0x55;
	var cmd_buf = Ep2_CmdData(0,61,param,0,0,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("执行跳转成功",MesgType.MESG_TYPE_INFO);
	}
}
async function Ep2_autoSetAddr(max_addr, addr_fac)
{
	var param = [];
	param[0] = max_addr & 0x0FF;
	param[1] = (max_addr >> 8) & 0x0FF;
	param[2] = addr_fac & 0x0FF;
	param[3] = (addr_fac >> 8) & 0x0FF;
	var cmd_buf = Ep2_CmdData(0,39,param,0,0,EP2_CMM_RATE)
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("设置自动分配地址因子成功 " + addr_fac + "最大地址 "+  max_addr ,MesgType.MESG_TYPE_INFO);
	}
	return rtv;
}
async function Ep2_ReadUID()
{
	var param = [];
	var cmd_buf = Ep2_CmdData(epro2_oper.commaddr,41,param,9,2,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0)
	{
		var read_buf = [];
		ArrayCopy(ep2_recive_buf,read_buf,10,8);
		//var result = UidCode_JQ2YM(read_buf);
		var result = UidCode_JQ2YM_NoBackwardSequence(read_buf);
		if(result.rtv)
		{
			epro2_oper.uid = result.uid;
			var mes = "地址 "+epro2_oper.commaddr +"  UID："+ epro2_oper.uid;
			ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
			$("#ep2_uid").val(epro2_oper.uid);
			return 0;
		}else{
			
			ShowCommMes("地址 "+epro2_oper.commaddr +" 读取UID失败 ",MesgType.MESG_TYPE_ERROR);
			rtv  = -1;
		}
		
	}
	return rtv;
}
async function Ep2_WriteUID()
{
	var uid_arr = Str2Bytes(epro2_oper.uid);
	//uid_arr = UidCode_YM2JQ(uid_arr);
	uid_arr = UidCode_YM2JQ_2(uid_arr);
	uid_arr[uid_arr.length]= Epj_CalcCrc8(uid_arr,uid_arr.length);
	var rtv = await Ep2_WriteMTPCmd(epro2_oper.commaddr,0,uid_arr.length,uid_arr);
	if(rtv != 0)
	{
		return;
	}
		
	rtv = await Ep2_WriteMTPCmd(epro2_oper.commaddr,0x80,uid_arr.length,uid_arr);
	if(rtv != 0)
	{
		return;
	}
		
	ShowCommMes(epro2_oper.commaddr + " UID 写入成功",MesgType.MESG_TYPE_INFO);	
	
	
}

async function Ep2_ReadPWD()
{
	var read_buf = []
	read_buf = await Ep2_ReadMTPCmd(epro2_oper.commaddr,0x10,5);
	if(read_buf.length < 1)
	{
		return;
	}
	if(Epj_CalcCrc8(read_buf,4) != read_buf[4])
	{
		ShowCommMes("0x10 PWD存储错误",MesgType.MESG_TYPE_ERROR);
		var mes = Bytes2Str(read_buf);
		console.log("######## " + mes);
		
	}else{
		epro2_oper.pwd = String.fromCharCode.apply(null,read_buf.slice(0,4));
		var mes = "地址 "+epro2_oper.commaddr +" 密码 ："+ epro2_oper.pwd;
		ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
		$("#ep2_pwd").val(epro2_oper.pwd);
	}
}

async function Ep2_WritePWD()
{
	
//ArrayCopy(uid_arr,buf,8,8);
	var pwd_arr = Str2Bytes(epro2_oper.pwd);
	pwd_arr[pwd_arr.length]= Epj_CalcCrc8(pwd_arr,pwd_arr.length);
	rtv = await Ep2_WriteMTPCmd(epro2_oper.commaddr,0x10,pwd_arr.length,pwd_arr);
	if(rtv != 0)
	{
		ShowCommMes(epro2_oper.commaddr + " 密码写入失败 ",MesgType.MESG_TYPE_ERROR);
		return;
	}
		
	rtv = await Ep2_WriteMTPCmd(epro2_oper.commaddr,0x90,pwd_arr.length,pwd_arr);
	if(rtv != 0)
	{
		ShowCommMes(epro2_oper.commaddr + " 密码写入失败 ",MesgType.MESG_TYPE_ERROR);
		return;
	}
		
	ShowCommMes(epro2_oper.commaddr + " 密码写入成功 ",MesgType.MESG_TYPE_INFO);
	
}

async function Ep2_VerfyPWD()
{
	
//ArrayCopy(uid_arr,buf,8,8);
	var param = Str2Bytes(epro2_oper.pwd);
	var cmd_buf = Ep2_CmdData(epro2_oper.commaddr,24,param,1,2,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("执行跳转成功",MesgType.MESG_TYPE_INFO);
	}
	
	
}


async function Ep2_UIDFeaOnline()
{
	var uid_arr = Str2Bytes(epro2_oper.uid);
	var param = PrapareUIDFeatrue(uid_arr);
	var cmd_buf = Ep2_CmdData(epro2_oper.commaddr,17,0,param,5);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0)
	{
		var states_buf = []; 
		ArrayCopy(ep2_recive_buf,states_buf,8,2);
		var delay_ = (ep2_recive_buf[11] & 0xFF) << 8;
		delay_ += (ep2_recive_buf[10] & 0xFF)
		if(delay_ < 5 || delay_ > 30005)
			delay_ = "未设置";
		else
		{
			delay_ -= 5;
			epro2_oper.delayfire = delay_;
			$("#ep2_delayfire").val(delay_);
		}
			
		
		var mes = "地址 "+ epro2_oper.commaddr+" 状态值 "+ Bytes2Str(states_buf)+" 延时：" + delay_;
		ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
	}
	return rtv;
	
}
async function Ep2_WritMTPTask()
{
	var regexp = /(,|;|0x|0X)/g;
	var w_data = epro2_oper.mtp_w_data.replace(regexp," ");
	var w_data_array = w_data.split(" ");
	var w_data_temp = '' ;
	for(var index = 0; index < w_data_array.length;index++)
	{
		var str = w_data_array[index];
		if(str.length < 1)
		{
			continue;
		}else if(str.length == 1)
		{
			str = "0"+str;
		}
		if(str.length != 2)
		{
			continue;
		}
		w_data_temp += str;
	}
	w_data = HexStr2Bytes(w_data_temp);
	await Ep2_WriteMTPCmd(epro2_oper.commaddr,epro2_oper.mtp_w_addr,w_data.length,w_data);
	
}
async function Ep2_SearchTask()
{
	var param = [];
	param[0] = epro2_oper.auset_addr_fac & 0x0FF;
	param[1] = (epro2_oper.auset_addr_fac >> 8) & 0x0FF;
	var cmd_buf = Ep2_CmdData(0,8,30,param,0)
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("设置自动分配地址因子成功 " + epro2_oper.auset_addr_fac  ,MesgType.MESG_TYPE_INFO);
	}
	return rtv;
}
async function Ep2_ReadDelay()
{
 
	 var param = [];
	 var cmd_buf = Ep2_CmdData(epro2_oper.commaddr,34,param,3,2,EP2_CMM_RATE);
	 ep2_excuedcmd = 0;
	 Ep2_SendCmd(cmd_buf);
	 var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	 if(rtv == 0)
	 {
		var read_buf = [];
		ArrayCopy(ep2_recive_buf,read_buf,10,2);
		var delay = read_buf[1];
		delay <<= 8;
		delay += read_buf[0];
		var mes ;
		if(delay < 5)
		{
			mes = "地址 "+epro2_oper.commaddr +" 延时未设置";
			delay = 0;
		}else{
			delay -= 5;
			mes = "地址 "+epro2_oper.commaddr +" 延时 "+ delay;
		}
		epro2_oper.delayfire = delay;
		
		ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
		$("#ep2_delayfire").val(epro2_oper.delayfire);
		return;
	 }else{
		 ShowCommMes("地址 "+epro2_oper.commaddr+" 读延时失败",MesgType.MESG_TYPE_ERROR);
	 }
}
async function Ep2_UpdateAddr(newaddr)
{
	var param = [];
	param[0] = newaddr & 0xFF;
	param[1] = (newaddr >> 8) & 0xFF;
	var cmd_buf = Ep2_CmdData(epro2_oper.commaddr,36,param,0,0,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0)
	{
		ShowCommMes("地址 "+ epro2_oper.commaddr+" 更新成功",MesgType.MESG_TYPE_INFO);
	}else{
		ShowCommMes("地址 "+ epro2_oper.commaddr+" 更新成功",MesgType.MESG_TYPE_ERROR);
	}
}
async function Ep2_WriteDelay()
{
	var param = [];
	epro2_oper.delayfire += 5;
	param[0] = epro2_oper.delayfire & 0xFF;
	param[1] = (epro2_oper.delayfire >> 8) & 0xFF;
	var cmd_buf = Ep2_CmdData(epro2_oper.commaddr,23,param,1,2,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0)
	{
		ShowCommMes("地址 "+ epro2_oper.commaddr+" 延时设置成功",MesgType.MESG_TYPE_INFO);
	}else{
		ShowCommMes("地址 "+ epro2_oper.commaddr+" 延时设置失败",MesgType.MESG_TYPE_ERROR);
	}
}
async function Ep2_ReadAmend()
{
	var param = [];
	var cmd_buf = Ep2_CmdData(epro2_oper.commaddr,37,param,5,2,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0)
	{
		var data = [];
		ArrayCopy(ep2_recive_buf,data,10,4);
		var amend = (data[3]<<24) | (data[2] << 16) | (data[1] << 8) | data[0] ;
		mes = "地址："+ epro2_oper.commaddr +" 校准值 "+amend;
	  	ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
	  	$("#ep2_sys_amend").val(amend);
	 }
}
async function Ep2_ReadRuncfg()
{
	var param = [];
	var cmd_buf = Ep2_CmdData(epro2_oper.commaddr,38,param,6,2,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		var read_buf = []
		ArrayCopy(ep2_recive_buf,read_buf,10,3);
		var cfg_str = Bytes2Str(read_buf);
		read_buf = [];
		ArrayCopy(ep2_recive_buf,read_buf,13,2);
		var ver_str = Bytes2Str(read_buf);
		$("#ep2_run_cfg_value").val(cfg_str);
		$("#ep2_firmware").val(ver_str);
		var mes = "地址："+ epro2_oper.commaddr +" 配置值为 "+ cfg_str + " 版本号 "+ver_str;
		ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
		epro2_oper.run_cfg_value = cfg_str;
		
	}else{
		
		ShowCommMes("读配置信息失败 ",MesgType.MESG_TYPE_ERROR);
	}

}
 
async function Ep2_WriteRuncfg()
{
	var regexp = /(,|;|0x|0X)/g;
	var w_data = epro2_oper.run_cfg_value.replace(regexp," ");
	var w_data_array = w_data.split(" ");
	var w_data_temp = '' ;
	for(var index = 0; (index < w_data_array.length) && (index < 3);index++)
	{
		var str = w_data_array[index];
		if(str.length < 1)
		{
			continue;
		}else if(str.length == 1)
		{
			str = "0"+str;
		}
		if(str.length != 2)
		{
			continue;
		}
		w_data_temp += str;
	}
	w_data = HexStr2Bytes(w_data_temp);
	w_data[w_data.length]= Epj_CalcCrc8(w_data,w_data.length);
	var rtv = await Ep2_WriteMTPCmd(epro2_oper.commaddr,0x1C,w_data.length,w_data);
	if(rtv != 0)
	{
		ShowCommMes("地址 "+ epro2_oper.commaddr+" 配置写入失败",MesgType.MESG_TYPE_ERROR);
		return;
	}
		
	rtv = await Ep2_WriteMTPCmd(epro2_oper.commaddr,0x9C,w_data.length,w_data);
	if(rtv != 0)
	{
		ShowCommMes("地址 "+ epro2_oper.commaddr+" 配置写入失败",MesgType.MESG_TYPE_ERROR);
		return;
	}
	ShowCommMes("地址 "+ epro2_oper.commaddr+" 配置写入成功",MesgType.MESG_TYPE_INFO);
}
 
async function Ep2_ExcueOperCmd()
{
	
	
	var once_excue = 0;
	if(epro2_oper.cycle_test > 0)
	{
		epro2_oper.commaddr = epro2_oper.cycle_bgaddr;
		epro2_oper.uid = epro2_oper.cycle_bguid;
		epro2_oper.delayfire = epro2_oper.cycle_bgdelay;
		epro2_oper.cycle_addr = epro2_oper.cycle_bgaddr;
	}else{
		once_excue = 1;
	}
	while((epro2_oper.cycle_test > 0 && epro2_oper.cycle_runcount > 0) || (once_excue > 0))
	{
		epro2_oper.cycle_runcount--;
		once_excue = 0;
		switch(epro2_oper.cmd)
		{
			case 1 : await Ep2_ChipReset();break;
			case 2 : await Ep2_DisCharge();break; 
			case 3 :  await Ep2_SaveFirInfo();break;
			case 4 : await Ep2_CheckSelfCmd(0);break;
			case 5 : await Ep2_CheckSelfCmd(0);break;
			case 6 : await Ep2_CheckSelfCmd(0);break;
			case 7 : await Ep2_ClkAmend();break;
			case 9:  await Ep2_Inspect_Task(0);break;
			case 11 : await Ep2_ReadCapChgT();break;
			case 13: await Ep2_HeartBeat_SW();break;
		    case 15: await Ep2_BusSwitch(1); break;
			case 16: await Ep2_BusSwitch(0); break;
			case 17: await Ep2_Charge(); break;
			case 18: await Ep2_Blast();break;
			case 19: await Ep2_ReadUID();break;
			case 20: await Ep2_WriteUID();break;
			case 21: await Ep2_ReadPWD();break;
			case 22: await Ep2_WritePWD();break;
			case 23: await Ep2_VerfyPWD();break;
			case 24: await Ep2_SetAddrByUID();break;
			case 25: await Ep2_FastCfgCmd(epro2_oper.uid,epro2_oper.commaddr,epro2_oper.delayfire); break;
			case 26: await Ep2_ReadDelay();break;
			case 27: await Ep2_WriteDelay();break;
			case 28: await Ep2_ReadMTPCmd(epro2_oper.commaddr,epro2_oper.mtp_r_bgaddr,epro2_oper.mtp_r_len);break;
			case 29: await Ep2_EnMTPWrite(1);break;
			case 30: await Ep2_EnMTPWrite(0);break;
			case 31: await Ep2_WritMTPTask();break;
			case 32: await Ep2_ReadSysStatesCmd(epro2_oper.commaddr);break;
			case 33: await Ep2_ReadAmend();break;
			case 35: await Ep2_autoSetAddr(epro2_oper.cycle_endaddr,epro2_oper.auset_addr_fac);break;
			case 36: await Ep2_ReadRuncfg();break;
			case 37: await Ep2_WriteRuncfg();break;
			case 38: await Ep2_CfgAck();break;
			//case 37: await fac_bind();break;
			// case 5 : await Ep2_CheckSelfCmd(0);break;
			// case 6 : await Ep2_CheckSelfCmd(0);break;
			// case 7 : await Ep2_ClkAmend_Task();break;
			// case 8 : 
			// case 9 : await Ep2_Inspect_Task(1);break;
			// case 10: await Ep2_Sleep();break;
			// case 11: await Ep2_AWake();break;
			// case 12: 
			// case 13: await Ep2_RunBootLoader();break;
			// case 14:


			// case 19: await Ep2_VerifiPwdCmd(epro2_oper.commaddr,epro2_oper.pwd);break;
			// case 20: await Ep2_UIDFeaOnline(); break;
			// 
			//
			// case 23: 
			// case 24: 
			// case 25: await Ep2_ReadFirmwareCmd(epro2_oper.commaddr);break;
			// case 26: await Ep2_SearchTask(); break;
			// case 27: 
			// case 28: await Ep2_ChipReset();break;
			// case 29: 
			// case 30: 
			// case 31: 
			// case 32: 
			// case 33: 
			// case 34: await Ep2_HeartBeat_SW();break;
			
			default:return;
		
		}
		if(epro2_oper.cycle_test > 0)
		{
			if(epro2_oper.cycle_rundelay > 0)
			{
				await  TransSleep(epro2_oper.cycle_rundelay);
			}
			Ep2_CycleParamUpdate();
			
		}
	}
	
	if(epro2_oper.cycle_test > 0)
	{
		
		ShowCommMes("循环测试完成",MesgType.MESG_TYPE_INFO);
	}	
		
	
	 
}

function Ep2_CycleParamUpdate()
{
	if(epro2_oper.cycle_addr < epro2_oper.cycle_endaddr)
	{
		epro2_oper.cycle_addr++;	
	}else{
		epro2_oper.cycle_addr = epro2_oper.cycle_bgaddr;
	}
	epro2_oper.commaddr = epro2_oper.cycle_addr;
	epro2_oper.delayfire = (epro2_oper.commaddr - epro2_oper.cycle_bgaddr)*epro2_oper.cycle_stepdelay + epro2_oper.cycle_bgdelay;
   var flow_str = epro2_oper.cycle_bguid.slice(12,16);
    var uidhead = epro2_oper.cycle_bguid.slice(0,12);
    var flow_num = parseInt(flow_str,10);
    flow_num = flow_num+epro2_oper.commaddr - epro2_oper.cycle_bgaddr;
	epro2_oper.uid = uidhead + IntFormat(4,flow_num);
}
