var recheck_oper = {
	cmd:0,
	comm_port:"",
	comm_baud:"",
	comm_parity:-1,
	comm_stopbit:-1,
	run_mode :1,
	firmware:"",
	update_firmware:"",
	firmware_size:0,
	firmware_crc:0,
}
var recheck_qthandle = null;
var Reremend_value =  new Array();
var channelre_v = new Array();
var recheck_recive_buf = [];
var recheck_chennel_enable = new Array();
var recheck_version  = [0x01,0x00,0x01,0x00];
var recheck_excuedcmd = 0;
var recheck_excued_rtv = 0;
var recheck_timeout_diff = 5000;
var recheck_updata_rtvinfo = "";
var recheck_app_addr =  0x08004000;
function ReCheck_Start_Comm()
{
	console.log("ReCheck_Start_Comm ");
	if( typeof(recheck_qthandle) != "undefined"  && recheck_qthandle != null )
	{
		recheck_qthandle.SearchCommport();
		console.log("SearchCommport ");
	}
}
function ReCheck_Open_CommPort(main_serial)
{
	recheck_oper.comm_port =main_serial.comm_port;
	recheck_oper.comm_baud =main_serial.comm_baud;
	recheck_oper.comm_parity =main_serial.comm_parity;
	recheck_oper.comm_stopbit = main_serial.comm_stopbit;
	
	Main_Close_CommPort_Select();
	var json_str = JSON.stringify(recheck_oper);
	console.log(json_str);
	if( typeof(recheck_qthandle) != "undefined"  && recheck_qthandle != null )
	{
		recheck_qthandle.OpenCommPort(json_str);
	}
}


function ReCheck_InputUpdateShow(data_array)
{
	var obj;
	var obj_name;
	var channel;
	for(var i=0; i < 20; i++)
	{
		
		channel = i+1;
		obj_name = "#RChannel"+channel+"_input";
		obj = $(obj_name);
		obj.val(data_array[i].toFixed(2));
		
	}
}

function ReCheck_Init()
{
	if(recheck_qthandle == null)
	{
		for(var i = 0; i < 20; i++)
		{
			Reremend_value[i] = 0.0;
			recheck_chennel_enable[i] = 1;
			channelre_v[i] = 0.0;
		}
	}
	
	
	 //if(typeof(qt) != "undefined" &&  qt != null )
	 if(typeof(qchannel) != "undefined" &&  qchannel != null )
	{
	
		 //var qwebchannel =  new QWebChannel(qt.webChannelTransport,function(channel){
		   recheck_qthandle = qchannel.objects.recheck_qthandle;
		   if((typeof(recheck_qthandle) != "undefined") && (recheck_qthandle != null) )
		   {
		   	 	console.log("ReCheck_Init  OK ---");
		   	 	
				recheck_qthandle.ShowCommMes.connect(function(mesg,type){
					ShowCommMes(mesg,type);
					
				});
				recheck_qthandle.ShowCommport.connect(function(jsoninfo){
					Main_ShowCommport(jsoninfo);
				});
				recheck_qthandle.ReCheckComm_Recive.connect(function(jsoninfo){
					ReCheckComm_Recive(jsoninfo);
				});
				recheck_qthandle.FirmWateUpdate_Return.connect(function(jsoninfo){
					recheck_excuedcmd = 0xFFFE;//获取固件更新返回
					recheck_excued_rtv = 0;
					recheck_updata_rtvinfo = jsoninfo;
					console.log("FirmWateUpdate_Return "+jsoninfo);
					
				});
				
		   }
		//});
	}

}

function ReCheckComm_Recive(jsoninfo)
{
	var frame = JSON.parse(jsoninfo);
	recheck_recive_buf = frame;
	if(recheck_oper.run_mode == 1)
	{
		var rtv = ZhangXingDecode(recheck_recive_buf);
		if(rtv.rtv > 0)
		{
			return ; 
		}
		recheck_excuedcmd = recheck_recive_buf[5];
		recheck_excued_rtv = recheck_recive_buf[8];
	}else{
		ZhangXingSDcode(recheck_recive_buf);
		cmd = recheck_recive_buf[4];
		switch(cmd)
		{
			case 0x01:ReCheckS_Measure();break;
			case 0x02:ReCheckS_WriteAmendV();break;
			case 0x03:ReCheckS_ReadAmendV();break;
			case 0x04:ReCheckS_Amend();break;
			case 0x05:ReCheckS_RVersion();break;
		}
	}
}


function ZhangXingCode( cmdparam)
{
	var ZhangXinHeader = "Ym";
	var buf = Str2Bytes(ZhangXinHeader);
	
	buf[2] = cmdparam.length & 0x0FF; //数据长度
	buf[3] = (cmdparam.length >> 8) & 0x0FF; //数据长度
	var buf_count = 4;
	var checkA = 0;
	var checkB = 0;
	for(var i = 0;  i < cmdparam.length;i++)
	{
		buf[buf_count++] = cmdparam[i];
		checkA += cmdparam[i] & 0x0FF;
		checkB += checkA;
	}
	checkA &= 0x0FF;
	checkB &= 0x0FF;
	buf[buf_count++] = checkA;
	buf[buf_count++] = checkB;
	return buf;
}

function ZhangXingSCode(cmdparam)
{
	var ZhangXinHeader = "YmC";
	var buf = Str2Bytes(ZhangXinHeader);
	
	buf[3] = cmdparam.length & 0x0FF; //数据长度
	buf[4] = (cmdparam.length >> 8) & 0x0FF; //数据长度
	var buf_count = 5;
	var checkA = 0;
	var checkB = 0;
	for(var i = 0;  i < cmdparam.length;i++)
	{
		buf[buf_count++] = cmdparam[i];
		checkA += cmdparam[i] & 0x0FF;
		checkB += checkA;
	}
	checkA &= 0x0FF;
	checkB &= 0x0FF;
	buf[buf_count++] = checkA;
	buf[buf_count++] = checkB;
	return buf;
}
function ZhangXingSDcode(buf)
{
	
	if(buf.len < 9)
	{
		return 1;
	}
	if(buf[0] != 0x59 || buf[1] != 0x6D)
	{
		return 1;
	}
	var len = buf[2] + (buf[3] << 8);
	if((len + 6) < buf.length)
	{
		return 1;
	}
	var checkA = 0;
	var checkB = 0;
	for(var i = 0; i <  len; i++)
	{
		checkA += buf[i+4] & 0x0FF;
		checkB += checkA;
	}
	checkA &= 0x0FF;
	checkB &= 0x0FF;
	if((buf[len+4] != checkA) || (buf[len+5] != checkB))
	{
		return 1;
	}
	return 0;
}
function ZhangXingDecode( buf)
{
	var ZhangXinHeader = "YmC";
	var headbuf = Str2Bytes(ZhangXinHeader);
	var index = 0;
	var result = {
		rtv:0,
		databuf:[],
	};
	for(index  = 0; index < buf.length && index < 3;index++)
	{
		if(buf[index] != headbuf[index])
		{
			break;
		}
	}
	if(index != 3)
	{
		result.rtv = 1;
		return result;
	}
	var len = ((buf[4] & 0x0FF) << 8 | (buf[3]&0x0FF));
	if((len+ 7) != buf.length)
	{
		result.rtv = 1;
		return result;
	}
	var checkA = 0;
	var checkB = 0;
	for(var i = 0; i <  len; i++)
	{
		result.databuf[i] = buf[i+5];
		checkA += result.databuf[i] & 0x0FF;
		checkB += checkA;
	}
	checkA &= 0x0FF;
	checkB &= 0x0FF;
	if((buf[buf.length-2] != checkA) || (buf[buf.length-1] != checkB))
	{
		result.rtv = 1;
		return result;
	}
	return result;
}

function ReChannel_Btn(channel)
{
	if(recheck_oper.run_mode != 1)
	{
		return;
	}
	var channel_en = recheck_chennel_enable[channel-1];
	var btnobj = $("#rechannel_btn"+channel);
	if(btnobj ==  "undefined" || btnobj == null)
	{
		return;
	}
	if(channel_en > 0 )
	{
		channel_en = 0;
		btnobj.removeClass("layui-btn-normal");
		btnobj.addClass("layui-btn-disabled");
	}else{
		
		channel_en = 1;
		btnobj.removeClass("layui-btn-disabled");
		btnobj.addClass("layui-btn-normal");
		
	}
	recheck_chennel_enable[channel-1] = channel_en;
}


function ReCheckSendData(buf)
{
	var json_str = JSON.stringify(buf);
	console.log(json_str);
	if( typeof(recheck_qthandle) != "undefined"  && recheck_qthandle != null )
	{
		recheck_qthandle.ReCheck_SendData(json_str);
	}
	var mes = "Tx:" + Bytes2Str(buf);
	ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
}
function ReCheckMode( mode)
{
	var masterbtnobj = $("#recheckMaster_btn");
	var slavebtnobj = $("#recheckSlave_btn");
	recheck_oper.run_mode = mode;
	if(recheck_oper.run_mode == 1)
	{
		masterbtnobj.removeClass("layui-btn-disabled");
		slavebtnobj.addClass("layui-btn-disabled");
	}else{
		slavebtnobj.removeClass("layui-btn-disabled");
		masterbtnobj.addClass("layui-btn-disabled");
	}

}
//同步非阻塞延时
async function ReCheck_WaitCmdExcu( cmd,  time_out)
{
    if(time_out == 0)
        return 0;
	time_out += recheck_timeout_diff;
    time_out = parseInt ( (time_out+19)/20);
    recheck_excued_rtv = 0;
    while((time_out > 0) && (recheck_excued_rtv < 1))
    {
        if(recheck_excuedcmd == cmd)
        {
            break;
        }
        await  TransSleep(20);
        time_out--;
    }
   if(recheck_excued_rtv != 0)
    {
    	 ShowCommMes("命令执行失败",MesgType.MESG_TYPE_ERROR);
    	 return 2;
    }
    if(time_out > 0 )
    {
    	
        return 0;
    }  
   else
    {
       ShowCommMes("通信超时",MesgType.MESG_TYPE_ERROR);
       return  1;

    }
    
}
async function ReCheckM_Measure()
{
	var cmdb_buf =[0x01,0x01,0x00,0x00,0x00,0x00,0x00];
	var channelmask = 0;
	var bit_mask;
	var temp_data = 0;
	for(var i = 0; i < 20; i++)
	{
		bit_mask = 0;
		if(recheck_chennel_enable[i] > 0)
		{
			bit_mask = 1 << i;			
		}
		channelmask |= bit_mask;
		
	}
	for(i = 0; i < 4; i++)
	{
		cmdb_buf[3+i] = ((channelmask>>(i*8)) & 0x0FF);
	}
	
	cmdb_buf = ZhangXingCode(cmdb_buf);
	ReCheckSendData(cmdb_buf);
	recheck_excuedcmd = 0;
	var rtv = await ReCheck_WaitCmdExcu(1,1000)
	if(rtv != 0)
	{
		return;
	}
	var mes_str = "测量成功： ";
	for(var i = 0 ,j = 0 ; i < 40;i+=2,j++)
	{
		if(recheck_chennel_enable[j] > 0)
		{
			channelre_v[j] = (recheck_recive_buf[14+i]*256 + recheck_recive_buf[13+i]) / 100.0;
			mes_str += "ch"+(j+1)+": ";
			mes_str += channelre_v[j].toFixed(2);
			mes_str += " ";
		}else{
			
			channelre_v[j] = 0;
		}
		
	}
	ReCheck_InputUpdateShow(channelre_v);
	ShowCommMes(mes_str,MesgType.MESG_TYPE_INFO);
}
function ReCheckS_Measure()
{
	var ack_buf = [];
	var channelmask = 0;
	var temp_data;
	for(var i = 0; i < 3;i++)
	{
		ack_buf[i] = recheck_recive_buf[4+i];
	}
	ack_buf[3] = 0;
	for(var i = 0; i < 4;i++)//拷贝掩码
	{
		temp_data = recheck_recive_buf[7+i];
		ack_buf[i+4] = temp_data;
		channelmask += ((temp_data & 0x0FF) << (i*8));
	}
	for(var i = 0; i < 20;i++)
	{
		if((channelmask & 0x01) > 0)
		{
			temp_data = parseInt(channelre_v[i] * 100,10);
			
		}else{
			temp_data = 0;
		}
		
		ack_buf[8+2*i] = temp_data & 0x0FF;
		ack_buf[9+2*i] = (temp_data  >> 8 )& 0x0FF;
	}
	
	ack_buf = ZhangXingSCode(ack_buf);
	ReCheckSendData(ack_buf);
	
}
async function ReCheckM_WriteAmendV()
{
	var cmdb_buf =[0x02,0x01,0x00,0x00,0x00,0x00,0x00];
	var channelmask = 0;
	var bit_mask;
	var temp_data = 0;
	for(var i = 0; i < 20; i++)
	{
		bit_mask = 0;
		if(recheck_chennel_enable[i] > 0)
		{
			bit_mask = 1 << i;
			temp_data = parseInt(Reremend_value[i]*100,10);
			cmdb_buf[i*2+7] = temp_data & 0x0FF;
			cmdb_buf[i*2+8] =  (temp_data >> 8)& 0x0FF;
			
		}else{
			
			cmdb_buf[i*2+7] = 0;
			cmdb_buf[i*2+8] = 0;
		}
		channelmask |= bit_mask;
		
	}
	for(i = 0; i < 4; i++)
	{
		cmdb_buf[3+i] = channelmask>>(i*8);
	}
	cmdb_buf = ZhangXingCode(cmdb_buf);
	ReCheckSendData(cmdb_buf);
	recheck_excuedcmd = 0;
	var rtv = await ReCheck_WaitCmdExcu(2,1000)
	if(rtv != 0)
	{
		return;
	}
	ShowCommMes("设置校准值成功： ",MesgType.MESG_TYPE_INFO);
}

function ReCheckS_WriteAmendV()
{
	var ack_buf = [];
	for(var i = 0; i < 3;i++)
	{
		ack_buf[i] = recheck_recive_buf[4+i];
	}
	ack_buf[3] = 0;
	ack_buf = ZhangXingSCode(ack_buf);
	ReCheckSendData(param);
}

async function ReCheckM_ReadAmendV()
{
	var cmdb_buf =[0x03,0x01,0x00];
	cmdb_buf = ZhangXingCode(cmdb_buf);
	ReCheckSendData(cmdb_buf);
	recheck_excuedcmd = 0;
	var rtv = await ReCheck_WaitCmdExcu(3,1000)
	if(rtv != 0)
	{
		return;
	}
	var mes_str = "读校准值成功： ";
	for(var i = 0,j=0 ; i < 40;i+=2,j++)
	{
		Reremend_value[j] = (recheck_recive_buf[10+i] + recheck_recive_buf[9+i]) / 100.0;
		mes_str += Reremend_value[j].toFixed(2);
		mes_str += " ";
		
	}
	ReCheck_InputUpdateShow(Reremend_value);
	ShowCommMes(mes_str,MesgType.MESG_TYPE_INFO);
}
function ReCheckS_ReadAmendV()
{
	var ack_buf = [];
	for(var i = 0; i < 3;i++)
	{
		ack_buf[i] = recheck_recive_buf[4+i];
	}
	ack_buf[3] = 0;
	var temp_data = 0;
	for(var i =0 ; i < 20; i++)
	{
		temp_data = parseInt(Reremend_value[i]*100,10);
		ack_buf[i*2+4] = temp_data & 0x0FF;
		ack_buf[i*2+5] = (temp_data >> 8)& 0x0FF;
	}
	ack_buf = ZhangXingSCode(ack_buf);
	ReCheckSendData(ack_buf);
}
async function ReCheckM_Amend()
{
	var checkmask = 0x00;
	var bit_mask = 0;
	for(var i = 0; i < 20; i++)
	{
		bit_mask = 0;
		if(recheck_chennel_enable[i] > 0)
		{
			bit_mask = 1 << i;
		}
		checkmask |= bit_mask;
	}
	var cmdb_buf= [0x04,0x01,0x00];
	for(i = 0; i < 4; i++)
	{
		cmdb_buf[3+i] = ((checkmask>>(i*8)) & 0x0FF);
	}
	cmdb_buf = ZhangXingCode(cmdb_buf);
	ReCheckSendData(cmdb_buf);
	recheck_excuedcmd = 0;
	var rtv = await ReCheck_WaitCmdExcu(4,1000)
	if(rtv != 0)
	{
		return;
	}
	ShowCommMes("校准成功",MesgType.MESG_TYPE_INFO);
}

function ReCheckS_Amend()
{
	var ack_buf = [];
	for(var i = 0; i < 3;i++)
	{
		ack_buf[i] = recheck_recive_buf[4+i];
	}
	ack_buf[3] = 0;
	for(var i = 0; i < 4;i++)//拷贝掩码
	{
		ack_buf[i+4] = recheck_recive_buf[7+i];
	}
	ack_buf = ZhangXingSCode(ack_buf);
	ReCheckSendData(ack_buf);
	
}

async function ReCheckM_RVersion()
{
	var cmdb_buf =[0x05,0x01,0x00];
	cmdb_buf = ZhangXingCode(cmdb_buf);
	ReCheckSendData(cmdb_buf);
	recheck_excuedcmd = 0;
	var rtv = await ReCheck_WaitCmdExcu(5,1000)
	if(rtv != 0)
	{
		return;
	}
	var mes = "";
	for(var i = 9; i < 13; i++ )
	{
		
		recheck_version[i-9] = recheck_recive_buf[i];
	}
	mes += 	Bytes2Str(recheck_version);
	var obj = $("#ReVersion_input");
	obj.val(mes);
	ShowCommMes("读取版本： "+mes,MesgType.MESG_TYPE_INFO);
	
}

function ReCheckS_RVersion()
{
	var ack_buf = [0x05,recheck_recive_buf[6],recheck_recive_buf[7],0x00,
		recheck_version[0],recheck_version[1],recheck_version[2],recheck_version[3]];
	ack_buf = ZhangXingSCode(ack_buf);
	ReCheckSendData(ack_buf);
}


function ReCheckS_SetAmendV()
{
	var obj;
	var obj_name;
	var channel;
	var mes_str ;
	for(var i=0; i < 20; i++)
	{
		if(recheck_chennel_enable[i] < 1)
		{
			continue;
		}
		channel = i+1;
		mes_str = "设置通道校准值  通道"+channel +": ";
		obj_name = "#RChannel"+channel+"_input";
		obj = $(obj_name);
		Reremend_value[i] = obj.val()*1;
		mes_str += obj.val();
		ShowCommMes(mes_str,MesgType.MESG_TYPE_INFO);
	}
}
function ReCheckS_SetVersion()
{
	var obj = $("#ReVersion_input");
	var version_v = obj.val();
	var regexp = /(,|;|0x|0X)/g;
	var w_data = version_v.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;
	}
	
	var version_buf = HexStr2Bytes(w_data_temp);
	if(version_buf.length != 4)
	{
		return;
	}
	var mes_str = "设置软件版本 ： "
	for(var i = 0; i < 4; i++)
	{
		recheck_version[i] = 0x00;
	}
	for(var i = 0; i < 4 && i < version_buf.length; i++)
	{
		recheck_version[i] = version_buf[i];
	}
	
	mes_str += Bytes2Str(recheck_version);
	ShowCommMes(mes_str,MesgType.MESG_TYPE_INFO);
}
function ReCheckS_SetReV()
{
	
	var obj;
	var obj_name;
	var channel;
	var mes_str ;
	for(var i=0; i < 20; i++)
	{
		if(recheck_chennel_enable[i] < 1)
		{
			continue;
		}
		channel = i+1;
		mes_str = "设置通道电阻值  通道"+channel +": ";
		obj_name = "#RChannel"+channel+"_input";
		obj = $(obj_name);
		channelre_v[i] = obj.val()*1;
		mes_str += obj.val();
		ShowCommMes(mes_str,MesgType.MESG_TYPE_INFO);
	}
}
function Test()
{
	recheck_recive_buf = "[89,116,3,0,3,1,0,4,11]";
	ReCheckComm_Recive(recheck_recive_buf);
}
async function ReCheck_cmd(cmd)
{
	switch(cmd)
	{
		case 0:ReCheckMode(0);break;
		case 1:ReCheckMode(1);break;
		case 10:await ReCheck_LoadFirmwareFile();break;
		case 11:await ReCheck_UpdateApp(); break;
	}
	if(recheck_oper.run_mode == 1)
	{
		switch(cmd)
		{
			case 2: await ReCheckM_ReadAmendV();break;
			case 3: await ReCheckM_WriteAmendV();break;
			case 4: await ReCheckM_Amend();break;
			case 5: await ReCheckM_Measure();break;
			case 6: await ReCheckM_RVersion();break;
			default: break;
		}
			
	}else{
		
		switch(cmd)
		{
			case 7:ReCheckS_SetAmendV(); break;
			case 8:ReCheckS_SetReV();break;
			case 9:ReCheckS_SetVersion();break;
		}
	}
	
}

function ReCheck_TestClear()
{
	var obj = $("#recheck_receara");
	obj.val("");
}
function ReCheck_TestSend()
{
	var obj = $("#recheck_receara");
	recheck_recive_buf = obj.val();
	ReCheckComm_Recive(recheck_recive_buf);
}


/***********************在线更新程序************************************/





 async function ReCheck_LoadFirmwareFile()
{
	if( typeof(recheck_qthandle) != "undefined"  && recheck_qthandle != null )
	{
	   recheck_qthandle.GetFirmWare();
	   recheck_excuedcmd = 0;
	   var rtv = await ReCheck_WaitCmdExcu(0xFFFE,10000000);
	   ShowCommMes("选择固件文件",MesgType.MESG_TYPE_INFO);
		if(rtv == 0){
			recheck_oper.update_firmware = recheck_updata_rtvinfo;
			$("#recheck_firmware_file").val(recheck_oper.update_firmware);
		}
	}

}

async function ReCheck_JumpLoader()
{
	var cmdb_buf =[0xFE,0x01,0x00];
	//读 0x08 03 F0 00 4字节
	var cmdtest_buf = [0xFB,0x01,0x00,0x00,0xF8,0x03,0x08,0x04,0x00]
	var try_count = 3;
	cmdb_buf = ZhangXingCode(cmdb_buf);
	cmdtest_buf = ZhangXingCode(cmdtest_buf);
	while(try_count > 0)
	{
		ReCheckSendData(cmdb_buf);
		await  TransSleep(500);
		ShowCommMes("等待程序复位",MesgType.MESG_TYPE_INFO);
		ReCheckSendData(cmdtest_buf);
		recheck_excuedcmd = 0;
		var rtv = await ReCheck_WaitCmdExcu(0xFB,1000)
		if(rtv == 0)
		{
			break;
		}
		try_count--;
	}
	if(try_count == 0)
	{
		ShowCommMes("复位失败",MesgType.MESG_TYPE_INFO);
		return 1;
	}
	
	ShowCommMes("等待更新程序",MesgType.MESG_TYPE_INFO);
	return 0;
}
async function ReCheck_CleanProgram()
{
	var cmdb_buf =[0xFD,0x01,0x00];
	var index = cmdb_buf.length;
	for(var i = 0; i < 4; i++)
	{
		cmdb_buf[index] = (recheck_app_addr>> (i*8)) & 0x0FF;
		index++;
	}
	for(var i = 0; i < 4; i++)
	{
		cmdb_buf[index] = (recheck_oper.firmware_size >> (i*8)) & 0x0FF;
		index++;
	}
	cmdb_buf = ZhangXingCode(cmdb_buf);
	ReCheckSendData(cmdb_buf);
	recheck_excuedcmd = 0;
	var rtv = await ReCheck_WaitCmdExcu(0xFD,2000)
	if(rtv != 0)
	{
		ShowCommMes("程序擦除失败",MesgType.MESG_TYPE_ERROR);
		return 1;
	}
	ShowCommMes("程序擦除",MesgType.MESG_TYPE_INFO);
	return 0;
	
	
}
async function ReCheck_RunApp()
{
	var cmdb_buf =[0xF9,0x01,0x00];
	cmdb_buf = ZhangXingCode(cmdb_buf);
	ReCheckSendData(cmdb_buf);
	await  TransSleep(500); 
	ShowCommMes("跳转到用户程序",MesgType.MESG_TYPE_INFO);
}

async function ReCheck_CheckRunApp()
{
	var crccmd_buf =[0xFA,0x01,0x00];
	var index = crccmd_buf.length;
	for(var i = 0; i < 4; i++)
	{
		crccmd_buf[index] = (recheck_app_addr>> (i*8)) & 0x0FF;
		index++;
	}
	for(var i = 0; i < 4; i++)
	{
		crccmd_buf[index] = (recheck_oper.firmware_size >> (i*8)) & 0x0FF;
		index++;
	}
	for(var i = 0; i < 4; i++)
	{
		crccmd_buf[index] = (recheck_oper.firmware_crc >> (i*8)) & 0x0FF;
		index++;
	}
	crccmd_buf = ZhangXingCode(crccmd_buf);
	ReCheckSendData(crccmd_buf);
	recheck_excuedcmd = 0;
	var rtv = await ReCheck_WaitCmdExcu(0xFA,3000)
	if(rtv != 0)
	{
		ShowCommMes("代码校验失败",MesgType.MESG_TYPE_ERROR);
		return ;
	}
	await ReCheck_RunApp();
}
async function ReCheck_WriteFlash_Program(regaddr,write_data)
{
	var cmdbuf = [0xFC,0x01,0x00];
	var len = write_data.length;
	for(var i = 0; i < 4; i++)
	{
		cmdbuf[i+3] = (regaddr >> (i*8)) & 0x0FF;
	}
	for(var i = 0; i < 2; i++)
	{
		cmdbuf[i+7] = (len >> (i*8)) & 0x0FF;
	}
	for(var i = 0,j =cmdbuf.length ; i <len ; i++,j++)
	{
		cmdbuf[j] = write_data[i];
	}
	
	
	cmdbuf = ZhangXingCode(cmdbuf);
	ReCheckSendData(cmdbuf);
	recheck_excuedcmd = 0;
	var rtv = await ReCheck_WaitCmdExcu(0xFC,1000)
	var mes = "地址： "+regaddr +" 写入数据长度： "+len;
	return rtv;
}
async function ReCheck_UpdateApp()
{
	var file_size = 0;
	var file_info = [];
	var check_sum  = 0;
	if(typeof(recheck_qthandle) == "undefined"  || recheck_qthandle == null)
	{
		return ;
	}
	
	recheck_excuedcmd = 0;
	recheck_oper.update_firmware = $("#recheck_firmware_file").val();
	recheck_qthandle.OpenFirmWare(recheck_oper.update_firmware);
	var rtv = await ReCheck_WaitCmdExcu(0xFFFE,10000);
	if(rtv != 0){
		ShowCommMes("文件打开失败",MesgType.MESG_TYPE_ERROR);
	}
	console.log("open file "+recheck_updata_rtvinfo);
	file_info =  JSON.parse(recheck_updata_rtvinfo);
	if(file_info.length != 8)
	{
		ShowCommMes("文件打开失败",MesgType.MESG_TYPE_ERROR)
	}
	recheck_oper.firmware_crc =  0;
	for(var i = 0; i < 4 ; i++)
	{
		recheck_oper.firmware_crc += (file_info[i] & 0x0FF) << (i*8)
	}
	recheck_oper.firmware_size = 0;
	for(var i = 0; i < 4 ; i++)
	{
		recheck_oper.firmware_size += (file_info[i+4] & 0x0FF) << (i*8)
	}
	ShowCommMes(epro1_oper.update_firmware + " 文件大小： "+recheck_oper.firmware_size +"  CRC " + recheck_oper.firmware_crc);

	rtv = await ReCheck_JumpLoader();
	if(rtv != 0)
	{
		return;
	}

	rtv = await ReCheck_CleanProgram();
	if(rtv != 0)
	{
		return;
	}
	await  TransSleep(100);
	ShowCommMes("开始编程");
	var write_len = 0;
	var count = 0;
	var regaddr = recheck_app_addr;
	var file_size = recheck_oper.firmware_size;
	var write_data ;
	while(count < file_size)
	{
		if((count+128) < file_size)
		{
			write_len = 128;
		}else{
			write_len = file_size - count;
		}
		
		

		recheck_excuedcmd = 0;
		recheck_qthandle.ReadFirmWare(count,  write_len);
	   var rtv = await ReCheck_WaitCmdExcu(0xFFFE,10000);
		if(rtv != 0){
			ShowCommMes("读取文件数据失败",MesgType.MESG_TYPE_ERROR);
			return ;
		}
		write_data = JSON.parse(recheck_updata_rtvinfo);
			
		
		rtv = await ReCheck_WriteFlash_Program(regaddr,write_data);
		if(rtv != 0)
		{
			ShowCommMes("地址: "+regaddr.toString(16)+" 写入长度 "+ write_len +" 失败",MesgType.MESG_TYPE_ERROR);
			return;
		}else{
			ShowCommMes("地址: "+regaddr.toString(16)+" 写入长度 "+ write_len +" 成功",MesgType.MESG_TYPE_INFO);
		}
		count += write_len;
		regaddr+= write_len;
	}
	ShowCommMes("程序加载完毕。。",MesgType.MESG_TYPE_INFO);
	
	await ReCheck_CheckRunApp();
	
}

/*********************************************************************/