var fac_bind_init_flag = 0;
var fac_bind_run = 0;
async function fac_bind()
{
	if(fac_bind_init_flag == 0)
	{
		ShowCommMes("注码脚本加载",MesgType.MESG_TYPE_INFO);
		fac_bind_init_flag =1;
		var content_str = '<div class="layui-row" style="line-height: 1.8rem; padding-top: 2px;">';
		content_str +=  '<div class="layui-col-xs2">'
		content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_bind_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="fac_bind()">启动测试</button>';
		content_str += '</div></div>'
		 $("#Oper_View").append(content_str);
		return;
	}
	if(fac_bind_run == 0)
	{
		fac_bind_run = 1;
		var obj = $(".plugin_bind_test")[0];
		obj.innerText = "停止测试";
		
	}else{
		ShowCommMes("注码脚本运行结束",MesgType.MESG_TYPE_INFO);
		fac_bind_run = 0;
		var obj = $(".plugin_bind_test")[0];
		obj.innerText = "启动测试";
		return;
	}
	
	epro2_oper.ep2_heartbeat_sw = 0;
	ep2_getInput();
	epro2_oper.cmd = 13;
	await Ep2_HeartBeat_SW();
	ShowCommMes("注码脚本运行启动 fac_bind_run = "+fac_bind_run,MesgType.MESG_TYPE_INFO);
	var flow_str = epro2_oper.uid.slice(12,16);
	var flow_num = parseInt(flow_str,10);
	var rtv  = 0;
	while(fac_bind_run > 0)
	{
		ShowCommMes("等待模块接入。。flow_num "+flow_num,MesgType.MESG_TYPE_INFO);
		while(ep2_bus_cur < 0.01 && fac_bind_run > 0 && ep2_bus_cur < 0.025)
		{
			await  TransSleep(300);
		}
		await  TransSleep(2000);
		epro2_oper.commaddr = 1;
		//读状态
		rtv = await Ep2_ReadSysStatesCmd(epro2_oper.commaddr);
		if(rtv > 0)
		{
			continue; 
		}
		epro2_oper.cmd = 29;
		await Ep2_EnMTPWrite(1);
		await  TransSleep(300);
		var last_uid = epro2_oper.uid;
		 var uidhead = epro2_oper.uid.slice(0,12);
		epro2_oper.uid = uidhead + IntFormat(4,flow_num);
		epro2_oper.cmd = 20;
		await Ep2_WriteUID();
		epro2_oper.cmd = 22;
		await Ep2_WritePWD();
		epro2_oper.cmd = 19;
		rtv = await Ep2_ReadUID();
		if(rtv != 0)
		{
			epro2_oper.uid = last_uid;
			ShowCommMes("赋码失败 rtv " + rtv,MesgType.MESG_TYPE_ERROR);
		}else{
			ShowCommMes("赋码成功",MesgType.MESG_TYPE_INFO);
			flow_num++;
		}
		while(ep2_bus_cur > 0.01 && fac_bind_run > 0)
		{
			await  TransSleep(300);
		}
	}
	ShowCommMes("注码脚本运行结束",MesgType.MESG_TYPE_INFO);
}