var PBCmd = new Object;
PBCmd.flow_num = 1;
PBCmd.PBGetCheck =  function (data)
 {
     var index = 0;
     var checkA = 0;
     var checkB = 0;
     while(index < data.length)
     {
 
         checkA += data[index];
         checkA &= 0xFF;
         checkB += checkA;
         checkB &= 0xFF;
         index++;
     }
	 var checsum = [checkA,checkB];
	 return checsum;
 }
 PBCmd.SlaveCheck = function(data )
 {
     var crc = 0x00;
     var len = data.length();
 	for(var index = 0; index < len; index++)
      {
        crc ^= data[index];
 	   crc &= 0xFF;
      }
      return crc;
 }
 PBCmd.SlaveCmd = function ( code,  cmd,  addr,  param)
 {
 	var cmd_buf = new Array();
 	var head = ((addr << 6) + code) & 0xFFFF;
 	cmd_buf.push(head&0xFF);
 	cmd_buf.push((head >> 8)&0xFF);
     if(cmd > 0)
     {
         cmd_buf.push(cmd);
     }
     cmd_buf = cmd_buf.concat(param);
     return cmd_buf;
 }
 
 
 PBCmd.PBGetSendCmd = function(cmd, data)
 {
     var head_=[0x59,0x6D,0,0];
	 var cmd_buf = new Array();
	 var len_ = data.length+3;
	 var data_buf = new Array();
	 data_buf.push(cmd);
	 data_buf.push(this.flow_num & 0xFF);
	 data_buf.push((this.flow_num >> 8) & 0xFF);
	 this.flow_num++;
	 data_buf = data_buf.concat(data);
	 var check_sum = this.PBGetCheck(data_buf);
	 cmd_buf = cmd_buf.concat(head_);
	 cmd_buf[2] = len_ & 0xFF ;
	 cmd_buf[3] = (len_ >>8) & 0xFF;
	 cmd_buf = cmd_buf.concat(data_buf);
	 cmd_buf = cmd_buf.concat(check_sum);
     return cmd_buf;
 }
 
 PBCmd.PBAnalyResDataLen = function(data)
{
    if(data.length < 7)
        return -1;
    var l_ = data.at(3);
    var h_ = data.at(4);
    var len = h_ & 0xFF;
    len = (len <<  8)+ (l_ & 0xFF);
    return len;
}

PBCmd.PBAnalyResData = function(data)
{
	var res_bytes = new Array();
    var size_ = data.length;
	var data_buf = data.slice(4,data.length-2);
    var chen_sum = this.PBGetCheck(data_buf);
    if((data.at(size_ -1) != chen_sum[1])
            || (data.at(size_ -2) != chen_sum[0])
            || (data.at(0) != 'Y'))
    {
        return res_bytes;
    }
    var data_len = this.PBAnalyResDataLen(data);
	res_bytes = data.slice(6,6+data_len);
	return res_bytes;
}

PBCmd.AddrReadCmd = function( cmd,  addr)
{
    var data = new Array();
    data.push( (addr & 0xFF));
    data.push(((addr >> 8) & 0xFF));
    data = this.PBGetSendCmd(cmd,data);
    return data;
}

PBCmd.AddrWriteCmd = function( cmd,  addr,  data)
{
    var data_ = new Array();
    data_.push((addr & 0xFF));
    data_.push(((addr >> 8) & 0xFF));
    data_ = data_.concat(data);
    data_ = this.PBGetSendCmd(cmd,data_);
    return data_;
}
/*
PBCmd.AddrWriteCmd = function( cmd,  addr,  param)
{
    var data_ = new Array();
    data_.push( (addr & 0xFF));
    data_.push( ((addr >> 8) & 0xFF));
    data_.push( (param & 0xFF));
    data_.push(((param >> 8) & 0xFF));
    data_ =this.PBGetSendCmd(cmd,data_);
    return data_;
}*/

PBCmd.UidOperCmd = function( cmd,  uid_arr,  param)
{
    var data = new Array();
	data = data.concat(uid_arr);
	data = data.concat(param);
    data = this.PBGetSendCmd(cmd,data);
    return data;
}

PBCmd.UidWriteAddr = function( uid_arr,  addr,  cmd)
{
    param = new Array();
    param.push((addr & 0xFF));
    param.push(((addr >> 8) & 0xFF));
    return this.UidOperCmd(cmd,uid_arr,param);
}

PBCmd.PBSlaveSendBuf = function( frame_type,  ack_len, ack_wait,  ack_try,  data)
{
    
	var param = new Array();
	param.push(frame_type);
	param.push(ack_len);
	param.push(ack_try);
	param.push(ack_wait);
    param = param.concat(data);
    return  this.PBGetSendCmd(SLAVE_BUS_CMD,param);
}

PBCmd.GetSlaveCmd = function( addr,  cmd, param)
{
	var head_ = (addr << 6) | cmd;
	head_ &= 0xFFFF;
    var data = new Array();
    data.push(byte2int_un.datas[1]);
    data.push(byte2int_un.datas[0]);
    data = data.concat(param);
	
    return data;

}

 PBCmd.SendBroadcast = function( cmd)
{
    var data_null = new Array();
    return this.PBGetSendCmd(cmd,data_null);
}

PBCmd.InspectAnaly = function ( bgaddr,  count,  res)
{
    var index1,index2;
    var data;
    var count_ = 0;
    index1 = 0;
	this.ok_addr = new Array();
	this.ng_addr = new Array();
    while (index1 < res.length)
    {
        data = res[index1++];
        index2 = 0;
        while(index2 < 8)
        {
            if((data & 0x01) > 0)
            {
                this.ok_addr.push(bgaddr);
            }else {

                this.ng_addr.push(bgaddr);
            }
            data >>= 1;
            bgaddr++;
            count_++;
            index2++;
            if(count_ >= count)
                return;
        }
    }
}

 
 