var eproj_oper = {
	cmd: 0,
	comm_port: "",
	comm_baud: "",
	comm_parity: -1,
	comm_stopbit: -1,
	uid: "581900230A00801",
	pwd: "85 AC AD AA",
	delayfire: -1,
	check_voltage: 25,
	h_voltage: 85,
	m_voltage: 75,
	commaddr: 0,
	boom_endaddr: 420,
	boom_bgaddr: 21,
	uidscan_count: 400,
	uidscan_netbeg: 21,
	amend_netbeg:21,
	amend_count:400,
	otp_r_bgaddr: 0, //0x3C00,
	otp_r_len: 8,
	otp_w_addr: 0, //0x3C00,
	otp_w_data: "",
	sys_states: "",
	firmware: "",
	inspecttype: 16,
	run_cfg_value: "",
	cycle_bgaddr: 0,
	cycle_endaddr: 500,
	cycle_group_count: 100,
	cycle_bgdelay: 0,
	cycle_stepdelay: 50,
	cycle_bguid: "581900230A00801",
	cycle_rundelay: 10,
	cycle_runcount: 1,
	cycle_test: 0,
};
var epj_qthandle = null;
var epj_excuedcmd;
var epj_excued_rtv;
var epj_cmd_excue_busy = 0
var epj_recive_buf = [];
var epj_bus_heartbeat = 1;
var epj_log_flag;
var epj_rec_plugin = null;
var epj_dac_voltage = [4.8, 5.3, 5.7, 6.1, 6.6, 7.0, 7.4, 7.9, 8.3, 8.8, 9.2, 9.6, 10.1, 10.5, 11.0, 11.4, 11.8, 12.3,
	12.7, 13.1, 13.6, 14.0, 14.5, 14.9, 15.3, 15.8, 16.2, 16.6, 17.1, 17.5, 18.0, 18.4
];
var epj_wotp_cmd = 23;

function Epj_Start_Comm() {
	console.log("Epj_Start_Comm ");
	if (typeof(epj_qthandle) != "undefined" && epj_qthandle != null) {
		epj_qthandle.SearchCommport();
		console.log("SearchCommport ");
	}
}

function Epj_Open_CommPort(main_serial) {
	eproj_oper.comm_port = main_serial.comm_port;
	eproj_oper.comm_baud = main_serial.comm_baud;
	eproj_oper.comm_parity = main_serial.comm_parity;
	eproj_oper.comm_stopbit = main_serial.comm_stopbit;

	Main_Close_CommPort_Select();
	var json_str = JSON.stringify(eproj_oper);
	console.log(json_str);
	if (typeof(epj_qthandle) != "undefined" && epj_qthandle != null) {
		epj_qthandle.OpenCommPort(json_str);
	}
}
async function epj_cmd(cmd) {
	if (epj_cmd_excue_busy > 0) {
		ShowCommMes("正在执行命令", MesgType.MESG_TYPE_INFO);
		return;
	}
	epj_cmd_excue_busy = 1;
	eproj_oper.cmd = cmd;
	eproj_oper.uid = $("#epj_uid").val();
	eproj_oper.pwd = $("#epj_pwd").val();
	eproj_oper.delayfire = parseInt($("#epj_delayfire").val(), 10);
	eproj_oper.commaddr = parseInt($("#epj_commaddr").val(), 10);
	eproj_oper.otp_r_bgaddr = parseInt($("#epj_otp_r_bgaddr").val(), 10);
	eproj_oper.otp_r_len = parseInt($("#epj_otp_r_len").val(), 10);
	eproj_oper.otp_w_addr = parseInt($("#epj_otp_w_addr").val(), 10);
	eproj_oper.otp_w_data = $("#epj_otp_w_data").val();
	eproj_oper.auset_addr_fac = parseInt($("#epj_auset_addr_fac").val(), 10);
	eproj_oper.run_cfg_value = $("#epj_run_cfg_value").val();
	eproj_oper.cycle_bgaddr = parseInt($("#epj_cycle_bgaddr").val(), 10);
	eproj_oper.cycle_endaddr = parseInt($("#epj_cycle_endaddr").val(), 10);
	eproj_oper.cycle_group_count = parseInt($("#epj_cycle_group_count").val(), 10);
	eproj_oper.cycle_bgdelay = parseInt($("#epj_cycle_bgdelay").val(), 10);
	eproj_oper.cycle_stepdelay = parseInt($("#epj_cycle_stepdelay").val(), 10);
	eproj_oper.cycle_bguid = $("#epj_cycle_bguid").val();
	eproj_oper.cycle_rundelay = parseInt($("#epj_cycle_rundelay").val(), 10);
	eproj_oper.cycle_runcount = parseInt($("#epj_cycle_runcount").val(), 10);
	eproj_oper.h_voltage = parseInt($("#epj_h_voltage").val(), 10);
	eproj_oper.m_voltage = parseInt($("#epj_m_voltage").val(), 10);
	eproj_oper.uidscan_netbeg = parseInt($("#epj_uidscan_netbeg").val(), 10);
	eproj_oper.uidscan_count = parseInt($("#epj_uidscan_count").val(), 10);
	eproj_oper.boom_bgaddr = parseInt($("#epj_boom_bgaddr").val(), 10);
	eproj_oper.boom_endaddr = parseInt($("#epj_boom_endaddr").val(), 10);
	eproj_oper.check_voltage = parseInt($("#epj_check_voltage").val(), 10);
	eproj_oper.amend_count = parseInt($("#epj_amend_count").val(), 10);
	eproj_oper.amend_netbeg = parseInt($("#epj_amend_netbeg").val(), 10);
	//eproj_oper.inspecttype =  parseInt($("#epj_inspecttype").val(), 16);
	await Epj_ExcueOperCmd();
	epj_cmd_excue_busy = 0;

}

function EPj_ShowDef() {
	$("#epj_uid").val(eproj_oper.uid);
	$("#epj_pwd").val(eproj_oper.pwd);
	if (eproj_oper.delayfire >= 0 && eproj_oper.delayfire < 30000) {
		$("#epj_delayfire").val(eproj_oper.delayfire);
	} else {
		$("#epj_delayfire").val('');
	}

	$("#epj_commaddr").val(eproj_oper.commaddr);
	$("#epj_otp_r_bgaddr").val(eproj_oper.otp_r_bgaddr);
	$("#epj_otp_r_len").val(eproj_oper.otp_r_len);
	$("#epj_otp_w_addr").val(eproj_oper.otp_w_addr);
	$("#epj_otp_w_data").val(eproj_oper.otp_w_data);
	$("#epj_auset_addr_fac").val(eproj_oper.auset_addr_fac);
	$("#epj_run_cfg_value").val(eproj_oper.run_cfg_value);
	$("#epj_cycle_bgaddr").val(eproj_oper.cycle_bgaddr);
	$("#epj_cycle_endaddr").val(eproj_oper.cycle_endaddr);

	$("#epj_cycle_group_count").val(eproj_oper.cycle_group_count);
	$("#epj_cycle_bgdelay").val(eproj_oper.cycle_bgdelay);
	$("#epj_cycle_stepdelay").val(eproj_oper.cycle_stepdelay);
	$("#epj_cycle_bguid").val(eproj_oper.cycle_bguid);
	$("#epj_cycle_rundelay").val(eproj_oper.cycle_rundelay);
	$("#epj_cycle_runcount").val(eproj_oper.cycle_runcount);

	$("#epj_sys_amend").val(eproj_oper.amend_v);
	$("#epj_sys_states").val(eproj_oper.sys_states);
	$("#epj_firmware").val(eproj_oper.firmware);

	$("#epj_h_voltage").val(eproj_oper.h_voltage);
	$("#epj_m_voltage").val(eproj_oper.m_voltage);

	$("#epj_uidscan_netbeg").val(eproj_oper.uidscan_netbeg);
	$("#epj_uidscan_count").val(eproj_oper.uidscan_count);

	$("#epj_boom_bgaddr").val(eproj_oper.boom_bgaddr);
	$("#epj_boom_endaddr").val(eproj_oper.boom_endaddr);

	$("#epj_check_voltage").val(eproj_oper.check_voltage);
	$("#epj_amend_count").val(eproj_oper.amend_count);
	$("#epj_amend_netbeg").val(eproj_oper.amend_netbeg);
	//$("#epj_inspecttype").val(Bytes2Str(eproj_oper.inspecttype));
}

function Epj_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 ? ' 启动' : ' 关闭'), data.othis);
			epj_log_flag = 0;
			if (this.checked)
				epj_log_flag = 1;
		}
	});
	form.on('checkbox', function(data) {
		var value = data.value;
		if (value == "epj_cycle_test") {
			layer.tips('循环测试 ' + (this.checked ? '启动' : '关闭'), data.this)
		}

	});
	$("#epj_check_vol_btn").mouseover(function() {
		var dac = parseInt($("#epj_check_voltage").val());
		if (dac > 31) {
			dac = 31;
		} else if (dac >= 0 && dac <= 31) {
			dac = dac;
		} else {
			dac = 0;
		}
		$("#epj_check_voltage").val(dac);
		layer.tips('检测电压 ' + epj_dac_voltage[dac], this);
	});
	var dropdown = layui.dropdown;
	  dropdown.render({
	    elem: '#epj_inspecttype',
	    data: [
		{ title: '充满',id: 0x80 },
		{title: '对码' ,id: 0x40},
		{title: '起爆使能',id: 0x20 },
		{ title: '校准' ,id: 0x02},
		{ title: '在线' ,id: 0x10},
		],
		click: function(obj){
	     // layer.tips('点击了：'+ obj.title, this.elem, {tips: [1, '#5FB878']})
		  $('#epj_inspecttype').text(obj.title);
		  eproj_oper.inspecttype = obj.id;
	    }
	  });
	
	
	if (typeof(qchannel) != "undefined" && qchannel != null) {
		if(epj_qthandle != null && typeof(epj_qthandle) != "undefined")
		{
			console.log("Ep1_Inited  OK ---");
		}else{
			epj_qthandle = qchannel.objects.epj_qthandle;
			if ((typeof(epj_qthandle) != "undefined") && (epj_qthandle != null)) {
				console.log("Epj_Init  OK ---");
				epj_qthandle.ShowCommMes.connect(function(mesg, type) {
					ShowCommMes(mesg, type);
			
				});
				epj_qthandle.ShowCommport.connect(function(jsoninfo) {
					Main_ShowCommport(jsoninfo);
				});
				epj_qthandle.EPJComm_Recive.connect(function(jsoninfo) {
					EPJComm_Recive(jsoninfo);
				});
			
			}
			
		}
	}
	EPj_ShowDef();
	PrapareUIDFeatrue(Str2Bytes(eproj_oper.uid));
	//console.log(Int2HexStr(65536,"0x",0));
}

function Epj_HeartBeat() {

	var bus_volate = epj_recive_buf[5] + (epj_recive_buf[6] << 8);
	bus_volate = bus_volate / 10;
	bus_volate = bus_volate.toFixed(1);


	var bus_current = epj_recive_buf[7] + (epj_recive_buf[8] << 8) + (epj_recive_buf[9] << 16) + (epj_recive_buf[10] <<
		24);
	bus_current = bus_current / 1000;
	bus_current = bus_current.toFixed(2);

	var mbus_volate = epj_recive_buf[11] + (epj_recive_buf[12] << 8);
	mbus_volate = mbus_volate / 10;
	mbus_volate = mbus_volate.toFixed(1);
	var mes = "总线电压 " + bus_volate + "V 中电平电压 " + mbus_volate + "V 总线电流  " + bus_current + "mA"
	ShowCommMes(mes, MesgType.MESG_TYPE_INFO);
}


function EPJComm_Recive(jsoninfo) {
	var frame = JSON.parse(jsoninfo);
	epj_recive_buf = frame;
	epj_excuedcmd = frame[4];
	if(epj_rec_plugin != null)
	{
		eval(plugin_func+"(epj_recive_buf);");
	}
	var frame_str = "Rx: " + Bytes2Str(frame);
	if (epj_excuedcmd == 0xFE) {
		ShowCommMes(frame_str, MesgType.MESG_TYPE_INFO);
		Epj_HeartBeat();
	} else {
		epj_excued_rtv = frame[5];
		ShowCommMes(frame_str, MesgType.MESG_TYPE_INFO, ep1_log_flag);
		BTInfoCount(1);
		if (epj_excued_rtv != 0) {
			BTErrorCount(1);
		}
	}
}

var Epj_Cmd_Head = "ESJ";

function Epj_SendCmd(cmd_buf) {

	var buf = Str2Bytes(Epj_Cmd_Head);
	buf[3] = 0; //数据长度
	buf[4] = eproj_oper.cmd;
	for (var index = 0; index < cmd_buf.length; index++) {
		buf[5 + index] = cmd_buf[index];
	}
	buf[3] = buf.length;
	var json_str = JSON.stringify(buf);
	console.log(json_str);
	if (typeof(epj_qthandle) != "undefined" && epj_qthandle != null) {
		epj_qthandle.Epj_SendData(json_str);
	}
	var mes = "Tx:" + Bytes2Str(buf);
	ShowCommMes(mes, MesgType.MESG_TYPE_INFO);
}
//同步非阻塞延时
async function Epj_WaitCmdExcu(cmd, time_out) {
	if (time_out == 0)
		return 0;
	time_out = parseInt((time_out + 19) / 20);
	epj_excued_rtv = 0;
	while ((time_out > 0) && (epj_excued_rtv < 1)) {
		if (epj_excuedcmd == cmd) {
			break;
		}
		await TransSleep(20);
		time_out--;
	}
	if (epj_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 Epj_WriteOTPCmd(regaddr, w_len, param) {
	var cmd_buf = [];
	cmd_buf[0] = regaddr & 0xFF;
	cmd_buf[1] = w_len;
	for (var index = 0; index < param.length; index++) {
		cmd_buf[2 + index] = param[index];
	}
	Epj_SendCmd(cmd_buf);
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, w_len*125+1000);
	return rtv;

}
async function Epj_ReadOTPCmd(comaddr, regaddr, r_len) {
	var cmd_buf = [];
	cmd_buf[0] = comaddr & 0xFF;
	cmd_buf[1] = (comaddr >> 8) & 0xFF;
	cmd_buf[2] = regaddr & 0xFF;
	cmd_buf[3] = r_len;
	epj_excuedcmd = 0;
	Epj_SendCmd(cmd_buf);
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	var read_buf = [];
	if (rtv == 0) {
		len = epj_recive_buf[3] - 6;
		if (len != r_len)
			return read_buf;
		read_buf = ArrayCopy(epj_recive_buf, read_buf, 6, len);

		//read_buf = [0x35, 0x38, 0x31, 0x39, 0x30, 0x30, 0x32, 0x33];
	}
	return read_buf;
}

async function Epj_PowerSet(power_sw) {
	var cmd_buf = [];
	cmd_buf[0] = power_sw;
	cmd_buf[1] = eproj_oper.h_voltage & 0xFF;
	cmd_buf[2] = (eproj_oper.h_voltage >> 8) & 0xFF;
	cmd_buf[3] = eproj_oper.m_voltage & 0xFF;
	cmd_buf[4] = (eproj_oper.m_voltage >> 8) & 0xFF;
	epj_excuedcmd = 0;
	Epj_SendCmd(cmd_buf);
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 5000);
	var read_buf = [];
	if (rtv == 0) {
		var mes_ = "设置高电平电压 " + eproj_oper.h_voltage + " 中电平电压 " + eproj_oper.m_voltage;
		ShowCommMes(mes_, MesgType.MESG_TYPE_INFO);
	}
	return read_buf;
}


async function Epj_SwitchHeartBeat() {
	var mes_ = ""
	var param = [0];
	if (epj_bus_heartbeat == 0) {
		param[0] = 1;
		epj_bus_heartbeat = 1;
		mes_ = "开心跳";
	} else {

		param[0] = 0;
		epj_bus_heartbeat = 0;
		mes_ = "关心跳";
	}
	epj_excuedcmd = 0;
	Epj_SendCmd(param);
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if (rtv == 0) {
		ShowCommMes(mes_, MesgType.MESG_TYPE_INFO);
	}
	return rtv;
}
async function Epj_ReadChipCod(addr) {
	var param = [];
	var cmd_buf = [];
	cmd_buf[0] = addr & 0xFF;
	cmd_buf[1] = (addr >> 8) & 0xFF;
	epj_excuedcmd = 0;
	Epj_SendCmd(cmd_buf);
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if (rtv == 0) {

		ArrayCopy(epj_recive_buf, param, 7, 2);
		eproj_oper.firmware = Bytes2Str(param);
		var mes = "地址 " + addr + " 芯片代码  " + eproj_oper.firmware;
		ShowCommMes(mes, MesgType.MESG_TYPE_INFO);
		$("#epj_firmware").val(eproj_oper.firmware);
	}
	return rtv;
}
async function Epj_ReadSysStatesCmd(addr) {
	var param = [];
	var cmd_buf = [];
	cmd_buf[0] = addr & 0xFF;
	cmd_buf[1] = (addr >> 8) & 0xFF;
	epj_excuedcmd = 0;
	Epj_SendCmd(cmd_buf);
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if (rtv == 0) {
		ArrayCopy(epj_recive_buf, param, 6, 1);
		eproj_oper.sys_states = Bytes2Str(param);
		var mes = "地址 " + addr + " 状态码  " + eproj_oper.sys_states;
		ShowCommMes(mes, MesgType.MESG_TYPE_INFO);
		$("#epj_sys_states").val(eproj_oper.sys_states);
	}
	return rtv;
}



async function Epj_ClkAmend(delaytime) {
	var cmd_buf = [];
	cmd_buf[0] = delaytime & 0xFF;
	cmd_buf[1] = (delaytime >> 8) & 0xFF;
	epj_excuedcmd = 0;
	Epj_SendCmd(cmd_buf);
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	return rtv;
}
/*
 * type 巡检类型 0 起爆巡检 1 在线巡检
 */
function Epj_InspectAnsy(databuf) {
	var ok_addr_buf = [];
	var index = 0;
	var ok_index = 0;
	while (index < databuf.length) {
		var data = databuf[index]; //？待定
		index++;
		for (var i = 0; i < 8; i++) {
			if ((data & 0x80) > 0) {
				ok_addr_buf[ok_index++] = 1;
			} else {
				ok_addr_buf[ok_index++] = 0;
			}
			data <<= 1;
		}
	}
	return ok_addr_buf;
}

async function Epj_Charge() {
	var cmd_buf = [];
	var bgaddr = eproj_oper.cycle_bgaddr;
	var endaddr = eproj_oper.cycle_endaddr;
	var group = eproj_oper.cycle_group_count;
	while (endaddr > bgaddr) {
		endaddr -= group;
		if(endaddr < 0)
			endaddr = 0;
		cmd_buf[0] = (endaddr & 0x0FF);
		cmd_buf[1] = (endaddr >> 8 & 0x0FF);
		Epj_SendCmd(cmd_buf);
		epj_excuedcmd = 0;
		var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 2000);
		if (rtv != 0) {
			return;
		}
		await TransSleep(eproj_oper.cycle_rundelay);
	}
	ShowCommMes("执行充电成功", MesgType.MESG_TYPE_INFO);
}
async function Epj_EnOTPWrite() {
	var cmd_buf = [];
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if (rtv == 0) {
		ShowCommMes("OTP写使能成功", MesgType.MESG_TYPE_INFO);
	}
}
async function Epj_DisCharge() {
	var cmd_buf = [];
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if (rtv == 0) {
		ShowCommMes("执行放电成功", MesgType.MESG_TYPE_INFO);
	}
}
async function Epj_EnBoom() {
	var cmd_buf = [];
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if (rtv == 0) {
		ShowCommMes("执行起爆使能成功", MesgType.MESG_TYPE_INFO);
	}

}
async function Epj_Reset() {
	var cmd_buf = [];
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if (rtv == 0) {
		ShowCommMes("芯片复位执行成功", MesgType.MESG_TYPE_INFO);
	}
}
async function Epj_Boom() {
	var cmd_buf = [];
	cmd_buf[0] = 1;
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1500);
	if (rtv == 0) {
		ShowCommMes("起爆成功", MesgType.MESG_TYPE_INFO);
	}
}
async function Epj_CheckSelfCmd(voltage) {
	var cmd_buf = [];
	cmd_buf[0] = voltage;
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1500);
	if (rtv == 0) {
		ShowCommMes("检测DAC设置成功", MesgType.MESG_TYPE_INFO);
	}
}

async function Epj_BoomInspect(bgaddr, endaddr) {
	var cmd_buf = [];
	cmd_buf[0] = 2;
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1500);
	if (rtv != 0) {
		ShowCommMes("巡检起爆失败", MesgType.MESG_TYPE_INFO);
	}
	var databuf = [];
	ArrayCopy(epj_recive_buf, databuf, 6, databuf.length - 6);
	var ok_buf = Epj_InspectAnsy(databuf);
	var ok_mes = "";
	var ng_mes = "";
	endaddr -= 2;
	bgaddr -= 1;
	for (var i = bgaddr; i < endaddr; i++) {
		if (ok_buf[i] > 0) {
			ok_mes += (i+2) + " ";
		} else {
			ng_mes +=  (i+2) + " ";
		}
	}
	if (ok_mes.length > 1) {
		ok_mes = "起爆成功： " + ok_mes;
		ShowCommMes(ok_mes, MesgType.MESG_TYPE_INFO);
	}
	if (ng_mes.length > 1) {
		ng_mes = "起爆失败： " + ng_mes;
		ShowCommMes(ng_mes, MesgType.MESG_TYPE_ERROR);
	}

}
async function Epj_Inspect_Task(type,netbgn,count) {
	
	var cmd_buf=[];
	var maxcount = count+netbgn+1;
	cmd_buf[0] = type;
	cmd_buf[1] =  (maxcount & 0x0FF);
	cmd_buf[2] =  ((maxcount >> 8) & 0x0FF);
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1500);
	if(rtv != 0)
	{
		ShowCommMes("巡检命令执行失败", MesgType.MESG_TYPE_ERROR);
		return;
	}
	var databuf = [];
	ArrayCopy(epj_recive_buf, databuf, 6, databuf.length - 6);
	var ok_buf = Epj_InspectAnsy(databuf);
	var ok_mes = "";
	var ng_mes = "";
	for (var i = netbgn;  i < ok_buf.length; i++) {
		if (ok_buf[i] > 0) {
			ok_mes += i + " ";
		} else {
			ng_mes += i + " ";
		}
	}
	if (ok_mes.length > 1) {
		ok_mes = "巡检成功： " + ok_mes;
		ShowCommMes(ok_mes, MesgType.MESG_TYPE_INFO);
	}
	if (ng_mes.length > 1) {
		ng_mes = "巡检失败： " + ng_mes;
		ShowCommMes(ng_mes, MesgType.MESG_TYPE_ERROR);
	}
}


async function Epj_ReadUID() {
	var cmd_buf = await Epj_ReadOTPCmd(eproj_oper.commaddr, 3, 8);
	if (cmd_buf.length != 8) {
		ShowCommMes("读UID失败", MesgType.MESG_TYPE_ERROR);
	} else {
		
		var rtv = UidCode_JQ2YM(cmd_buf);
		if (!rtv.rtv) {
			ShowCommMes("读UID失败", MesgType.MESG_TYPE_ERROR);
			return;
		}
		eproj_oper.uid = rtv.uid;
		var mes = "地址 " + eproj_oper.commaddr + "  UID：" + eproj_oper.uid;
		ShowCommMes(mes, MesgType.MESG_TYPE_INFO);
		$("#epj_uid").val(eproj_oper.uid);
	}
}
async function Epj_WriteUID() {
	var cmd_buf = Str2Bytes(eproj_oper.uid);
	cmd_buf = UidCode_YM2JQ(cmd_buf);
	//var rtv = UidCode_JQ2YM(cmd_buf);
	eproj_oper.cmd = epj_wotp_cmd;
	var rtv = await Epj_WriteOTPCmd(3, 8, cmd_buf)
	if (rtv == 0) {
		ShowCommMes("写UID成功", MesgType.MESG_TYPE_INFO);
	} else {
		ShowCommMes("写UID失败", MesgType.MESG_TYPE_ERROR);
	}
}
async function Epj_ReadPWD() {
	cmd_buf = await Epj_ReadOTPCmd(eproj_oper.commaddr, 11, 4);
	if (cmd_buf.length != 4) {
		ShowCommMes("读PWD失败", MesgType.MESG_TYPE_ERROR);
	} else {
		eproj_oper.pwd = Bytes2Str(cmd_buf);
		var mes = "地址 " + eproj_oper.commaddr + "  PWD：" + eproj_oper.pwd;
		ShowCommMes(mes, MesgType.MESG_TYPE_INFO);
		$("#epj_pwd").val(eproj_oper.pwd);
	}
}
async function Epj_WritePWD() {
	var cmd_buf;
	if (eproj_oper.pwd.length < 4) {
		cmd_buf = Str2Bytes(eproj_oper.uid);
		cmd_buf = Epj_Uid2Pwd(cmd_buf);
	} else {

		cmd_buf = HexString2ByteArray(eproj_oper.pwd);
		if (cmd_buf.length != 4) {
			ShowCommMes("密码数据错误", MesgType.MESG_TYPE_ERROR);
			return;
		}
	}
	eproj_oper.cmd = epj_wotp_cmd;
	var rtv = await Epj_WriteOTPCmd(11, 4, cmd_buf)
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if (rtv == 0) {
		ShowCommMes("写密码成功", MesgType.MESG_TYPE_INFO);
	}else{
		ShowCommMes("写密码失败", MesgType.MESG_TYPE_ERROR);
	}

}
async function Epj_VerifiPwdCmd(type,infodata) 
{
	var cmd_buf = [];
	if(type == 1)
	{
		cmd_buf = Str2Bytes(infodata);
		cmd_buf = Epj_Uid2Pwd(cmd_buf);
	}else {
		cmd_buf =  HexString2ByteArray(infodata);
		if(cmd_buf.length != 4)
		{
			ShowCommMes("密码数据错误", MesgType.MESG_TYPE_ERROR);
			return;
		}
	}
	epj_excuedcmd = 0;
	Epj_SendCmd(cmd_buf);
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if(rtv == 0)
	{
		ShowCommMes("密码对比执行成功", MesgType.MESG_TYPE_INFO);
	}else{
		ShowCommMes("密码对比执行失败", MesgType.MESG_TYPE_INFO);
	}
	return rtv;
}
async function Epj_ScanUID(bgaddr, count) {
	var cmd_buf = [];
	cmd_buf[0] = bgaddr & 0xFF;
	cmd_buf[1] = (bgaddr >> 8) & 0xFF;
	cmd_buf[2] = count & 0xFF;
	cmd_buf[3] = (count >> 8) & 0xFF;
	Epj_SendCmd(cmd_buf);
	var commaddr = 0;
	while (true) {
		epj_excuedcmd = 0;
		var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 2000);
		if (rtv != 0) {
			ShowCommMes("扫描结束", MesgType.MESG_TYPE_INFO);
			return;
		}
		rtv = epj_recive_buf[6];
		if (rtv == 1) {
			ShowCommMes("扫描结束", MesgType.MESG_TYPE_INFO);
			return;
		}
		var param = [];
		var param2 = [];
		commaddr = (epj_recive_buf[8] << 8) | epj_recive_buf[7];
		ArrayCopy(epj_recive_buf, param, 9, 8);

		for (var i = 0; i < param.length; i++) {
			param2[i] = param[param.length - 1 - i];
		}
		rtv = UidCode_JQ2YM(param2);
		var mes = "地址： " + commaddr;
		if (!rtv.rtv) {
			mes += " 解析失败";
			ShowCommMes(mes, MesgType.MESG_TYPE_ERROR);
		} else {
			mes += " UID " + rtv.uid;
			eproj_oper.uid = rtv.uid;
			ShowCommMes(mes, MesgType.MESG_TYPE_INFO);
		}

	}

}

async function Epj_WritOTPTask() {
	var w_data = HexString2ByteArray(eproj_oper.otp_w_data);
	await Epj_WriteOTPCmd(eproj_oper.commaddr, eproj_oper.otp_w_addr, w_data.length, w_data);

}

async function Epj_ReadDelay() {
	var cmd_buf = [];
	cmd_buf[0] = eproj_oper.commaddr & 0x0FF;
	cmd_buf[1] = (eproj_oper.commaddr >> 8) & 0x0FF;
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if (rtv == 0) {
		var delay_ = epj_recive_buf[7];
		delay_ <<= 8;
		delay_ |= epj_recive_buf[6];
		mes = "地址：" + eproj_oper.commaddr + " 延时为 " + delay_;
		$("#epj_delayfire").val(delay_);
		eproj_oper.delayfire = delay_;
		ShowCommMes(mes, MesgType.MESG_TYPE_INFO);
	}
}
async function Epj_WriteDelay() {
	var cmd_buf = [];
	cmd_buf[0] = eproj_oper.commaddr & 0x0FF;
	cmd_buf[1] = (eproj_oper.commaddr >> 8) & 0x0FF;
	cmd_buf[2] = eproj_oper.delayfire & 0x0FF;
	cmd_buf[3] = (eproj_oper.delayfire >> 8) & 0x0FF;
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if (rtv == 0) {
		ShowCommMes("地址 " + eproj_oper.commaddr + " 延时设置成功", MesgType.MESG_TYPE_INFO);
	}
}
async function Epj_ReadFirmwareCmd(addr)
{
	var cmd_buf = [];
	cmd_buf[0] = addr & 0x0FF;
	cmd_buf[1] = (addr >> 8) & 0x0FF;
	Epj_SendCmd(cmd_buf);
	epj_excuedcmd = 0;
	var rtv = await Epj_WaitCmdExcu(eproj_oper.cmd, 1000);
	if (rtv == 0) {
		var param = [];
		param[0]  = epj_recive_buf[6];
		param[1]  = epj_recive_buf[7];
		eproj_oper.firmware = Bytes2Str(param);
		$("#epj_firmware").val(eproj_oper.firmware);
		ShowCommMes("地址 " + eproj_oper.commaddr + " 芯片ID "+ eproj_oper.firmware, MesgType.MESG_TYPE_INFO);
	}else{
		
		ShowCommMes("地址 " + eproj_oper.commaddr + " 读芯片ID失败 " , MesgType.MESG_TYPE_ERROR);
	}
}
function Epj_Beep(delay_h,delay_m,count)
{
	var cmd_buf = [delay_h&0x0FF,(delay_h>>8)&0x0FF,delay_m&0x0FF,(delay_m>>8)&0x0FF,count&0x0FF,(count>>8)&0x0FF];
	Epj_SendCmd(cmd_buf);
	
}
async function Epj_ExcueOperCmd() {
	switch (eproj_oper.cmd) {
		case 1:
			await Epj_Charge();
			break;
		case 2:
			await Epj_DisCharge();
			break;
		case 3:
			await Epj_EnBoom();
			break;
		case 4:
			await Epj_Reset();
			break;
		case 5:
			await Epj_Boom();
			break;
		case 6:
			await Epj_CheckSelfCmd(eproj_oper.check_voltage);
			break;
		case 7:
			await Epj_SwitchHeartBeat();
			break;
		case 8:
			await Epj_BoomInspect(eproj_oper.boom_bgaddr, eproj_oper.boom_endaddr);
			break;
		case 9:
			await Epj_PowerSet(1);
			break;
		case 10:
			await Epj_PowerSet(0);
			break;
		case 11:
			eproj_oper.cmd = 21;
			await Epj_ReadUID();
			break;
		case 12:
			await Epj_WriteUID();
			break;
		case 13:
			eproj_oper.cmd = 21;
			await Epj_ReadPWD();
			break;
		case 14:
			await Epj_WritePWD();
			break;
		case 15:
			await Epj_VerifiPwdCmd(1,eproj_oper.uid);
			break;
		case 16:
			await Epj_VerifiPwdCmd(2,eproj_oper.pwd);
			break;
		case 17:
			await  Epj_ScanUID(eproj_oper.commaddr, 1);
			break;
		case 18:
			await Epj_ClkAmend(eproj_oper.delayfire);
			break;
		case 19:
			await Epj_ReadDelay();
			break;
		case 20:
			await Epj_WriteDelay();
			break;
		case 21:
			await Epj_ReadOTPCmd(eproj_oper.commaddr, eproj_oper.otp_r_bgaddr, eproj_oper.otp_r_len);
			break;
		case 22:
			await Epj_EnOTPWrite();
			break;
		case 23:
			await Epj_WritOTPTask();
			break;
		case 24:
			await Epj_ReadSysStatesCmd(eproj_oper.commaddr);
			break;
		case 25:
			await Epj_ReadFirmwareCmd(eproj_oper.commaddr);
			break;
		case 26:
			await Epj_Inspect_Task(eproj_oper.inspecttype,eproj_oper.amend_netbeg,eproj_oper.amend_count);
			break;
		case 27:
			await Epj_ScanUID(eproj_oper.uidscan_netbeg, eproj_oper.uidscan_count);
			break;


	}

}





//JQuid 转 YM uid
function UidCode_JQ2YM(u8ArrayJQUidCode) {
	var i = 0;
	var u16Year = 0;
	var u16Month = 0;
	var u8Day = 0;
	var u16Date = 0;
	var ucArrayYMUidCode = [];
	if (u8ArrayJQUidCode[7] != Epj_CalcCrc8(u8ArrayJQUidCode, 7))
		return false;

	ucArrayYMUidCode[0] = parseInt(u8ArrayJQUidCode[0] / 10) + 0x30;
	ucArrayYMUidCode[1] = u8ArrayJQUidCode[1] % 10 + 0x30;
	ucArrayYMUidCode[4] = u8ArrayJQUidCode[1]; //特征码
	if(ucArrayYMUidCode[4] == 0 )
	{
		ucArrayYMUidCode[4]  = 0x30;
	}
	u16Date = u8ArrayJQUidCode[2];
	u16Date = u16Date << 8 | u8ArrayJQUidCode[3];

	u16Year = (u16Date >> 9) & 0x7F;
	u16Month = (u16Date >> 5) & 0x0F;
	u8Day = u16Date & 0x1F;

	ucArrayYMUidCode[2] = parseInt(u16Year / 10) + 0x30;
	ucArrayYMUidCode[3] = u16Year % 10 + 0x30;

	ucArrayYMUidCode[5] = parseInt(u16Month / 10) + 0x30;
	ucArrayYMUidCode[6] = u16Month % 10 + 0x30;

	ucArrayYMUidCode[7] = parseInt(u8Day / 10) + 0x30;
	ucArrayYMUidCode[8] = u8Day % 10 + 0x30;
	
	var stSerialCode = 0;
	stSerialCode |= (u8ArrayJQUidCode[4] << 8) & 0x00FF00;
	stSerialCode |= (u8ArrayJQUidCode[5] << 16) & 0xFF0000;
	stSerialCode |= (u8ArrayJQUidCode[6] << 24) & 0xFF000000;

	stSerialCode = UINT32_ENDIAN_CHANGE(stSerialCode);

	ucArrayYMUidCode[9] = (stSerialCode >> 17) & 0x7F;
	if(ucArrayYMUidCode[9] == 0)
	{
		ucArrayYMUidCode[9] = 0x30;
	}
	stSerialCode = stSerialCode & 0x1FFFF;

	for (i = 14; i >= 10; i--) {
		ucArrayYMUidCode[i] = (stSerialCode % 10 + 0x30);
			stSerialCode = parseInt(stSerialCode / 10);

	}

	var rtv = {
		uid: "",
		rtv: false,

	}
	if (!YMUid_Valid_check(ucArrayYMUidCode))
		return rtv;
	rtv.uid = String.fromCharCode.apply(null, ucArrayYMUidCode);
	rtv.rtv = true;
	return rtv;
}



function UidCode_JQ2YM_NoBackwardSequence(u8ArrayJQUidCode)
{
	var i=0;
	var ucYear = 0;
	var ucMonth = 0;
	var ucDay = 0;
	var ulNumber =0;
	var ucTeZhengMa = 0;
	var usBoxCode = 0;
	var ucIndexCode = 0;
	var len = 16;
	var ucArrayYMUidCode = [];
	var result = {
		
		uid:"",
		rtv:false,
		
	};
	if(u8ArrayJQUidCode[7] != Epj_CalcCrc8(u8ArrayJQUidCode,7))
	{
		return result;
	}
	var temp_array = [];
	temp_array[0] = u8ArrayJQUidCode[0];
	temp_array[1] = u8ArrayJQUidCode[1];
	temp_array = Bytes2ChartArray(temp_array);
	
	for(var index_ = 0; index_ < 4; index_++)
	{
		ucArrayYMUidCode[index_] = parseInt(temp_array[index_].charCodeAt(0),10);
	}
	
	ucArrayYMUidCode[4] = ucArrayYMUidCode[2];
	ucArrayYMUidCode[5] = ucArrayYMUidCode[3];
	
	
	ucYear = u8ArrayJQUidCode[2]>>1;
	ucArrayYMUidCode[2] = parseInt(ucYear/10)+0x30;
	ucArrayYMUidCode[3] = parseInt(ucYear%10)+0x30;
	
	ucMonth = ((u8ArrayJQUidCode[2]&0x01)<<3)|((u8ArrayJQUidCode[3]>>5));
	ucArrayYMUidCode[6] = parseInt(ucMonth/10)+0x30;
	ucArrayYMUidCode[7] = parseInt(ucMonth%10)+0x30;
	
	ucDay = (u8ArrayJQUidCode[3]&0x1F);
	ucArrayYMUidCode[8] = parseInt(ucDay/10)+0x30;
	ucArrayYMUidCode[9] = parseInt(ucDay%10)+0x30;
	
	ulNumber = u8ArrayJQUidCode[4];
	ulNumber <<= 8;
	ulNumber |= u8ArrayJQUidCode[5];
	ulNumber <<= 8;
	ulNumber |= u8ArrayJQUidCode[6];
	
	ucTeZhengMa = parseInt(ulNumber/100000);
	ucArrayYMUidCode[10] = ucTeZhengMa;
	
	ulNumber = parseInt(ulNumber%100000);
	usBoxCode = parseInt( ulNumber/100);
	ucArrayYMUidCode[11] = parseInt(usBoxCode/100) + 0x30;
	usBoxCode%=100;
	ucArrayYMUidCode[12] = parseInt(usBoxCode/10)+0x30;
	ucArrayYMUidCode[13] =parseInt( usBoxCode%10) + 0x30;
	
	ucIndexCode = parseInt(ulNumber%100);
	ucArrayYMUidCode[14] =parseInt(ucIndexCode/10)+0x30;
	ucArrayYMUidCode[15] =parseInt(ucIndexCode%10) + 0x30;
		
	if(!YMUid_Valid_check_2(ucArrayYMUidCode))
	{
		return result;
	}
	ucArrayYMUidCode = String.fromCharCode.apply(null, ucArrayYMUidCode);
	result.rtv = true;
	result.uid = ucArrayYMUidCode;
	return result;
}


//AD23000628A00004
//hex AD 00 2E DC 63 2E A4 9B
function UidCode_YM2JQ_2(ucArrayYMUidCode)
{

	var i=0;
	var usYear = 0;
	var usMonth = 0;
	var usDay = 0;
	var ulNumber =0;
	var usBoxCode = 0;
	var ucIndexCode = 0;
	var dataLen = 0;
	var u8ArrayJQUidCode = [];
	if(!YMUid_Valid_check_2(ucArrayYMUidCode))
		return u8ArrayJQUidCode;
	
	//string2hex((char *)pstYMUID->ucArrayYMUidCode,6,(char *)pstJQUID->u8ArrayJQUidCode,&dataLen); //0xAD 0x23 0x58
	var str_temp = String.fromCharCode.apply(null,ucArrayYMUidCode.slice(0,6));
	var data_buf = HexStr2Bytes(str_temp);
	//0xAD 0x58
	u8ArrayJQUidCode[0] = data_buf[0];
	u8ArrayJQUidCode[1] = data_buf[2];
	
	//转换 年 月 日
	usYear = (ucArrayYMUidCode[2]-0x30)*10 + (ucArrayYMUidCode[3]-0x30);
	usMonth = (ucArrayYMUidCode[6]-0x30)*10 + (ucArrayYMUidCode[7]-0x30);
	usDay = (ucArrayYMUidCode[8]-0x30)*10 + (ucArrayYMUidCode[9]-0x30);
	
	usYear = usYear<<9|usMonth<<5|usDay;

	u8ArrayJQUidCode[2] = (usYear&0xFF00)>>8;
	u8ArrayJQUidCode[3] = (usYear&0xFF);
	
	
	
	ulNumber = ucArrayYMUidCode[10]*100000;
	usBoxCode = (ucArrayYMUidCode[11]-0x30)*100+ (ucArrayYMUidCode[12]-0x30)*10+(ucArrayYMUidCode[13]-0x30);
	ucIndexCode = (ucArrayYMUidCode[14]-0x30)*10+(ucArrayYMUidCode[15]-0x30);
	
	ulNumber += usBoxCode*100+ucIndexCode;
	
	u8ArrayJQUidCode[4] = (ulNumber&0xFF0000)>>16;
	u8ArrayJQUidCode[5] = (ulNumber&0xFF00)>>8;
	u8ArrayJQUidCode[6] = (ulNumber&0xFF);

	
	u8ArrayJQUidCode[7] = Epj_CalcCrc8(u8ArrayJQUidCode,7);
	
	return u8ArrayJQUidCode;
}




function UidCode_YM2JQ(ucArrayYMUidCode) {
	var i = 0;
	var u16Year = 0;
	var u16Month = 0;
	var u8Day = 0;
	var u16Date = 0;

	if (!YMUid_Valid_check(ucArrayYMUidCode))
		return false;
	var u8ArrayJQUidCode = [];


	u8ArrayJQUidCode[0] = (ucArrayYMUidCode[0] - 0x30) * 10 + (ucArrayYMUidCode[1] - 0x30); //企业代码
	u8ArrayJQUidCode[1] = ucArrayYMUidCode[4]; //特征码

	u16Year = (ucArrayYMUidCode[2] - 0x30) * 10 + (ucArrayYMUidCode[3] - 0x30);
	u16Month = (ucArrayYMUidCode[5] - 0x30) * 10 + (ucArrayYMUidCode[6] - 0x30);
	u8Day = (ucArrayYMUidCode[7] - 0x30) * 10 + (ucArrayYMUidCode[8] - 0x30);
	u16Date = (u16Year & 0x7F) << 9 | (u16Month & 0x0F) << 5 | (u8Day & 0x1F);
	u8ArrayJQUidCode[2] = (u16Date & 0xFF00) >> 8;
	u8ArrayJQUidCode[3] = (u16Date & 0x00FF);

	var stSerialCode = 0;
	for (i = 10; i < 15; i++) {
		stSerialCode = stSerialCode * 10 + (ucArrayYMUidCode[i] - 0x30);
	}

	stSerialCode |= ucArrayYMUidCode[9] << 17;
	stSerialCode = UINT32_ENDIAN_CHANGE(stSerialCode);;
	u8ArrayJQUidCode[4] = (stSerialCode >> 8) & 0x0FF;
	u8ArrayJQUidCode[5] = (stSerialCode >> 16) & 0x0FF;
	u8ArrayJQUidCode[6] = (stSerialCode >> 24) & 0x0FF;
	u8ArrayJQUidCode[7] = Epj_CalcCrc8(u8ArrayJQUidCode, 7);

	return u8ArrayJQUidCode;
}

function Epj_CalcCrc8(Ptr, num) {
	var i, j, crc = 0;

	for (j = 0; j < num; j++) {
		crc &= 0x0FF;
		crc ^= Ptr[j];
		for (i = 0; i < 8; i++) {
			if ((crc & 0x01) != 0) {
				crc >>= 1;
				crc ^= 0x8c;
			} else {
				crc >>= 1;
			}
		}
	}
	return crc;
}

function YMUid_Valid_check(u8ArrayYMUid) {
	var i = 0;
	var u8ValidDataCounter = 0;

	for (i = 0; i < 15; i++) {
		
		if (u8ArrayYMUid[i] >= 48 && u8ArrayYMUid[i] <= 57) {
			u8ValidDataCounter++;
			continue;
		}
		if (u8ArrayYMUid[i] >= 65 && u8ArrayYMUid[i] <= 90) {
			u8ValidDataCounter++;
			continue;
		}
		if (u8ArrayYMUid[i] >= 97 && u8ArrayYMUid[i] <= 122) {
			u8ValidDataCounter++;
			continue;
		}
	}

	if (u8ValidDataCounter < 7)
		return false;
	return true;
}

function YMUid_Valid_check_2(u8ArrayYMUid)
{
	var i=0;
	var u8ValidDataCounter = 0;
	for(i=0;i<16;i++)
	{
		if(u8ArrayYMUid[i] >= 48 && u8ArrayYMUid[i] <= 57)
		{
			u8ValidDataCounter++;
			continue;
		}
		if(u8ArrayYMUid[i] >= 65 && u8ArrayYMUid[i] <= 90)
		{
			u8ValidDataCounter++;
			continue;
		}
		if(u8ArrayYMUid[i] >= 97 && u8ArrayYMUid[i] <= 122)
		{
			u8ValidDataCounter++;
			continue;
		}
	}
	
	if(u8ValidDataCounter < 7)
		return false;
	return true;
}


function Epj_GetSpecialCodeIndex(specialcode) {
	if (specialcode >= 0x30 && specialcode <= 0x39) {
		return specialcode - 0x30;
	} else if (specialcode >= 65 && specialcode <= 90) {
		return specialcode - 65+10;
	} else if (specialcode >= 97 && specialcode <= 122) {
		return specialcode - 97+36;
	}
}

function UINT32_ENDIAN_CHANGE(val) {

var temp_val = ((val & 0x000000ff) << 24);
	temp_val |= ((val & 0x0000ff00) << 8);
	temp_val |= ((val & 0x00ff0000) >> 8);
	temp_val |= ((val & 0xff000000) >>24) & 0x0FF;
	return temp_val;
}

function Epj_GetiDateInfo(uidarray, index) {
	var month = (uidarray[index] - 48) * 10 + (uidarray[index + 1] - 48);
	var day = (uidarray[index + 2] - 48) * 10 + (uidarray[index + 3] - 48);
	return ((month << 5) | day);
}

function Epj_Uid2Pwd(uidarray) {
	var pwdbuf = [];
	if (uidarray.length != 15) {
		return pwdbuf;
	}
	var ssp = uidarray[9];
	var flownum = 0;
	var index = 10;
	while (index < 15) {
		flownum *= 10;
		flownum += ((uidarray[index] - 48) & 0x0FF);
		index++;
	}
	code = ((Epj_GetiDateInfo(uidarray, 5) & 0x01FF) << 23);
	code = code | ((Epj_GetSpecialCodeIndex(ssp) & 0x03F) << 17);
	code |= (flownum & 0x01FFFF);
	for (index = 0; index < 4; index++) {
		pwdbuf[index] = code & 0x0FF;
		code >>= 8;
	}
	return pwdbuf;
}
