var HexBase = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
var EProCmdV = {
	 WRITE_UID_  :  0x01,
     WRITE_PWD_  : 0x02,
     READ_UID_   :  0x03,
     READ_PWD_  :  0x04,
     VERIFY_UID_ : 0x05,
     VERIFY_PWD_   : 0x06,
     READ_STATE_   : 0x07,
     SET_DELAY_   : 0x08,
     SET_DELAY_ALL_  : 0x09,
     WRITE_ADDR_  : 0x0A,
     CHARGE_      : 0x0B,
     DISCHARGE_   : 0x0C,
     AMEND_       : 0x0D,
     READ_FIRWARE_  : 0x0E,
     READ_ADDR_   : 0x0F,
     BOOM_     :   0x10,
     INSPECT_SLAVE_  :  0x11,
     BASE_CHECK_   : 0x12,
     SLAVE_ONLINE_   : 0x13,
     ALL_SLEEP    : 0x14,
     READ_DELAY_   : 0x16,
     BC_WRITE_ADDR_   : 0x17,
     READ_AMEND_V_   : 0x18,
     READ_S_BUS_C_   : 0x19,
     READ_S_BUS_V_   : 0x1A,
     SLAVE_BUS_CMD  : 0x1B,
     EQUEL_UID_FEA  : 0x1C,
     SET_PSWD_FLAG   : 0x1D,
     SET_SUPER_T_    : 0x1E,
     MASTER_HARTBEAT_  : 0x20,
     SET_BUS_MODE_   : 0x21,
	 BROADCAST_ADDR  : 0xFF,

      BUS_MODE_R_    : 0xFFFE,
      BUS_MODE_B_    : 0xFFFD,
      WAIT_ONLINE_   : 0xFFFC,
      WAIT_RELEASE_   : 0xFFFB,
      TRANS_DELAY_    : 0xFFFA,
      

     S_A_ERROR_S     :            0 , //从机应答错误状态
     S_RA_WRITE_ID   :            1 , //主机写入UUID param :15字节
     S_RA_WRITE_PSW  :            2 , //主机写入密码 param :8字节
     S_RA_EQUAL_ID   :            3 , //主机验证UUID param :13字节
     S_RA_EQUAL_PSW  :            4 , //主机验证密码 param :8字节
     S_RA_SET_DELAY  :            5 , //主机设置单发雷管的延时 param :2字节
     S_RA_SET_ADDR   :            7 , //设置从机的通讯地址 param :2字节 或 15+2（广播通过UID设置地址）
     S_RA_OPER_CMD   :            8 , //主机发送操作命令 param :1字节
     S_RA_REQUEST_ACK  :          9 , //主机发送的应答请求 param :0字节
     S_RA_SET_BAUDRATE :          10 ,//设置通讯速率 param :2字节
     S_R_SET_LOCAL     :          11 ,//设置使用位置 param :8字节
     S_R_SET_USETIME   :          12 , //设置使用时间 param :2字节
     S_R_HEARTBEAT     :          13 ,//心跳包 param :0字节
     S_R_FACTORY_SET   :          14 ,//释放工厂模式 param :4字节
     S_UID_REQUEST     :          15 ,//利用UID进行访问，只能用广播访问 param  : {UID+(10bit地址+6bit功能码)+功能码参数}
     S_UID_SEARCH      :          16 ,//UID搜索功功能码
     S_RA_EQUAL_UID_F  :          17 ,//UID特征码验证
     S_RA_SETSUPER_T   :          18 ,//设置复位保存延时起爆时间
     RA_REGISTER	    :		  62 ,//读寄存器
     WA_REGISTER	    :		  63 ,//写4字节寄存器

     S_CMD_READ_ID        :       1 ,  //主机读取UUID
     S_CMD_READ_PSW       :       2 ,  //主机读取PSWD
     S_CMD_READ_SYS_S     :       3 ,  //主机读取系统状态
     S_CMD_READ_VERSION   :       4 ,  //主机读取系统版本
     S_CMD_READ_LOCAL     :       5 ,  //读取位置信息
     S_CMD_READ_OPERTIME  :       6 ,  //读取操作时间
     S_CMD_READ_ADDR      :       7 ,  //读取通讯地址
     S_CMD_READ_DELAY     :       8 ,  //主机读取延时设置
     S_CMD_READ_SYS_F     :       9 ,  //读取备份区的状态信息

     S_CMD_CHARGE         :       10 , //充电
     S_CMD_DISCHARGE      :       11 , //放电
     S_CMD_CLK_CHECK      :       12 , //时钟校准
     S_CMD_BLAST          :       13 , //起爆
     S_CMD_SLEEP          :       14 , //强制休眠，不在监听总线
     S_CMD_INSPECT        :       15 , //巡检
     S_CMD_SYS_CHECK      :       16 , //执行自检
    //public final static byte CMD_SEARCH_UID         :   17  //搜索UID
     S_CMD_STOP_CHARGE    :       18 , //停止充电
     S_CMD_FAC_CHECK1     :       19 , //工厂自检
     S_CMD_FAC_CHECK2     :       20 , //工厂自检
     S_CND_READ_AMEND     :       21 ,  //读校准值
     S_CMD_FAC_CHECK3     :       22 , //工厂自检
     S_CMD_READ_BUSV      :       23 , //读总线电压
     S_CMD_EQUEL_PWD      :       24 , //密码验证置位
};
//十六进制字符串转字节数组
 
     function HexStr2Bytes(str) 
	{
		var pos = 0;
		var len = str.length;
		if(len %2 != 0)
		{
		   return null; 
	 
		}
		len /= 2;
		var hexA = new Array();
		for(var i=0; i<len; i++)
		{
		   var s = str.substr(pos, 2);
		   var v = parseInt(s, 16);
		   hexA.push(v);
		   pos += 2;
		}
		/*
		len = hexA.length;
		var index = 0;
		while(index < (len-index-1))
		{
			pos = hexA[index];
			hexA[index] = hexA[len-index - 1];
			hexA[len-index - 1] = pos;
			index++;
		}*/
		
		return hexA;
	 
	}
	function HexString2ByteArray(src)
	{
		var regexp = /(,|;|0x|0X)/g;
		var w_data = src.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);
		return w_data;
	}
	function Str2Bytes(str)
	{
		var pos = 0;
		var len = str.length;
		if(len < 1)
		{
		   return null; 
	 
		}
		var buf = new Array();
		for(var i=0; i<len; i++)
		{
		   var v = parseInt(str.charCodeAt(i), 10);
		   buf.push(v);
		   pos += 2;
		}
		return buf;
	}
	//字节数组转十六进制字符串
	 function Bytes2Str(arr)
	{
		var str = "";
		for(var i=0; i<arr.length; i++)
		{
			str += HexBase[(arr[i] >> 4) & 0x0F];
			str += HexBase[(arr[i]) & 0x0F];
			if(i != arr.length )
				str+= " ";
		}
		return str;
	}
	
	function Bytes2ChartArray(arr)
	{
		var str = [];
		var index = 0;
		for(var i=0; i<arr.length; i++)
		{
			str[index++]= HexBase[(arr[i] >> 4) & 0x0F];
			str[index++]= HexBase[(arr[i]) & 0x0F];
			
		}
		return str;
	}
	
	
	function Byte2Hex(data)
	{
		var str = "";
		str += HexBase[(data >> 4) & 0x0F];
		str += HexBase[(data) & 0x0F];
		return str;
	}
	
	function Bytes2ULong(arr)
	{
		var data_l = 0;
		for(i = 0; i < arr.length; i++)
		{
			data_l += (arr[i] & 0xFF) << (8*i);
		}
		return data_l;
	}
	
	function Bytes2UShort(arr){
		var data_s = Bytes2ULong(arr);
		if((data_s & 0x8000) > 0)
		{
			data_s = (65536 - data_s)*-1;
		}
		return data_s;
			
	}
	
	function IntFormat(charcount,val)
	{
		var buf_ = new Array();
		for(var count_ = 0 ; count_ < charcount; count_++)
		{
			buf_.push(val % 10);
			val = parseInt(val / 10);
		}
		var str_ = "";
		while(charcount > 0)
		{
			charcount--;
			str_ += buf_[charcount];
		}
		return str_;
	}
	
	function ArrayCopy(src,dst,offset,len)
	{
		var len_
		if(typeof(len) == "undefined" || len == null || len < 1)
		{
			if(src.length > offset)
				len_ = src.length - offset;
			else
				return;
		}else{
			len_ = len;
		}
		if(offset < 0)
		{
			offset = 0;
		}
		var index = 0;
		while(index < len_)
			dst[index++] = src[offset++];
		return dst;
	}
	function ArrayAppend(src,dst,offset,len)
	{
		var len_
		if(typeof(len) == "undefined" || len == null || len < 1)
		{
			len_ = src.length;
		}else{
			len_ = len;
		}
		if(offset < 0)
		{
			offset = 0;
		}
		var index = 0;
		while(index < len_)
			dst[offset++] = src[index++];
		return dst;
	}
	function Int2HexStr(intdata,spec,format)
	{
		var data = [];
		var count = 0;
		var hexdata = '';
		var index = 0;
		var index2 = 0;
		var temp = '';
		if((format == 0) && (intdata == 0))
		{
			return '0';
		}
		while(intdata > 0)
		{
			data[count++] = HexBase[intdata  & 0x0F]; 
			intdata >>= 4;
		}
		if((count %2) != 0)
		{
			data[count++] = '0';
		}
		index = 0;
		while((count > 0) && ((count-1-index) >index))
		{
			temp = data[index];
			data[index] = data[count-1-index];
			data[count-1-index] = temp;
			index++;
		}
		
		if(format > count){
			index = 0;
			if((format %2) > 0)
				format++;
			while(format > count)
			{
				if((index % 2) == 0)
				{
					hexdata += spec;
				}
				hexdata += '0';
				format--;
				index++;				
			}
		}
		index = 0;
		while(index < count)
		{
			if((index % 2) == 0)
			{
				hexdata += spec;
			}
			hexdata += data[index++];
		}
		return hexdata;
		
	}
function Crc8(buf)
{
	var crc = 0;
	var index = 0;
	while(index < buf.length)
	{
		crc ^= buf[index++];
		crc &= 0xFF;
	}
	return crc;
}

function PrapareUIDFeatrue(uidarray)
{
 var index1 = 0;
 var us_temp = 0;
 var uidfeatrue = [];
 var uid_f = uidarray[9];
 //拷贝特征值
 uidfeatrue[0] = 0;// uidarray[9];
  //合并年月
 uidfeatrue[1] = uidarray[3] -  0x30;
 uidfeatrue[1] <<= 4;
 if(uidarray[5] > 48)
 {
    uidfeatrue[1] += 0x0A;
 }
 uidfeatrue[1] += (uidarray[6] - 0x30 ); 
  //特征码于日期调整
  uidarray[9] =  uidarray[8];
  uidarray[8] =  uidarray[7];
 //将日期和流水号转为3字节int
  us_temp = 0;
  for(var index1 = 8; index1 < 15; index1++)
  {
   us_temp *= 10;
    us_temp +=  (uidarray[index1] - 0x30 );
  }
  for(var index1 = 0; index1 < 3; index1++)
  {
    uidfeatrue[2+index1] = us_temp & 0xFF;
    us_temp >>= 8;
  }
  for(var index = 0; index < 4;index++)
  {
  	uidfeatrue[index] = uidfeatrue[index+1];
  }
  uidfeatrue[4] = uid_f;
  return uidfeatrue;
//	Mem_Copy(searchfeatrue,uidfeatrue,6);
//	index1 = searchfeatrue[4];
//	searchfeatrue[4] = searchfeatrue[2];
//	searchfeatrue[2] = index1;
}

function PrapareUIDFeatrue2(uidarray)
{
 var index1 = 0;
 var us_temp = 0;
 var uidfeatrue = [];
 //拷贝特征值
 uidfeatrue[0] =  uidarray[9];
  //合并年月
 uidfeatrue[1] = uidarray[3] -  0x30;
 uidfeatrue[1] <<= 4;
 if(uidarray[5] > 48)
 {
    uidfeatrue[1] += 0x0A;
 }
 uidfeatrue[1] += (uidarray[6] - 0x30 ); 
  //特征码于日期调整
  uidarray[9] =  uidarray[8];
  uidarray[8] =  uidarray[7];
 //将日期和流水号转为3字节int
  us_temp = 0;
  for(var index1 = 8; index1 < 15; index1++)
  {
   us_temp *= 10;
    us_temp +=  (uidarray[index1] - 0x30 );
  }
  for(var index = 0; index < 3;index++)
  {
  	 uidfeatrue[2+index] = us_temp & 0xFF;		
     us_temp >>= 8;
  }
  return uidfeatrue;
//	Mem_Copy(searchfeatrue,uidfeatrue,6);
//	index1 = searchfeatrue[4];
//	searchfeatrue[4] = searchfeatrue[2];
//	searchfeatrue[2] = index1;
}

function Epro_CmdData(addr,func,cmd,param,read_len)
{
	var bufdata = [];
	var index = 0;
	addr <<= 6;
	addr |= (func & 0x3F);
	bufdata[index++] =	addr & 0xFF;
	bufdata[index++] =  (addr>>8) & 0xFF; 
	if(func == 0x08 || func == 0x0A)
	{
		bufdata[index++] =  cmd;
	}
	if(typeof(param) != "undefined" && param != null)
	{
		for(i = 0; i < param.length;i++)
		{
			bufdata[index++] = param[i];
		}
	}
	var crc =  Crc8(bufdata);
	bufdata[index++]  = crc;
	var bufdata2 = [];
	bufdata2[0] = index;
	bufdata2[1] = read_len;
	for(var i = 0; i < index ; i++)
	{
		bufdata2[i+2] = bufdata[i];
	}
	return bufdata2;
	
}


