 var cmd_excute_rt = 0;
 var cmd_excute = 0;
 var cycledelay_flag = false;
 var heart_beat_delay = 1000;
 var wait_ack_cmdmap = new Map();
 function EShotCmdRecv(frame)
    {
     var cmd_ = frame[5] & 0xFF;
     var ack =  frame[8] & 0xFF;
	 var log_mes = "";
     if(cycledelay_flag)
     {
      cmd_excute_rt = ack;
      cmd_excute = cmd_;
	  ShowCommMes(rec,MesgType.MESG_TYPE_INFO);
	  return;
     }
     if((cmd_ != EProCmdV.MASTER_HARTBEAT_ ) && 
		(ack == 0x0A) && 
		(frame[9] == 0 )&& 
		(frame[10] == 0)){
			 ack = 0;
		}
     if((BtOper_Json.cycletest > 0)  && (cmd_ != EProCmdV.MASTER_HARTBEAT_))
     {
       if(ack != 0)
       {
        log_show = "模块通讯错误 Rx: ";
        log_show += Bytes2Str(frame);
        ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
       }else{
	    log_show = "Rx: "+Bytes2Str(frame);
		 ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
       }
     }
     switch(cmd_)
     {
      case EProCmdV.WRITE_UID_: if(ack == 0){
       log_show =  "地址： "+BtOper_Json.commaddr+" 写入的UID是："+ BtOper_Json.uid;
	   ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
       log_show = "地址："+BtOper_Json.commaddr+" 写入失败；UID是："+BtOper_Json.uid;
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.WRITE_PWD_: if(ack == 0)
      {
       log_show = "地址："+BtOper_Json.commaddr+" 写入的密码是："+ BtOper_Json.pwd;
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
       log_show = "地址："+BtOper_Json.commaddr+" 写入失败,密码:"+ BtOper_Json.pwd;
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;

      case EProCmdV.READ_UID_:
       if(ack == 0){
		BtOper_Json.uid =  String.fromCharCode.apply(null,frame.slice(11,26));
		log_show = "地址: "+BtOper_Json.commaddr + " UID: "+BtOper_Json.uid;
		ShowBtUID(BtOper_Json.uid);
		ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else {
		log_show = "地址："+ BtOper_Json.commaddr + " 读UID失败";
		ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.READ_PWD_: if(ack == 0)
      {
       BtOper_Json.pwd = String.fromCharCode.apply(null,frame.slice(9,17));
	   log_show = BtOper_Json.uid + "的密码是："+BtOper_Json.pwd;
	   ShowBtPWD(BtOper_Json.pwd);
	   ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
	   log_show = BtOper_Json.uid + " 读密码失败";
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.VERIFY_UID_:	  
       var transaddr = frame[10];
       transaddr = (transaddr & 0xFF) << 8;
       transaddr += frame[9] & 0x0FF;
       BtOper_Json.commaddr = transaddr;
       if(ack == 0){
	   log_show = "地址 "+BtOper_Json.commaddr +" UID验证成功"
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
	   log_show = "地址 "+ BtOper_Json.commaddr +" UID验证失败"
	   ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.VERIFY_PWD_:
       var transaddr = frame[10];
       transaddr = (transaddr & 0xFF) << 8;
       transaddr += frame[9] & 0x0FF;
       BtOper_Json.commaddr = transaddr;
       if(ack == 0)
      {
		log_show = "地址 "+BtOper_Json.commaddr+" 密码验证成功"
		ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
		 log_show = "地址 "+BtOper_Json.commaddr+" 密码验证失败"
		 ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.READ_STATE_:
       var transaddr = frame[10];
       transaddr = (transaddr & 0xFF) << 8;
       transaddr += frame[9] & 0x0FF;
       BtOper_Json.commaddr = transaddr;
       if(ack == 0){
		BtOper_Json.state_v = frame.slice(11,15);
		BtOper_Json.state_str = Bytes2Str(frame.slice(11,13));
        log_show = "地址 "+ BtOper_Json.commaddr +" 的状态值是 "+BtOper_Json.state_str;
		ShowBtState(BtOper_Json.state_str);
        ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
		  log_show = "读地址 "+BtOper_Json.commaddr+" 状态失败";
		  ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.SET_DELAY_:
       var transaddr = frame[10];
       transaddr = (transaddr & 0xFF) << 8;
       transaddr += frame[9] & 0x0FF;
       BtOper_Json.commaddr = transaddr;
       if(ack == 0){
		log_show = "地址 "+ BtOper_Json.commaddr +" 的设置的延时是 "+BtOper_Json.setdelay;
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
		log_show = "地址 "+ BtOper_Json.commaddr +" 设置延时失败 ";
		ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.WRITE_ADDR_: if(ack == 0){
		 
       log_show = "UID "+BtOper_Json.uid+" 设置的地址是 "+ BtOper_Json.commaddr;
	   ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
		log_show = "UID "+BtOper_Json.uid+" 设置地址失败";
		ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      };break;
      case EProCmdV.READ_AMEND_V_: if(ack == 0){
	   BtOper_Json.amendv = Bytes2UShort(frame.slice(11,13));
	   log_show = "地址 "+BtOper_Json.commaddr+" 的校准值是 "+BtOper_Json.amendv;
	   ShowBtAmendV(BtOper_Json.amendv);
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
		log_show = "读地址 "+BtOper_Json.commaddr+" 校准值失败";
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.INSPECT_SLAVE_: if(ack == 0)
      {
       BtOper_Json.inspect_buf = frame.slice(9,frame.length-2);
       log_show = "巡检成功";
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
		log_show = "巡检失败";
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.READ_FIRWARE_: if(ack == 0){
       BtOper_Json.firmware = Bytes2Str(frame.slice(9,13));
	   log_show = "地址 "+BtOper_Json.commaddr+" 固件版本 "+ BtOper_Json.firmware;
	   ShowBtFirmware(BtOper_Json.firmware);
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
       log_show = "读地址 "+BtOper_Json.commaddr+" 固件版本失败";
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
	   ShowBtFirmware(" ");
      }break;
      case EProCmdV.READ_ADDR_: if(ack == 0){
       var transaddr = frame[10];
       transaddr = (transaddr & 0xFF) << 8;
       transaddr += frame[9] & 0x0FF;
	   BtOper_Json.commaddr = transaddr;
       log_show = BtOper_Json.uid + " 的地址是 "+BtOper_Json.commaddr;
	   ShowBtAddr(BtOper_Json.commaddr);
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else {
	   log_show = " 读UID "+BtOper_Json.uid+" 地址失败";
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      } break;
      case EProCmdV.READ_DELAY_: if(ack == 0){
       var delay_ = frame[10];
       delay_ = (delay_ & 0xFF) << 8;
       delay_ += frame[9]& 0x0FF;
       delay_ -= 5;
	   BtOper_Json.setdelay = delay_;
       log_show = "地址 "+BtOper_Json.commaddr+" 的延时是 "+BtOper_Json.setdelay+"ms";
	   ShowBtDelay(BtOper_Json.setdelay);
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
       log_show = "地址 "+BtOper_Json.commaddr+" 读延时失败";
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.READ_S_BUS_V_:if(ack == 0){
       var voltage = frame[10];
       voltage = (voltage & 0x0F) << 8;
       voltage += frame[9] & 0x0FF;
       BtOper_Json.cfir_v = (voltage * 3.3 *3.43)/(4096 * 0.43);
	   BtOper_Json.cfir_v  = BtOper_Json.cfir_v.toFixed(2);
	   log_mes = "地址 "+BtOper_Json.commaddr+" 的电压是 "+BtOper_Json.cfir_v+"v";
       ShowBtBusV(BtOper_Json.cfir_v);
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
       log_mes = " 地址 "+BtOper_Json.commaddr+" 读电压失败";
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.EQUEL_UID_FEA:if(ack == 0){
       var transaddr = frame[10];
       transaddr = (transaddr & 0x0F) << 8;
       transaddr += frame[9] & 0x0FF;
	   log_show = "UID "+BtOper_Json.uid+" 的地址是 "+BtOper_Json.commaddr;
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
       log_show = "UID "+BtOper_Json.uid+" 特征码设置地址失败";
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.SET_PSWD_FLAG:if(ack == 0){
       var transaddr = frame[10];
       transaddr = (transaddr & 0x0F) << 8;
       transaddr += frame[9] & 0x0FF;
       log_show = "地址 "+BtOper_Json.commaddr+" 强制置位密码验证";
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
       log_show = "地址 "+BtOper_Json.commaddr+" 强制置位密码验证失败";
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
      case EProCmdV.CHARGE_: if(ack == 0){
       log_show = "发送充电命令";
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      };break;
      case EProCmdV.ALL_SLEEP: if(ack == 0){
       log_show = "发送充休眠命令";
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      };break;
      case EProCmdV.BASE_CHECK_:if(ack == 0){
       log_show =   "发送检测命令";
      ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      };break;
      case 0x21: if(ack == 0)
      {
       if(BtOper_Json.busmode == 1)
       {
        log_show =  "开启总线";
        ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
       }else{
        log_show =  "关闭总线";
        ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
       }
      }break;
      case EProCmdV.MASTER_HARTBEAT_: AnalyHartBeat(frame); ack = 0;break;
      case EProCmdV.SLAVE_BUS_CMD: if(ack == 0){
       log_show =  "命令直传成功";
       ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      }else{
       log_show =  "命令直传失败";
       ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }
       break;
      default : if(ack == 0){
		  log_show =  "操作成功";
		  ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
      } else{
	   log_show =  "操作失败";
	   ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
      }break;
     }
     cmd_excute_rt = ack;
     cmd_excute = cmd_;
     SendWaitSignal(cmd_,ack);
	 if(ack != 0)
	  BTErrorCount( 1 );
	 else
	  BTInfoCount( 1 ) ;
  }
 function AnalyHartBeat(frame)
 {

  var cmd_ = frame[5] & 0xFF;
  var log_show = "";
  if(cmd_ != EProCmdV.MASTER_HARTBEAT_)
   return null;
  if(frame.length < 22)
  {
   log_show = "关闭心跳包";
   ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
   return ;
  }
  var batter_state = frame[8];
  var bat_buf = new ArrayBuffer(4);
  var dataview = new DataView(bat_buf);
  dataview.setUint8(0, frame[12]);
  dataview.setUint8(1, frame[11])
  dataview.setUint8(2, frame[10])
  dataview.setUint8(3, frame[9])
   bat_v = dataview.getFloat32(0);
   bat_v = bat_v.toFixed(2);
   
  var bus_switch = frame[15];
  var bus_mode = frame[16];
  var bus_v = Bytes2ULong(frame.slice(17,19)) / 10.0;
  bus_v = bus_v.toFixed(1);
  var bus_c = Bytes2ULong(frame.slice(19,23)) / 10000.0;
  bus_c = bus_c.toFixed(2);

  log_show = "电池电量 "+batter_state+"% 电压"+bat_v+"V";
  BtOper_Json.bat_v = bat_v;

  if(bus_switch == 0)
  {
   log_show += "未开启总线  ";
  }else{
   if(bus_mode == 1)
   {
    log_show += "注册模式  ";
   }else{
    log_show += "起爆模式  ";
   }
  }
  log_show += "总线电压   "+bus_v+"V"
  log_show += "总线电流   "+bus_c+"mA"
  BtOper_Json.bus_v = bus_v;
  BtOper_Json.bus_c = bus_c;
  ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
 }
 
 
function RestWaitSignal( cmd)
{
    wait_ack_cmdmap.set(cmd,0xFFFF);
}

function GetWaitSignal( cmd)
{
	if(wait_ack_cmdmap.has(cmd))
		return wait_ack_cmdmap.get(cmd);
	else
		return -1;
}

function SendWaitSignal( cmd,  ack)
{
 
    wait_ack_cmdmap.set(cmd,ack);
    
}

function ReleasWaitSignal( cmd)
{
    wait_ack_cmdmap.delete(cmd);
}
 

 
 function TransSleep_delay(delay) {
  return new Promise((resolve, reject) => {
    setTimeout(function(){
      resolve("done");
    }, delay);
  });
};
 async function TransSleep(delay)
 {
	 await TransSleep_delay(delay);
 }
 
 async function WaitCmdExcu( cmd,  time_out)
{
    var wait_signal = -1;
    RestWaitSignal(cmd);
    if(time_out == 0)
        return 0;
    time_out = parseInt ( (time_out+19)/20);

    while(time_out > 0)
    {
         wait_signal = GetWaitSignal(cmd);
        if(wait_signal != 0xFFFF)
        {
            break;
        }
        await  TransSleep(20);
        time_out--;
    }
    cmd_excute = 0;
    ReleasWaitSignal(cmd);
    
    if(time_out > 0 &&  wait_signal == 0 )
    {
        return 0;
    }
    if(time_out == 0)
    {
       ShowCommMes("通信超时",MesgType.MESG_TYPE_ERROR);
       return  -1;

    }
    else
        return cmd_excute_rt;
}

async function WriteByAddr_ ( cmd,  addr, param,  waitexcue,  logflag,  timeout)
{
	var rtv = 0;
	cmd_excute = 0;
	BtSendBytes(PBCmd.AddrWriteCmd(cmd,addr,param),logflag);
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(cmd,timeout);
    }
    return rtv;
}
async function WriteByAddr(logflag)
{
    if(BtOper_Json.cycletest < 1)
    {
        await WriteByAddr_(BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.setparam,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
    }else{
        var count = 0;
        while((count <  BtOper_Json.cycletimes) && (BtOper_Json.cycletest > 0))
        {
            count++;
            var index  = 0;

             while((index < BtOper_Json.groupcount)
            && (BtOper_Json.cycletimes > 0 )
            && (BtOper_Json.cycletest > 0))
            {
				BtOper_Json.commaddr = BtOper_Json.begaddr + index;
                await WriteByAddr_(BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.setparam,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
                index++;
               await TransSleep(BtOper_Json.rundelay);
            }
        }
    }
}

async function WriteUID( logflag)
{
    if(BtOper_Json.cycletest < 1)
    {
		BtOper_Json.setparam = Str2Bytes( BtOper_Json.uid);
		await WriteByAddr_(BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.setparam,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
    }else{
        var count = 0;
        var flow_str = BtOper_Json.beguid.slice(11,15);
        var uidhead = BtOper_Json.beguid.slice(0,11);
        var flow_num = (flow_str);
        if(flow_num = NaN || flow_num == undefined)
        {
            var log_show = "循环写入UID流水号解析错误";
            ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
        }
        while((count <  BtOper_Json.cycletimes) && (BtOper_Json.cycletest > 0))
        {
            count++;
            index = 0;
            while((index < BtOper_Json.groupcount)
			 && (BtOper_Json.cycletimes > 0 )
			 && (BtOper_Json.cycletest > 0))
            {
				BtOper_Json.uid = uidhead + IntFormat(4,flow_num+index);
				BtOper_Json.setparam = Str2Bytes( BtOper_Json.uid);
				BtOper_Json.commaddr = BtOper_Json.begaddr + index;
                await WriteByAddr_(BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.setparam,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
                index++;
                await TransSleep(BtOper_Json.rundelay);
            }

        }
    }
}


async function WritePwd_( pwd,  addr,  waitexcue,  logflag,  timeout)
{
	var pwd_arr = Str2Bytes(pwd); 
	var rtv = 0;
	BtSendBytes(pbcmd.AddrWriteCmd(EProCmdV.WRITE_PWD_,addr,pwd_arr),logflag);
    cmd_excute = 0;
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(EProCmdV.WRITE_PWD_,timeout);
    }
    return rtv;
}

async function WritePwd(  logflag)
{
    if(BtOper_Json.cycletest < 1)
    {
		BtOper_Json.setparam = Str2Bytes( BtOper_Json.pwd); 
	 await 	WriteByAddr_(BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.setparam,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
    }else{
        var count = 0;
        while((count <  BtOper_Json.cycletimes) && (BtOper_Json.cycletest > 0))
        {
            count++;
            var index = 0;
			 while((index < BtOper_Json.groupcount)
			 && (BtOper_Json.cycletimes > 0 )
			 && (BtOper_Json.cycletest > 0))
            {
				BtOper_Json.commaddr = BtOper_Json.begaddr + index;
                BtOper_Json.setparam = Str2Bytes( BtOper_Json.pwd); 
                await WriteByAddr_(BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.setparam,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
                index++;
                await TransSleep(BtOper_Json.rundelay);
            }
        }
    }
}

async function WriteAddr_( uid,  cmd, addr,  waitexcue,  logflag,  timeout)
{
	var rtv = 0;
	var uid_arr =  Str2Bytes(uid);  
    cmd_excute = 0;
	BtSendBytes(PBCmd.UidWriteAddr(uid_arr,addr,cmd),logflag);
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(cmd,timeout);
    }
    return rtv;
}

async function WriteAddr( logflag)
{
    if(BtOper_Json.cycletest < 1)
    {
	 await 	WriteAddr_(BtOper_Json.uid,BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
    }else{
        var count = 0;
         var flow_str = BtOper_Json.beguid.slice(11,15);
        var uidhead = BtOper_Json.beguid.slice(0,11);
        var flow_num = (flow_str);
        if(flow_num = NaN || flow_num == undefined)
        {
            var log_show = "循环写入UID流水号解析错误";
            ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
        }

        while(count <  BtOper_Json.cycletimes && (BtOper_Json.cycletest > 0))
        {
            var index = 0;
             while((index < BtOper_Json.groupcount)
            && (BtOper_Json.cycletimes > 0 )
            && (BtOper_Json.cycletest > 0))
            {
				BtOper_Json.uid = uidhead + IntFormat(4,flow_num+index);
				BtOper_Json.commaddr = BtOper_Json.begaddr + index;
                await WriteAddr_(BtOper_Json.uid,BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
                index++;
                await TransSleep(BtOper_Json.rundelay);
            }
            count++;
        }
    }
}

async function WriteDelay(logflag)
{
    if(BtOper_Json.cycletest < 1)
    {
		var param = [];
		param[0] = BtOper_Json.setdelay & 0xFF;
		param[1] = (BtOper_Json.setdelay >> 8) & 0xFF;
		BtOper_Json.setparam = param;
	 await 	WriteByAddr_(BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.setparam,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
    }else{
        var count = 0;
		var param = [];
        while(count <  BtOper_Json.cycletimes && (BtOper_Json.cycletest > 0))
        {
            count++;
            var index  = 0;
            while((index < BtOper_Json.groupcount)
           && (BtOper_Json.cycletest > 0))
            {
                 BtOper_Json.setdelay = BtOper_Json.begdelay+BtOper_Json.delaysetp*index;
				 BtOper_Json.commaddr = BtOper_Json.begaddr + index;
                 param[0] = BtOper_Json.setdelay & 0xFF;
                 param[1] = (BtOper_Json.setdelay >> 8) & 0xFF;
                 BtOper_Json.setparam = param;
                await WriteByAddr_(BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.setparam,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
                index++;
                await TransSleep(BtOper_Json.rundelay);
            }
        }
    }
}




async function ReadPwd_( uid,  waitexcue,  logflag,  timeout)
{
	var rtv = 0;
    cmd_excute = 0;
	var uid_arr = Str2Bytes(BtOper_Json.uid);
	BtSendBytes(PBCmd.PBGetSendCmd(EProCmdV.READ_PWD_,uid_arr),logflag);
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(EProCmdV.READ_PWD_,timeout);
    }
    return rtv;
}

async function ReadAddr_( uid,  waitexcue,  logflag,  timeout)
{
    var rtv = 0;
    cmd_excute = 0;
    var uid_arr = Str2Bytes(BtOper_Json.uid);
	BtSendBytes(PBCmd.PBGetSendCmd(EProCmdV.READ_ADDR_,uid_arr),logflag);
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(EProCmdV.READ_PWD_,timeout);
    }
    return rtv;
}


async function ReadBusCurrent( waitexcue,  logflag, timeout)
{
    var nulldata= [];
	var rtv = 0;
	cmd_excute = 0;
	BtSendBytes(PBCmd.PBGetSendCmd(EProCmdV.READ_S_BUS_C_,nulldata),logflag);
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(EProCmdV.READ_S_BUS_C_,timeout);
    }
    return rtv;
}
async function ReadByAddr_( cmd,  addr,  waitexcue,  logflag,  timeout)
{
	var rtv = 0;
    cmd_excute = 0;	
	var cmd_buf = PBCmd.AddrReadCmd(cmd,addr);
	BtSendBytes(cmd_buf,logflag);
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(cmd,timeout);
    }
    return rtv;
}
 async function ReadByAddr(logflag)
{
    if(BtOper_Json.cycletest < 1)
    {
      await  ReadByAddr_(BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
    }else{
        var count = 0;
        while((count <  BtOper_Json.cycletimes) && (BtOper_Json.cycletest > 0))
        {
            count++;
            var index = 0;
            while((index < BtOper_Json.groupcount)
            && (BtOper_Json.cycletimes > 0 )
			&& (BtOper_Json.cycletest > 0))
            {
				BtOper_Json.commaddr = BtOper_Json.begaddr + index;
				await ReadByAddr_(BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
                index++;
                await TransSleep(BtOper_Json.rundelay);
            }

        }
    }
}
async function AddrCmd_( cmd,  addr,  waitexcue,  logflag,  timeout)
{
	var rtv = await ReadByAddr_(cmd,  addr,  waitexcue,  logflag,  timeout);
	return rtv;
}
async function AddrCmd( logflag)
{

   var rtv = await ReadByAddr(logflag);
   return rtv;
}
async function ReadByUID_(cmd,  uid,  waitexcue,  logflag,  timeout)
{
	var rtv = 0;
	var uid_arr = Str2Bytes(uid);
    cmd_excute = 0;
	BtSendBytes(PBCmd.PBGetSendCmd(cmd,uid_arr),logflag);
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(cmd,timeout);
    }
    return rtv;
}
async function ReadByUid(logflag)
{
    if(BtOper_Json.cycletest < 1)
    {
	 await 	ReadByUID_(BtOper_Json.opercmd,BtOper_Json.uid,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
    }else{
    	
    	 var count = 0;
        var flow_str = BtOper_Json.beguid.slice(11,15);
        var uidhead = BtOper_Json.beguid.slice(0,11);
        var flow_num = (flow_str);
        if(flow_num = NaN || flow_num == undefined)
        {
            var log_show = "循环写入UID流水号解析错误";
            ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
        }
        while((count <  BtOper_Json.cycletimes) && (BtOper_Json.cycletest > 0))
        {
            var index = 0;
             while((index < BtOper_Json.groupcount)
            && (BtOper_Json.cycletimes > 0 )
			&& (BtOper_Json.cycletest > 0))
            {
            	BtOper_Json.uid = uidhead + IntFormat(4,flow_num+index);
                await ReadByUID_(BtOper_Json.opercmd,BtOper_Json.uid,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
                index++;
                await TransSleep(BtOper_Json.rundelay);
            }
           count++;

        }
    }
}
async function WaitSlaveAck( addr,  waitexcue,  logflag,  timeout)
{
	var rtv = 0;
    cmd_excute = 0;
    BtSendBytes(PBCmd.AddrReadCmd(EProCmdV.SLAVE_ONLINE_,addr),logflag);
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(EProCmdV.SLAVE_ONLINE_,timeout);
    }
    return rtv;
}
async function VerifyUID_( addr,  uid,  waitexcue,  logflag,  timeout)
{
	var rtv = 0;
	cmd_excute = 0;
	var uid_arr = Str2Bytes(uid);
	BtSendBytes(PBCmd.AddrWriteCmd(EProCmdV.VERIFY_UID_,addr,uid_arr),logflag);
	if(waitexcue)
	{
	    rtv = await WaitCmdExcu(EProCmdV.VERIFY_UID_,timeout);
	}
	return rtv;
}
async function VerifyUID( logflag)
{
    if(BtOper_Json.cycletest < 1)
    {
       await   VerifyUID_(BtOper_Json.commaddr,BtOper_Json.uid, BtOper_Json.waitack,logflag, BtOper_Json.waittimeout);
    }else{
         var count = 0;
        var flow_str = BtOper_Json.beguid.slice(11,15);
        var uidhead = BtOper_Json.beguid.slice(0,11);
        var flow_num = (flow_str);
        if(flow_num = NaN || flow_num == undefined)
        {
            var log_show = "循环写入UID流水号解析错误";
            ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
        }
        while((count <  BtOper_Json.cycletimes) && (BtOper_Json.cycletest > 0))
        {
            var index = 0;
             while((index < BtOper_Json.groupcount)
            && (BtOper_Json.cycletimes > 0 )
			&& (BtOper_Json.cycletest > 0))
            {
                BtOper_Json.uid = uidhead + IntFormat(4,flow_num+index);
                await VerifyUID_(BtOper_Json.commaddr,BtOper_Json.uid, BtOper_Json.waitack,logflag, BtOper_Json.waittimeout);
                index++;
                await TransSleep(BtOper_Json.rundelay);
            }
           count++;

        }
    }
}
async function VerifyPwd_(addr,  pwd,  waitexcue,  logflag,  timeout)
{
	var rtv = 0;
    var pwd_arr = Str2Bytes(pwd); 
    cmd_excute = 0;
    BtSendBytes(PBCmd.AddrWriteCmd(EProCmdV.VERIFY_PWD_,addr,pwd_arr),logflag);
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(EProCmdV.VERIFY_PWD_,timeout);
    }
    return rtv;
}

async function VerifyPwd( logflag)
{
    if(BtOper_Json.cycletest < 1)
    {
       await   VerifyPwd_(BtOper_Json.commaddr,BtOper_Json.pwd, BtOper_Json.waitack,logflag, BtOper_Json.waittimeout);
    }else{
    	var count = 0
         while((count <  BtOper_Json.cycletimes) && (BtOper_Json.cycletest > 0))
        {
            var index = 0;
             while((BtOper_Json.cycletimes > 0 )
			&& (BtOper_Json.cycletest > 0))
            while(index < BtOper_Json.groupcount )
            {
               await VerifyPwd_(BtOper_Json.commaddr + index,BtOper_Json.pwd, BtOper_Json.waitack,logflag, BtOper_Json.waittimeout);
               index++;
               await TransSleep(BtOper_Json.rundelay);

            }
           count++;
        }
    }
}

 async function TimerAmend_( bgaddr,  endaddr,  waitexcue,  logflag,  timeout)
{
	var rtv = 0;
    cmd_excute = 0;
    var log_show = "时钟校准地址范围" +  bgaddr +" 到 " + endaddr;
    ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
    var datas = new Array();
    datas.push((bgaddr & 0xFF));
    datas.push(((bgaddr >>8) & 0xFF));
	datas.push((endaddr & 0xFF));
    datas.push(((endaddr >>8) & 0xFF));
    cmd_excute = 0;
    BtSendBytes(PBCmd.PBGetSendCmd(EProCmdV.AMEND_,datas),logflag);
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(EProCmdV.AMEND_,timeout);
    }
    return rtv;
}

 async function  TimerAmend( logflag)
{
    var amend_endaddr = BtOper_Json.begaddr + BtOper_Json.amendcount;
    cyclerun_flag = true;
    var count = 0;
    do
    {
         var bgaddr = BtOper_Json.begaddr;
         do
         {
             var endaddr = bgaddr + BtOper_Json.groupcount;
             await TimerAmend_(bgaddr,endaddr,true,logflag,1200);
             await TransSleep(BtOper_Json.rundelay);
             bgaddr = endaddr;
         }while((endaddr < amend_endaddr) && (BtOper_Json.cycletest > 0));
         count ++;
    } while((count <  BtOper_Json.cycletimes) && (BtOper_Json.cycletest > 0))
    return 0;
}

 async function  InspectSlave_( bgaddr,  endaddr,  waitexcue,  logflag,  timeout)
{
    var log_show = "巡检地址范围" +  bgaddr +" 到 " + (endaddr -1);
    ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
    var datas = new Array();
    datas.push((bgaddr & 0xFF));
    datas.push(((bgaddr >>8) & 0xFF));
	datas.push((endaddr & 0xFF));
    datas.push(((endaddr >>8) & 0xFF));
    BtSendBytes(PBCmd.PBGetSendCmd(EProCmdV.INSPECT_SLAVE_,datas),logflag);
    if(!waitexcue)
        return 0;
    cmd_excute = 0;
    var state_addr;
    var rtv = await WaitCmdExcu(EProCmdV.INSPECT_SLAVE_,timeout);
    if(rtv == 0)
    {
        if(BtOper_Json.inspect_buf.length > 0)
        {
        
            PBCmd.InspectAnaly(bgaddr,endaddr-bgaddr,BtOper_Json.inspect_buf) ;
            var log_show = "准备起爆有 " + PBCmd.ok_addr.length + "发";
            ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);
            var index = 0;
            log_show = "";
            while(index < PBCmd.ok_addr.length)
            {
                state_addr = PBCmd.ok_addr[index++];
                if(state_addr > endaddr)
                    continue;
                log_show +=  state_addr + " ";

            }
            if(log_show.length > 2)
                ShowCommMes(log_show,MesgType.MESG_TYPE_INFO);

            log_show = "无法起爆有 " +PBCmd.ng_addr.length + "发";
            ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
            index = 0;
            log_show = "";
            while(index < PBCmd.ng_addr.length)
            {

                state_addr = PBCmd.ng_addr[index++] ;
                if(state_addr > endaddr)
                    continue;
                log_show +=  state_addr + " ";
            }
            if(log_show.length > 1)
                 ShowCommMes(log_show,MesgType.MESG_TYPE_ERROR);
        }
    }
	
    return 0;
}

async function InspectSlave(logflag)
{

    var cycleInspectRunCount = parseInt(((BtOper_Json.inspectcount + BtOper_Json.groupcount-1)  / BtOper_Json.groupcount));
    var count = 0;
    var bgaddr ;
    var endaddr;
    do
    {
        var runcount = 0;
        bgaddr = BtOper_Json.begaddr;
        do
        {
            endaddr = BtOper_Json.groupcount + bgaddr;
            if(endaddr > 1023)
            {
                endaddr = 1023;
            }
            await InspectSlave_(bgaddr,endaddr,true,logflag,BtOper_Json.waittimeout);
            runcount ++;
            bgaddr = endaddr;
            await TransSleep(BtOper_Json.rundelay);
        }while(runcount < cycleInspectRunCount && (BtOper_Json.cycletest > 0));

        count ++;
    }while ((count <  BtOper_Json.cycletimes) && (BtOper_Json.cycletest > 0));
   return 0;
}

async function SlaveCheckSelf( type,  waitexcue,  logflag,  timeout)
{
	var rtv = 0;
    var data = [];
    data[0] = type & 0xFF;
    cmd_excute = 0;
    BtSendBytes(PBCmd.PBGetSendCmd(EProCmdV.BASE_CHECK_,data),logflag);
    if(waitexcue)
    {
         rtv = await WaitCmdExcu(EProCmdV.BASE_CHECK_,timeout);
    }
    return rtv;
}

async function BroadCastCmd_( cmd,  waitexcue,  logflag,  timeout)
{
    var data_null = [];
    var rtv = 0;
    data_null = PBCmd.PBGetSendCmd(cmd,data_null);
    cmd_excute = 0;
    BtSendBytes(data_null,logflag);
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(cmd,timeout);
    }
    return rtv;
}

async function BroadCastCmd(logflag)
{
    await BroadCastCmd_(BtOper_Json.opercmd,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
}

async function BroadCastCmdSet( cmd,  param,  waitexcue,  logflag,  timeout)
{
  	var rtv = 0;
    var data = PBCmd.PBGetSendCmd(cmd,param);
    cmd_excute = 0;
    BtSendBytes(data,logflag);
    if(waitexcue)
    {
       rtv = await WaitCmdExcu(cmd,timeout);
    }
    return rtv;
}

async function  BusMode( mode,  s_flag,  waitexcue,  logflag,  timeout)
{
  var rtv = 0;
   var data = new Array();
   if(BtOper_Json.busmode == 2)
   {
   	data.push(0x01);
   	BtOper_Json.busmode = 1;
   }else{
   	BtOper_Json.busmode = 2;
   	data.push(0x02);
   }
   SwitchBusMode(BtOper_Json.busmode);
    data.push(mode);
    data = PBCmd.PBGetSendCmd(EProCmdV.SET_BUS_MODE_,data);
    BtSendBytes(data,logflag);
    cmd_excute = 0;
    if(waitexcue)
    {
        rtv = await WaitCmdExcu(EProCmdV.SET_BUS_MODE_,timeout);
    }
    return rtv;
}

async function SetHartBeat(logflag)
{
	var heart_beat_delay = 0;
	var data = [];
	if(BtOper_Json.heartbeat == 1)
	{
		heart_beat_delay = 0xFFFF;
		BtOper_Json.heartbeat = 0;
	}
	else{
		heart_beat_delay = parseInt(BtOper_Json.rundelay / 10);
		BtOper_Json.heartbeat = 1; 
	}
   	SwitchHartBeat(BtOper_Json.heartbeat);
    data[0] = heart_beat_delay & 0xFF;
    data[1] = (heart_beat_delay  >> 8 ) & 0xFF;

    data = PBCmd.PBGetSendCmd(EProCmdV.MASTER_HARTBEAT_,data);
    BtSendBytes(data,logflag);
}
//addr 通讯地址
//code 功能码
//cmd 功能码参数 没有传入负数
//frame_type 数据帧类型 0 非广播帧 1 广播帧
//ack_len 读取数据长度 0 读取应答位 大于0 数据长度
//param 命令码或功能码参数
//ack_wait 起始读信号等待时间
//ack_try  超时尝试次数
//waitexcue 等待命令执行完成
//timeout  命令超时时间ms

async function SlaveCmd( addr,  code,  cmd,  frame_type,  ack_len,  param,  ack_wait,  ack_try,  waitexcue,  logflag,  timeout)
{
    var cmd_buf = new Array();
    var c_slavecheck = 0;
    var rtv = 0;
    cmd_buf = PBCmd.SlaveCmd(code,cmd,addr,param);
    c_slavecheck = PBCmd.SlaveCheck(cmd_buf);
    while(true)
    {
        if(((cmd_buf.length +1) % 4) == 0)
        {
            break;
        }
        cmd_buf.push(0x00);
    }
    cmd_buf.push(c_slavecheck);
    cmd_buf = PBCmd.PBSlaveSendBuf(frame_type,ack_len,ack_wait,ack_try,cmd_buf);
    cmd_excute = 0;
    BtSendBytes(cmd_buf,logflag);
    if(waitexcue)
    {
       rtv = await WaitCmdExcu(EProCmdV.SLAVE_BUS_CMD,timeout);
    }
    return rtv;
}
async function BroadCast_SetAddr(logflag)
{
	var data = [];
	data[0] = BtOper_Json.commaddr & 0xFF;
	data[1] = (BtOper_Json.commaddr >> 8) & 0xFF;
	await BroadCastCmdSet(EProCmdV.BC_WRITE_ADDR_,data,  BtOper_Json.waitack, logflag, BtOper_Json.waittimeout);
}
async function SetSuperTime(logflag)
{
	var param = [];
	param[0] = BtOper_Json.setdelay & 0xFF;
	param[1] = (BtOper_Json.setdelay >> 8) & 0xFF;
	BtOper_Json.setparam = param;
	WriteByAddr_(BtOper_Json.opercmd,BtOper_Json.commaddr,BtOper_Json.setparam,BtOper_Json.waitack,logflag,BtOper_Json.waittimeout);
}
async function SendCmd(txflag)
{

    switch(BtOper_Json.opercmd)
    {
    case EProCmdV.WRITE_UID_: await WriteUID(txflag);break;
    case EProCmdV.WRITE_PWD_: await WritePwd(txflag);break;
    case EProCmdV.READ_UID_: await ReadByAddr(txflag) ;break;
    case EProCmdV.READ_PWD_: await ReadByUid(txflag) ;break;
    case EProCmdV.VERIFY_UID_: await VerifyUID(txflag); break;
    case EProCmdV.VERIFY_PWD_: await VerifyPwd(txflag);break;
    case EProCmdV.READ_STATE_: await ReadByAddr(txflag) ;break;
    case EProCmdV.SET_DELAY_: await WriteDelay(txflag) ;break;
    case EProCmdV.SET_DELAY_ALL_: await  WriteDelay(1,BtOper_Json.setdelay ,BtOper_Json.waitack,BtOper_Json.waittimeout) ;break;
    case EProCmdV.WRITE_ADDR_: await WriteAddr(txflag);break;
    case EProCmdV.EQUEL_UID_FEA: await WriteAddr(txflag);break;
    case EProCmdV.CHARGE_: await BroadCastCmd(txflag);break;
    case EProCmdV.AMEND_: await TimerAmend(txflag);break;
    case EProCmdV.READ_ADDR_:  await ReadByUid(txflag) ;break;
    case EProCmdV.BOOM_: await BroadCastCmd(txflag);break;
    case EProCmdV.READ_AMEND_V_: await ReadByAddr(txflag) ;break;
    case EProCmdV.SLAVE_ONLINE_: await ReadByAddr(txflag);break;
    case EProCmdV.ALL_SLEEP: await AddrCmd(txflag);break;
    case EProCmdV.DISCHARGE_: await BroadCastCmd(txflag);break;
    case EProCmdV.INSPECT_SLAVE_: await InspectSlave(true);break;
    case EProCmdV.BASE_CHECK_: await SlaveCheckSelf(BtOper_Json.setparam[0],BtOper_Json.waitack,txflag,BtOper_Json.waittimeout);break;
    case EProCmdV.READ_DELAY_: await ReadByAddr(txflag) ;break;
    case EProCmdV.READ_S_BUS_V_: await ReadByAddr(txflag) ;break;
    case EProCmdV.READ_FIRWARE_: await ReadByAddr(txflag);break;
    case EProCmdV.SET_PSWD_FLAG: await ReadByAddr(txflag);break;
    case /*BUS_MODE_B_*/ EProCmdV.SET_BUS_MODE_: await BusMode(2,true,BtOper_Json.waitack,txflag,BtOper_Json.waittimeout);break;
    //case BUS_MODE_R_:BusMode(1,true,cmdclass.waitack,txflag,cmdclass.timeout);break;
    case EProCmdV.MASTER_HARTBEAT_:  await SetHartBeat(txflag);break;
    case EProCmdV.WAIT_ONLINE_: await  WaitSlaveOnline(timeout);break;
    case EProCmdV.WAIT_RELEASE_: await WaitSlaveRelease(timeout);break;
    case EProCmdV.TRANS_DELAY_:  await await TransSleep(BtOper_Json.setdelay);break;
    case EProCmdV.SET_SUPER_T_:   await SetSuperTime(txflag) ;break;
    case EProCmdV.BC_WRITE_ADDR_: await BroadCast_SetAddr(true);break;
    default:break;
    }
   
}
