#include "soft_bms.h"
#include "main.h"
#include "soft_engine.h"
#include "soft_bms.h"
#include "soft_okcell.h"
#include "soft_tattu.h"
#include "soft_poweramp.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "soft_herewin.h"
#include "soft_crc.h"
#include "soft_p_2_c.h"
#include "soft_seed_device.h"
#include "soft_version.h"
#include "qingxie_bms.h"


Vkbms_Device Device1 = {.Vkbms_Link.connect_status = COMP_NOEXIST};
Vkbms_Device Device2 = {.Vkbms_Link.connect_status = COMP_NOEXIST};


struct BMS_DATA bms_data = {.bms_ss = 0xFFFF}, bms_data2 = {.bms_ss = 0xFFFF};

/**
  * @file    update_bms_data
  * @brief   更新电池信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void update_bms_data( void )
{
    //正方电池
    Check_dev_link(&okcell_device1.Okcell_Link,5000,(char *)&okcell_device1.okcell_inf,sizeof(_CELL_INF));
    Check_dev_link(&okcell_device2.Okcell_Link,5000,(char *)&okcell_device2.okcell_inf,sizeof(_CELL_INF));
    //格式电池
    Check_dev_link(&tattu_device1.Tattu_Link,5000,(char *)&tattu_device1.ttu_inf,sizeof(_TATTU_INFO));
    Check_dev_link(&tattu_device2.Tattu_Link,5000,(char *)&tattu_device2.ttu_inf,sizeof(_TATTU_INFO));
    //极目电池
    //check_poweramp_link();
    //海盈电池
    Check_dev_link(&Herewin_Link,5000,(char *)&herewin_info,sizeof(herewin_bms));
    //VK电池
    Check_dev_link(&Device1.Vkbms_Link,5000,(char *)&Device1,sizeof(Vkbms_Device));
    Check_dev_link(&Device2.Vkbms_Link,5000,(char *)&Device2,sizeof(Vkbms_Device));
    
    if ( okcell_device1.Okcell_Link.connect_status == COMP_NORMAL || okcell_device2.Okcell_Link.connect_status == COMP_NORMAL )
    {
        Dev.Bms_Link.connect_status = COMP_NORMAL;

        //第一组电池
        if ( okcell_device1.Okcell_Link.connect_status == COMP_NORMAL )
        {
            bms_data.bms_cycle = okcell_device1.okcell_inf.cell_D3.cell_d3[4];
            if(okcell_device1.okcell_inf.cell_D0.cell_d0[6] >= 18) //正方超18S电池10mv发送
            {
                bms_data.bms_volt = okcell_device1.okcell_inf.cell_D0.cell_d0[0];
            }
            else
            {
                bms_data.bms_volt = okcell_device1.okcell_inf.cell_D0.cell_d0[0] / 10;
            }
            bms_data.bms_temp = okcell_device1.okcell_inf.cell_D0.cell_d0[2];
            bms_data.bms_vs = okcell_device1.okcell_inf.cell_D0.cell_d0[3];
            //正方电池只关心飞控对接异常不让解锁，不关心异常5
            bms_data.bms_ss = ( ( okcell_device1.okcell_inf.cell_D0.cell_d0[5] >> 8 ) != 5 ) ? ( okcell_device1.okcell_inf.cell_D0.cell_d0[5] >> 8 ) : 0;
            bms_data.bms_num = okcell_device1.okcell_inf.cell_D0.cell_d0[6];

            bms_data.bms_v1 = okcell_device1.okcell_inf.cell_D0.cell_d0[7];
            bms_data.bms_v2 = okcell_device1.okcell_inf.cell_D0.cell_d0[8];
            bms_data.bms_v3 = okcell_device1.okcell_inf.cell_D0.cell_d0[9];
            bms_data.bms_v4 = okcell_device1.okcell_inf.cell_D0.cell_d0[10];
            bms_data.bms_v5 = okcell_device1.okcell_inf.cell_D0.cell_d0[11];
            bms_data.bms_v6 = okcell_device1.okcell_inf.cell_D0.cell_d0[12];
            bms_data.bms_v7 = okcell_device1.okcell_inf.cell_D0.cell_d0[13];
            bms_data.bms_v8 = okcell_device1.okcell_inf.cell_D0.cell_d0[14];
            bms_data.bms_v9 = okcell_device1.okcell_inf.cell_D0.cell_d0[15];
            bms_data.bms_v10 = okcell_device1.okcell_inf.cell_D0.cell_d0[16];
            bms_data.bms_v11 = okcell_device1.okcell_inf.cell_D0.cell_d0[17];
            bms_data.bms_v12 = okcell_device1.okcell_inf.cell_D0.cell_d0[18];
            bms_data.bms_v13 = okcell_device1.okcell_inf.cell_D0.cell_d0[19];
            bms_data.bms_v14 = okcell_device1.okcell_inf.cell_D0.cell_d0[20];
            bms_data.bms_v15 = okcell_device1.okcell_inf.cell_D0.cell_d0[21];
            bms_data.bms_v16 = okcell_device1.okcell_inf.cell_D0.cell_d0[22];
            bms_data.bms_v17 = okcell_device1.okcell_inf.cell_D0.cell_d0[23];
            bms_data.bms_v18 = okcell_device1.okcell_inf.cell_D0.cell_d0[24];
            bms_data.bms_v19 = okcell_device1.okcell_inf.cell_D0.cell_d0[25];
            bms_data.bms_v20 = okcell_device1.okcell_inf.cell_D0.cell_d0[26];
            bms_data.bms_v21 = okcell_device1.okcell_inf.cell_D0.cell_d0[27];
            bms_data.bms_v22 = okcell_device1.okcell_inf.cell_D0.cell_d0[28];
            bms_data.bms_v23 = okcell_device1.okcell_inf.cell_D0.cell_d0[29];
            bms_data.bms_v24 = okcell_device1.okcell_inf.cell_D0.cell_d0[30];
            bms_data.bms_v25 = okcell_device1.okcell_inf.cell_D0.cell_d0[31];
            bms_data.bms_v26 = okcell_device1.okcell_inf.cell_D0.cell_d0[32];

            bms_data.bms_ids[0] = okcell_device1.okcell_inf.cell_P3.cell_p3[0];
            bms_data.bms_ids[1] = okcell_device1.okcell_inf.cell_P3.cell_p3[1];
            for(int i = 1; i< 11; i++){
                // bms_data.bms_ids[2*i] = ((okcell_device1.okcell_inf.cell_P3.cell_p3[2 + i] >> 4) & 0xf) + '0';
                // bms_data.bms_ids[2*i+1] = (okcell_device1.okcell_inf.cell_P3.cell_p3[2 + i] & 0xf )+ '0';
                bms_data.bms_ids[2*i] = ((okcell_device1.okcell_inf.cell_P3.cell_p3[1 + i] >> 4) & 0xf) + '0';
                bms_data.bms_ids[2*i+1] = (okcell_device1.okcell_inf.cell_P3.cell_p3[1 + i] & 0xf )+ '0';
            }
            //memcpy( bms_data.bms_ids, "okcell", 6 );
            bms_data.bms_ac = okcell_device1.okcell_inf.cell_D0.cell_d0[1];
            bms_data.serial_num = FAC_OKCELL;

            //注册
            if(StrArrayNull( &bms_data.bms_ids[0], sizeof( bms_data.bms_ids ) ) == false && dev_bms1.regist.sn == false)
                regist_dev_info(&dev_bms1,DEVICE_BMS1,false,(char *)bms_data.bms_ids,32,NULL,0,NULL,0,"okcell1",8);
        }
        else
        {
            memset( &bms_data, 0, sizeof( bms_data ) );
        }

        //第二组电池
        if ( okcell_device2.Okcell_Link.connect_status == COMP_NORMAL )
        {
            bms_data2.bms_cycle = okcell_device2.okcell_inf.cell_D3.cell_d3[4];
            if(okcell_device2.okcell_inf.cell_D0.cell_d0[6] >= 18)
            {
                bms_data2.bms_volt = okcell_device2.okcell_inf.cell_D0.cell_d0[0];
            }
            else
            {
                bms_data2.bms_volt = okcell_device2.okcell_inf.cell_D0.cell_d0[0] / 10;
            }
            bms_data2.bms_temp = okcell_device2.okcell_inf.cell_D0.cell_d0[2];
            bms_data2.bms_vs = okcell_device2.okcell_inf.cell_D0.cell_d0[3];
            //正方电池只关心飞控对接异常不让解锁，不关心异常5
            bms_data2.bms_ss = ( ( okcell_device2.okcell_inf.cell_D0.cell_d0[5] >> 8 ) != 5 ) ? ( okcell_device2.okcell_inf.cell_D0.cell_d0[5] >> 8 ) : 0;
            bms_data2.bms_num = okcell_device2.okcell_inf.cell_D0.cell_d0[6];

            bms_data2.bms_v1 = okcell_device2.okcell_inf.cell_D0.cell_d0[7];
            bms_data2.bms_v2 = okcell_device2.okcell_inf.cell_D0.cell_d0[8];
            bms_data2.bms_v3 = okcell_device2.okcell_inf.cell_D0.cell_d0[9];
            bms_data2.bms_v4 = okcell_device2.okcell_inf.cell_D0.cell_d0[10];
            bms_data2.bms_v5 = okcell_device2.okcell_inf.cell_D0.cell_d0[11];
            bms_data2.bms_v6 = okcell_device2.okcell_inf.cell_D0.cell_d0[12];
            bms_data2.bms_v7 = okcell_device2.okcell_inf.cell_D0.cell_d0[13];
            bms_data2.bms_v8 = okcell_device2.okcell_inf.cell_D0.cell_d0[14];
            bms_data2.bms_v9 = okcell_device2.okcell_inf.cell_D0.cell_d0[15];
            bms_data2.bms_v10 = okcell_device2.okcell_inf.cell_D0.cell_d0[16];
            bms_data2.bms_v11 = okcell_device2.okcell_inf.cell_D0.cell_d0[17];
            bms_data2.bms_v12 = okcell_device2.okcell_inf.cell_D0.cell_d0[18];
            bms_data2.bms_v13 = okcell_device2.okcell_inf.cell_D0.cell_d0[19];
            bms_data2.bms_v14 = okcell_device2.okcell_inf.cell_D0.cell_d0[20];
            bms_data2.bms_v15 = okcell_device2.okcell_inf.cell_D0.cell_d0[21];
            bms_data2.bms_v16 = okcell_device2.okcell_inf.cell_D0.cell_d0[22];
            bms_data2.bms_v17 = okcell_device2.okcell_inf.cell_D0.cell_d0[23];
            bms_data2.bms_v18 = okcell_device2.okcell_inf.cell_D0.cell_d0[24];
            bms_data2.bms_v19 = okcell_device2.okcell_inf.cell_D0.cell_d0[25];
            bms_data2.bms_v20 = okcell_device2.okcell_inf.cell_D0.cell_d0[26];
            bms_data2.bms_v21 = okcell_device2.okcell_inf.cell_D0.cell_d0[27];
            bms_data2.bms_v22 = okcell_device2.okcell_inf.cell_D0.cell_d0[28];
            bms_data2.bms_v23 = okcell_device2.okcell_inf.cell_D0.cell_d0[29];
            bms_data2.bms_v24 = okcell_device2.okcell_inf.cell_D0.cell_d0[30];
            bms_data2.bms_v25 = okcell_device2.okcell_inf.cell_D0.cell_d0[31];
            bms_data2.bms_v26 = okcell_device2.okcell_inf.cell_D0.cell_d0[32];

            bms_data2.bms_ids[0] = okcell_device1.okcell_inf.cell_P3.cell_p3[0];
            bms_data2.bms_ids[1] = okcell_device1.okcell_inf.cell_P3.cell_p3[1];
            for(int i = 1; i< 11; i++){
                bms_data2.bms_ids[2*i] = ((okcell_device2.okcell_inf.cell_P3.cell_p3[1 + i] >> 4) & 0xf) + '0';
                bms_data2.bms_ids[2*i+1] = (okcell_device2.okcell_inf.cell_P3.cell_p3[1 + i] & 0xf )+ '0';
            }
            //memcpy( bms_data2.bms_ids, "okcell2", 7 );
            bms_data2.bms_ac = okcell_device2.okcell_inf.cell_D0.cell_d0[1];
            bms_data2.serial_num = FAC_OKCELL;

            if(StrArrayNull( &bms_data2.bms_ids[0], sizeof( bms_data2.bms_ids ) ) == false && dev_bms2.regist.sn == false)
                regist_dev_info(&dev_bms2,DEVICE_BMS2,false,(char *)bms_data2.bms_ids,32,NULL,0,NULL,0,"okcell2",8);
        }
        else
        {
            memset( &bms_data2, 0, sizeof( bms_data2 ) );
        }
    }
    else if(tattu_device1.Tattu_Link.connect_status == COMP_NORMAL || tattu_device2.Tattu_Link.connect_status == COMP_NORMAL)
    {
        Dev.Bms_Link.connect_status = COMP_NORMAL;
        
        if(tattu_device1.Tattu_Link.connect_status  == COMP_NORMAL)
        {
            group_num = 1;
            bms_data.bms_cycle = tattu_device1.ttu_inf.tattu_cycle;
            bms_data.bms_volt = tattu_device1.ttu_inf.tattu_volt /10;
            bms_data.bms_temp = tattu_device1.ttu_inf.tattu_temp * 10;
            bms_data.bms_vs = tattu_device1.ttu_inf.tattu_perc;
            bms_data.bms_ss = 0;//ttu_inf.def_info[24] & 0xc00;//格式电池只关心部分报警不让解锁
            bms_data.bms_num = tattu_device1.ttu_inf.tattu_cnum;
            bms_data.bms_v1 = tattu_device1.ttu_inf.tattu_v1;
            bms_data.bms_v2 = tattu_device1.ttu_inf.tattu_v2;
            bms_data.bms_v3 = tattu_device1.ttu_inf.tattu_v3;
            bms_data.bms_v4 = tattu_device1.ttu_inf.tattu_v4;
            bms_data.bms_v5 = tattu_device1.ttu_inf.tattu_v5;
            bms_data.bms_v6 = tattu_device1.ttu_inf.tattu_v6;
            bms_data.bms_v7 = tattu_device1.ttu_inf.tattu_v7;
            bms_data.bms_v8 = tattu_device1.ttu_inf.tattu_v8;
            bms_data.bms_v9 = tattu_device1.ttu_inf.tattu_v9;
            bms_data.bms_v10 = tattu_device1.ttu_inf.tattu_v10;
            bms_data.bms_v11 = tattu_device1.ttu_inf.tattu_v11;
            bms_data.bms_v12 = tattu_device1.ttu_inf.tattu_v12;
            bms_data.bms_v13 = tattu_device1.ttu_inf.tattu_v13;
            bms_data.bms_v14 = tattu_device1.ttu_inf.tattu_v14;
            memcpy(bms_data.bms_ids,&tattu_device1.ttu_inf.tuattu_id[0],16);
            //memcpy(bms_data.bms_ids, "Tattu1", 6);
            bms_data.bms_ac = tattu_device1.ttu_inf.tattu_cur;
            bms_data.serial_num = FAC_TATTU;

            if(StrArrayNull( &bms_data.bms_ids[0], sizeof( bms_data.bms_ids ) ) == false && dev_bms1.regist.sn == false)
                regist_dev_info(&dev_bms1,DEVICE_BMS1,false,(char *)bms_data.bms_ids,16,NULL,0,NULL,0,"tattu1",7);
        }
        else
        {
            memset( &bms_data, 0, sizeof(bms_data));
        }

        if(tattu_device2.Tattu_Link.connect_status == COMP_NORMAL)
        {
            group_num = 2;
            bms_data2.bms_cycle = tattu_device2.ttu_inf.tattu_cycle;
            bms_data2.bms_volt = tattu_device2.ttu_inf.tattu_volt / 10;
            bms_data2.bms_temp = tattu_device2.ttu_inf.tattu_temp * 10;
            bms_data2.bms_vs = tattu_device2.ttu_inf.tattu_perc;
            bms_data2.bms_ss = 0;//ttu_inf.def_info[24] & 0xc00;//格式电池只关心部分报警不让解锁
            bms_data2.bms_num = tattu_device2.ttu_inf.tattu_cnum;
            bms_data2.bms_v1 = tattu_device2.ttu_inf.tattu_v1;
            bms_data2.bms_v2 = tattu_device2.ttu_inf.tattu_v2;
            bms_data2.bms_v3 = tattu_device2.ttu_inf.tattu_v3;
            bms_data2.bms_v4 = tattu_device2.ttu_inf.tattu_v4;
            bms_data2.bms_v5 = tattu_device2.ttu_inf.tattu_v5;
            bms_data2.bms_v6 = tattu_device2.ttu_inf.tattu_v6;
            bms_data2.bms_v7 = tattu_device2.ttu_inf.tattu_v7;
            bms_data2.bms_v8 = tattu_device2.ttu_inf.tattu_v8;
            bms_data2.bms_v9 = tattu_device2.ttu_inf.tattu_v9;
            bms_data2.bms_v10 = tattu_device2.ttu_inf.tattu_v10;
            bms_data2.bms_v11 = tattu_device2.ttu_inf.tattu_v11;
            bms_data2.bms_v12 = tattu_device2.ttu_inf.tattu_v12;
            bms_data2.bms_v13 = tattu_device2.ttu_inf.tattu_v13;
            bms_data2.bms_v14 = tattu_device2.ttu_inf.tattu_v14;
            memcpy(bms_data2.bms_ids,&tattu_device2.ttu_inf.tuattu_id[0],16);
            //memcpy(bms_data2.bms_ids, "Tattu2", 6);
            bms_data2.bms_ac = tattu_device2.ttu_inf.tattu_cur;
            bms_data2.serial_num = FAC_TATTU;

            if(StrArrayNull( &bms_data2.bms_ids[0], sizeof( bms_data2.bms_ids ) ) == false && dev_bms2.regist.sn == false)
                regist_dev_info(&dev_bms2,DEVICE_BMS2,false,(char *)bms_data2.bms_ids,16,NULL,0,NULL,0,"tattu2",7);
        }
        else
        {
            memset( &bms_data2, 0, sizeof( bms_data2 ) );
        }
    }
    // else if ( Poweramp_Link.connect_status == COMP_NORMAL )
    // {
    //     Dev.Bms_Link.connect_status = COMP_NORMAL;
    //     group_num = 1;

    //     //ID不足12位的从buf[0]开始存放
    //     bms_data.bms_cycle = poweramp_sys.amp_cycle;
    //     bms_data.bms_volt = ((uint16_t)poweramp_inf.amp_data[0]) / 10;
    //     bms_data.bms_temp = poweramp_inf.amp_data[3];
    //     bms_data.bms_vs = poweramp_inf.amp_data[5] / 10;
    //     bms_data.bms_ss = poweramp_inf.amp_data[22];
    //     bms_data.bms_num = 13;
    //     bms_data.bms_v1 = poweramp_inf.amp_data[9];
    //     bms_data.bms_v2 = poweramp_inf.amp_data[10];
    //     bms_data.bms_v3 = poweramp_inf.amp_data[11];
    //     bms_data.bms_v4 = poweramp_inf.amp_data[12];
    //     bms_data.bms_v5 = poweramp_inf.amp_data[13];
    //     bms_data.bms_v6 = poweramp_inf.amp_data[14];
    //     bms_data.bms_v7 = poweramp_inf.amp_data[15];
    //     bms_data.bms_v8 = poweramp_inf.amp_data[16];
    //     bms_data.bms_v9 = poweramp_inf.amp_data[17];
    //     bms_data.bms_v10 = poweramp_inf.amp_data[18];
    //     bms_data.bms_v11 = poweramp_inf.amp_data[19];
    //     bms_data.bms_v12 = poweramp_inf.amp_data[20];
    //     bms_data.bms_v13 = poweramp_inf.amp_data[21];
    //     memcpy( bms_data.bms_ids, "poweramp", 8 );
    //     bms_data.bms_ac = poweramp_inf.amp_data[1];
    //     bms_data.serial_num = FAC_POWERAMP;

    //     if(dev_bms1.regist.dev == false)
    //         regist_dev_info(&dev_bms1,DEVICE_BMS1,false,NULL,0,NULL,0,NULL,0,"poweramp",9);
    // }
    else if ( Herewin_Link.connect_status == COMP_NORMAL )
    {
        Dev.Bms_Link.connect_status = COMP_NORMAL;
        group_num = 1;

        bms_data.bms_cycle = herewin_info.circulation_num;//海盈电池循环次数
        bms_data.bms_volt = herewin_info.total_vol;
        
        if(herewin_info.get_temp_flag == true)
            bms_data.bms_temp = ( ( herewin_info.battery_temp[0] + herewin_info.battery_temp[1] + herewin_info.battery_temp[2] ) / 3 - 40 ) * 10;
        
        bms_data.bms_vs = herewin_info.SOC_info;

        bms_data.bms_ss = herewin_info.warn_info;
        bms_data.bms_num = herewin_info.battery_num;

        bms_data.bms_v1 = herewin_info.battery_vol[0];
        bms_data.bms_v2 = herewin_info.battery_vol[1];
        bms_data.bms_v3 = herewin_info.battery_vol[2];
        bms_data.bms_v4 = herewin_info.battery_vol[3];
        bms_data.bms_v5 = herewin_info.battery_vol[4];
        bms_data.bms_v6 = herewin_info.battery_vol[5];
        bms_data.bms_v7 = herewin_info.battery_vol[6];
        bms_data.bms_v8 = herewin_info.battery_vol[7];
        bms_data.bms_v9 = herewin_info.battery_vol[8];
        bms_data.bms_v10 = herewin_info.battery_vol[9];
        bms_data.bms_v11 = herewin_info.battery_vol[10];
        bms_data.bms_v12 = herewin_info.battery_vol[11];
        bms_data.bms_v13 = herewin_info.battery_vol[12];
        bms_data.bms_v14 = herewin_info.battery_vol[13];
        memcpy( bms_data.bms_ids, "herewin", 7 );
        bms_data.bms_ac = herewin_info.tolal_ele;
        bms_data.serial_num = FAC_HEREWIN;

        if(dev_bms1.regist.dev == false)
            regist_dev_info(&dev_bms1,DEVICE_BMS1,false,NULL,0,NULL,0,NULL,0,"herewin",8);
    }
    else if(Device1.Vkbms_Link.connect_status == COMP_NORMAL || Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    {
        Dev.Bms_Link.connect_status = COMP_NORMAL;

        //第一组电池
        if ( Device1.Vkbms_Link.connect_status == COMP_NORMAL )
        {
            group_num = 1;
            bms_data.bms_cycle = Device1.vk_bms5.circulation_time;
            bms_data.bms_volt = Device1.vk_bms2.voltage;
            bms_data.bms_temp = Device1.vk_bms2.tempture;
            bms_data.bms_vs = Device1.vk_bms2.persent / 10;
            bms_data.bms_ss = Device1.vk_bms2.warn_flag;
            bms_data.bms_num = Device1.vk_bms2.bms_num;
            memcpy(&bms_data.bms_v1,&Device1.vk_bms2.bms_v1,26 * 2);
            
            memcpy(&bms_data.bms_ids[0],&Device1.vk_bms3.bms_id[0],20);
            // bms_data.bms_ids[0] = Device1.vk_bms3.bms_id[0];
            // bms_data.bms_ids[1] = Device1.vk_bms3.bms_id[1];
            // for(int i = 1; i< 11; i++){
            //     bms_data.bms_ids[2*i] = ((Device1.vk_bms3.bms_id[1 + i] >> 4) & 0xf) + '0';
            //     bms_data.bms_ids[2*i+1] = (Device1.vk_bms3.bms_id[1 + i] & 0xf )+ '0';
            // }
            bms_data.bms_ac = (short)(Device1.vk_bms2.current / 10);
            bms_data.serial_num = FAC_VK_BMS;
            // if( Device1.vk_bms3.bms_type_info[0] == 'S' && Device1.vk_bms3.bms_type_info[0] == 'H'
            // && Device1.vk_bms3.bms_type_info[0] == 'A' && Device1.vk_bms3.bms_type_info[0] == '1')
            // {
            //     bms_data.serial_num = 11; //???
            // }

            //厂家还没加
            if(StrArrayNull( &bms_data.bms_ids[0], sizeof( bms_data.bms_ids ) ) == false && dev_bms1.regist.sn == false)
                regist_dev_info(&dev_bms1,DEVICE_BMS1,false,(char *)bms_data.bms_ids,32,NULL,0,NULL,0,NULL,0);
            if(StrArrayNull( (uint8_t *)&Device1.vk_bms3.bms_company_info[0], sizeof( Device1.vk_bms3.bms_company_info ) ) == false && dev_bms1.regist.manu == false)
                regist_dev_info(&dev_bms1,DEVICE_BMS1,false,NULL,0,NULL,0,NULL,0,&Device1.vk_bms3.bms_company_info[0],16);
            
        }
        else
        {
            memset( &Device1, 0, sizeof( Device1 ) );
        }

        //第二组电池
        if ( Device2.Vkbms_Link.connect_status == COMP_NORMAL )
        {
            group_num = 2;
            bms_data2.bms_cycle = Device2.vk_bms5.circulation_time;
            bms_data2.bms_volt = Device2.vk_bms2.voltage;
            bms_data2.bms_temp = Device2.vk_bms2.tempture;
            bms_data2.bms_vs = Device2.vk_bms2.persent / 10;
            bms_data2.bms_ss = Device2.vk_bms2.warn_flag;
            bms_data2.bms_num = Device2.vk_bms2.bms_num;

            memcpy(&bms_data2.bms_v1,&Device2.vk_bms2.bms_v1,26 * 2);
            
            memcpy(&bms_data2.bms_ids[0],&Device2.vk_bms3.bms_id[0],20);

            bms_data2.bms_ac = (short)(Device2.vk_bms2.current / 10);
            bms_data2.serial_num = FAC_VK_BMS;
            // if( Device2.vk_bms3.bms_type_info[0] == 'S' && Device2.vk_bms3.bms_type_info[0] == 'H'
            // && Device2.vk_bms3.bms_type_info[0] == 'A' && Device2.vk_bms3.bms_type_info[0] == '1')
            // {
            //     bms_data2.serial_num = 11; //???
            // }
            if(StrArrayNull( &bms_data2.bms_ids[0], sizeof( bms_data2.bms_ids ) ) == false && dev_bms2.regist.sn == false)
                regist_dev_info(&dev_bms2,DEVICE_BMS2,false,(char *)bms_data2.bms_ids,32,NULL,0,NULL,0,NULL,0);
            if(StrArrayNull( (uint8_t *)&Device2.vk_bms3.bms_company_info[0], sizeof( Device2.vk_bms3.bms_company_info ) ) == false && dev_bms2.regist.manu == false)
                regist_dev_info(&dev_bms2,DEVICE_BMS2,false,NULL,0,NULL,0,NULL,0,&Device2.vk_bms3.bms_company_info[0],16);
            
        }
        else
        {
            memset( &bms_data2, 0, sizeof( bms_data2 ) );
        }   
    }
    else
    {
        if ( Dev.Bms_Link.connect_status == COMP_NORMAL )
        {
            Dev.Bms_Link.connect_status = COMP_LOST;
            //memset(&bms_data, 0, sizeof(bms_data));
            //状态未置异常
            bms_data.bms_ss = 0;
        }
    }
}


/**
  * @file    Send_VKbms_info
  * @brief   VK智能电池协议飞控端发送
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
uint8_t utc_send_time = 10;
void send_msg_to_VKbms(void)
{   
    static uint8_t change_dev = 1;
    Vkbms_Device *ptr = NULL;

    static uint32_t bms_heart_time = 0;;
    unsigned int can_id = 0;
    uint8_t len = 0, msg_id = 0,i = 0;
    uint8_t bms_can_buf[8] = {0};
    uint16_t crc = 0;

    if(change_dev == 1)
    {
        change_dev = 2;
        ptr = &Device1;
    }
    else if(change_dev == 2)
    {
        change_dev = 1;
        ptr = &Device2;
    }

    if(ptr->Vkbms_Link.connect_status != COMP_NORMAL)
        return;

    //握手应答
    if(ptr->ack_to_bms == true)
    {
        msg_id = 0x01;
        can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
        len = 2;
        bms_can_buf[0] = 0x56;
        bms_can_buf[1] = 0x31;
        can_send_msg_normal(&bms_can_buf[0], len, can_id);
        
        ptr->ack_to_bms = false;
    }

    if(ptr->get_bms3_ack == false)
    {
        len = 0;
        msg_id = 0x03;//canid 4083768 3E5039
        can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
        can_send_msg_normal(&bms_can_buf[0], len, can_id);
    }
    else if(ptr->get_bms4_ack == false)
    {
        len = 0;
        msg_id = 0x04; //canid 5132344 4E5039
        can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
        can_send_msg_normal(&bms_can_buf[0], len, can_id);
    }
    else if(ptr->get_bms5_ack == false)
    {
        len = 0;
        msg_id = 0x05;//canid 6180920 5E5039
        can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
        can_send_msg_normal(&bms_can_buf[0], len, can_id);
    }


    if(start_msg.key_to_bms == true)
    {
        msg_id = 0x07;

        crc = Get_Crc16(&start_msg.key_data[0],20);

        memcpy(&bms_can_buf[0],&crc,2);
        memcpy(&bms_can_buf[2],&start_msg.key_data[i],6);

        can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x0 << 3 | 1;
        can_send_msg_normal(&bms_can_buf[0], 8, can_id);
        i += 6;
        
        can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x0 << 4 | 0x0 << 3 | 2;
        can_send_msg_normal(&start_msg.key_data[i], 8, can_id);
        i += 8;

        can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x0 << 4 | 0x1 << 3 | 3;
        can_send_msg_normal(&start_msg.key_data[i], 6, can_id);
    }
    //开启mos
    else if(start_msg.mos_status == true)
    {
        msg_id = 0x08;

        can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
        bms_can_buf[0] = 0x01;
        can_send_msg_normal(&bms_can_buf[0], 1, can_id);
    }
    else if(utc_time != 0 && utc_send_time > 0 )
    {
        msg_id = 0x09;
        can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
        memcpy(&bms_can_buf[0],&utc_time,4);
        can_send_msg_normal(&bms_can_buf[0], 4, can_id);

        utc_send_time--;
    }
    else if(HAL_GetTick() - bms_heart_time > 500)//实时信息应答包
    {
        msg_id = 0x02;
        can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
        can_send_msg_normal(&bms_can_buf[0], 0, can_id);
        bms_heart_time = HAL_GetTick();
    }


    // if(Device1.Vkbms_Link.connect_status == COMP_NORMAL || Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    // {
    //     static uint8_t bsm_step_num = 6;
    //     static uint32_t bms_heart_time = 0;;
    //     unsigned int can_id = 0;
    //     uint8_t len = 0, msg_id = 0,i = 0;
    //     uint8_t bms_can_buf[8] = {0};
    //     uint16_t crc = 0;

    //     //上电握手
    //     if(Device1.ack_to_bms == true || Device2.ack_to_bms == true)
    //     {
    //         msg_id = 0x01;

    //         if(Device1.ack_to_bms == true)
    //         {
    //             can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //             Device1.ack_to_bms = false;
    //         }
    //         else if (Device2.ack_to_bms == true)
    //         {
    //             can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //             Device2.ack_to_bms = false;
    //         }
    //         len = 2;
    //         bms_can_buf[0] = 0x56;
    //         bms_can_buf[1] = 0x31;
    //         can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //     }

    //     //读取电池信息
    //     if(bms_step <= 2)
    //     {
    //         //上电发送的信息
    //         switch (bms_step)
    //         {
    //         case 0:
    //             if(bsm_step_num > 0)
    //             {
    //                 len = 0;
    //                 msg_id = 0x03;//canid 4083768 3E5039
    //                 can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                 can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 if(Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    //                 {
    //                     can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                     can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 }
    //                 bsm_step_num--;
    //             }
    //             else
    //             {
    //                 bsm_step_num = 5;
    //                 bms_step = 1; 
    //             }
    //             break;
    //         case 1:
    //             if(bsm_step_num > 0)
    //             {
    //                 len = 0;
    //                 msg_id = 0x04; //canid 5132344 4E5039
    //                 can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                 can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 if(Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    //                 {
    //                     can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                     can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 }
    //                 bsm_step_num--;
    //             }
    //             else
    //             {
    //                 bsm_step_num = 5;
    //                 bms_step = 2; 
    //             }
    //             break;
    //         case 2:
    //             if(bsm_step_num > 0)
    //             {
    //                 len = 0;
    //                 msg_id = 0x05;//canid 6180920 5E5039
    //                 can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                 can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 if(Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    //                 {
    //                     can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                     can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 }
    //                 bsm_step_num--;
    //             }
    //             else
    //             {
    //                 bsm_step_num = 5;
    //                 bms_step = 3; 
    //             }
    //             break;
        
    //         default:
    //             break;
    //         }
    //     }
    //     //秘钥匹配
    //     else if(start_msg.key_to_bms == true && bsm_step_num > 0 && bms_step == 3 ) //上个if语句发完再发
    //     {
    //         msg_id = 0x07;

    //         crc = Get_Crc16(&start_msg.key_data[0],20);

    //         memcpy(&bms_can_buf[0],&crc,2);
    //         memcpy(&bms_can_buf[2],&start_msg.key_data[i],6);

    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x0 << 3 | 1;
    //         can_send_msg_normal(&bms_can_buf[0], 8, can_id);
    //         i += 6;
            
    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x0 << 4 | 0x0 << 3 | 2;
    //         can_send_msg_normal(&start_msg.key_data[i], 8, can_id);
    //         i += 8;

    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x0 << 4 | 0x1 << 3 | 3;
    //         can_send_msg_normal(&start_msg.key_data[i], 6, can_id);

    //         bsm_step_num--;
    //         if(bsm_step_num == 0)
    //         {
    //             bsm_step_num = 5;
    //             bms_step = 4; 
    //         }
    //     }
    //     //开启mos
    //     else if(start_msg.mos_status == true && bsm_step_num > 0 && (bms_step == 4 || start_msg.mos_status == true))
    //     {
    //         msg_id = 0x08;

    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //         bms_can_buf[0] = 0x01;
    //         can_send_msg_normal(&bms_can_buf[0], 1, can_id);
    //         bsm_step_num--;
    //     }
    //     else if(utc_time != 0 && utc_send_time > 0 )
    //     {
    //         msg_id = 0x09;
    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //         memcpy(&bms_can_buf[0],&utc_time,4);
    //         can_send_msg_normal(&bms_can_buf[0], 4, can_id);
    //         if(Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    //         {
    //             can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //             can_send_msg_normal(&bms_can_buf[0], 4, can_id);
    //         }
    //         utc_send_time--;
    //     }
    //     else if(HAL_GetTick() - bms_heart_time > 1000)//实时信息应答包
    //     {
    //         msg_id = 0x02;
    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //         can_send_msg_normal(&bms_can_buf[0], 0, can_id);
    //         if(Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    //         {
    //             can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //             can_send_msg_normal(&bms_can_buf[0], 0, can_id);
    //         }
    //         bms_heart_time = HAL_GetTick();
    //     }
    // }
    //开启mos
    // else if(start_msg.mos_status == true)
    // {
    //     msg_id = 0x08;

    //     can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //     bms_can_buf[0] = 0x01;
    //     can_send_msg_normal(&bms_can_buf[0], 1, can_id);
    // }
    // else if(utc_time != 0 && utc_send_time > 0 )
    // {
    //     msg_id = 0x09;
    //     can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //     memcpy(&bms_can_buf[0],&utc_time,4);
    //     can_send_msg_normal(&bms_can_buf[0], 4, can_id);

    //     utc_send_time--;
    // }
    // else if(HAL_GetTick() - bms_heart_time > 500)//实时信息应答包
    // {
    //     msg_id = 0x02;
    //     can_id = msg_id << 20 | 0x7 << 17 | ptr->source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //     can_send_msg_normal(&bms_can_buf[0], 0, can_id);
    //     bms_heart_time = HAL_GetTick();
    // }


    // if(Device1.Vkbms_Link.connect_status == COMP_NORMAL || Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    // {
    //     static uint8_t bsm_step_num = 6;
    //     static uint32_t bms_heart_time = 0;;
    //     unsigned int can_id = 0;
    //     uint8_t len = 0, msg_id = 0,i = 0;
    //     uint8_t bms_can_buf[8] = {0};
    //     uint16_t crc = 0;

    //     //上电握手
    //     if(Device1.ack_to_bms == true || Device2.ack_to_bms == true)
    //     {
    //         msg_id = 0x01;

    //         if(Device1.ack_to_bms == true)
    //         {
    //             can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //             Device1.ack_to_bms = false;
    //         }
    //         else if (Device2.ack_to_bms == true)
    //         {
    //             can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //             Device2.ack_to_bms = false;
    //         }
    //         len = 2;
    //         bms_can_buf[0] = 0x56;
    //         bms_can_buf[1] = 0x31;
    //         can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //     }

    //     //读取电池信息
    //     if(bms_step <= 2)
    //     {
    //         //上电发送的信息
    //         switch (bms_step)
    //         {
    //         case 0:
    //             if(bsm_step_num > 0)
    //             {
    //                 len = 0;
    //                 msg_id = 0x03;//canid 4083768 3E5039
    //                 can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                 can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 if(Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    //                 {
    //                     can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                     can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 }
    //                 bsm_step_num--;
    //             }
    //             else
    //             {
    //                 bsm_step_num = 5;
    //                 bms_step = 1; 
    //             }
    //             break;
    //         case 1:
    //             if(bsm_step_num > 0)
    //             {
    //                 len = 0;
    //                 msg_id = 0x04; //canid 5132344 4E5039
    //                 can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                 can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 if(Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    //                 {
    //                     can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                     can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 }
    //                 bsm_step_num--;
    //             }
    //             else
    //             {
    //                 bsm_step_num = 5;
    //                 bms_step = 2; 
    //             }
    //             break;
    //         case 2:
    //             if(bsm_step_num > 0)
    //             {
    //                 len = 0;
    //                 msg_id = 0x05;//canid 6180920 5E5039
    //                 can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                 can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 if(Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    //                 {
    //                     can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //                     can_send_msg_normal(&bms_can_buf[0], len, can_id);
    //                 }
    //                 bsm_step_num--;
    //             }
    //             else
    //             {
    //                 bsm_step_num = 5;
    //                 bms_step = 3; 
    //             }
    //             break;
        
    //         default:
    //             break;
    //         }
    //     }
    //     //秘钥匹配
    //     else if(start_msg.key_to_bms == true && bsm_step_num > 0 && bms_step == 3 ) //上个if语句发完再发
    //     {
    //         msg_id = 0x07;

    //         crc = Get_Crc16(&start_msg.key_data[0],20);

    //         memcpy(&bms_can_buf[0],&crc,2);
    //         memcpy(&bms_can_buf[2],&start_msg.key_data[i],6);

    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x0 << 3 | 1;
    //         can_send_msg_normal(&bms_can_buf[0], 8, can_id);
    //         i += 6;
            
    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x0 << 4 | 0x0 << 3 | 2;
    //         can_send_msg_normal(&start_msg.key_data[i], 8, can_id);
    //         i += 8;

    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x0 << 4 | 0x1 << 3 | 3;
    //         can_send_msg_normal(&start_msg.key_data[i], 6, can_id);

    //         bsm_step_num--;
    //         if(bsm_step_num == 0)
    //         {
    //             bsm_step_num = 5;
    //             bms_step = 4; 
    //         }
    //     }
    //     //开启mos
    //     else if(start_msg.mos_status == true && bsm_step_num > 0 && (bms_step == 4 || start_msg.mos_status == true))
    //     {
    //         msg_id = 0x08;

    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //         bms_can_buf[0] = 0x01;
    //         can_send_msg_normal(&bms_can_buf[0], 1, can_id);
    //         bsm_step_num--;
    //     }
    //     else if(utc_time != 0 && utc_send_time > 0 )
    //     {
    //         msg_id = 0x09;
    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //         memcpy(&bms_can_buf[0],&utc_time,4);
    //         can_send_msg_normal(&bms_can_buf[0], 4, can_id);
    //         if(Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    //         {
    //             can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //             can_send_msg_normal(&bms_can_buf[0], 4, can_id);
    //         }
    //         utc_send_time--;
    //     }
    //     else if(HAL_GetTick() - bms_heart_time > 1000)//实时信息应答包
    //     {
    //         msg_id = 0x02;
    //         can_id = msg_id << 20 | 0x7 << 17 | Device1.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //         can_send_msg_normal(&bms_can_buf[0], 0, can_id);
    //         if(Device2.Vkbms_Link.connect_status == COMP_NORMAL)
    //         {
    //             can_id = msg_id << 20 | 0x7 << 17 | Device2.source_id << 11 | 0x1 << 5 | 0x1 << 4 | 0x1 << 3 | 1;
    //             can_send_msg_normal(&bms_can_buf[0], 0, can_id);
    //         }
    //         bms_heart_time = HAL_GetTick();
    //     }
    // }
        
    
}

/**
  * @file    bms_function
  * @brief   智能电池检测函数
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void bms_function()
{
    static uint32_t bms_time = 0;
    static uint32_t bms_5hz_time = 0;

    if(Check_Timer_Ready(&bms_time,_10_HZ_))
    {
        update_bms_data();

        send_msg_to_VKbms();//存在双电池 不同时发送 发送频率加快
    }

    if(Check_Timer_Ready(&bms_5hz_time,_5_HZ_))
    {
        //给智能电池发送上电信息
        send_msg_to_okcell(); 
        send_msg_to_herewin();
        //send_msg_to_poweramp(); 
    }
}


/**
  * @file    BMSCanRecvHookFunction
  * @brief   VK智能电池协议
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
uint16_t bms_test = 0;
uint16_t bms_re = 0;
uint8_t fmu_send_vkbms_msgid = 0;
void BMSCanRecvHookFunction(uint32_t CanID, uint8_t data[], uint8_t len)
{
    uint32_t source_id = 0, msg_id = 0;
    uint8_t bms_SOF = 0, bms_EOF = 0, frame_num = 0;
    static uint32_t bms1_source_id = 0;
    static uint32_t pack_msg_id = 0;

    Vkbms_Device *Bmspointer = NULL;

    msg_id = (CanID & VK_MSG_MASK_ID) >> 20;
    source_id = (CanID & VK_SRC_MASK_ID) >> 5;
    bms_SOF = (CanID >> 4) & 0x1;
    bms_EOF = (CanID >> 3) & 0x1;
    frame_num = CanID & 0x7;

    if(source_id == bms1_source_id || bms1_source_id == 0)
    {
        bms1_source_id = source_id;
        Bmspointer = &Device1;
        Bmspointer->source_id = source_id;
    }
    else
    {
        Bmspointer = &Device2;
        Bmspointer->source_id = source_id;
    }

    Bmspointer->Vkbms_Link.connect_status = COMP_NORMAL;
    Bmspointer->Vkbms_Link.recv_time = HAL_GetTick();

    //单帧
    if(bms_SOF == 1 && bms_EOF == 1)
    {
        memcpy(&Bmspointer->bms_can_buf[0],&data[0],len);
    }
    //多帧 帧头
    else if(bms_SOF == 1 && bms_EOF == 0)
    {   
        Bmspointer->mul_frame_i = 0;
        Bmspointer->mul_frame_num = 2;
        Bmspointer->if_mul_frame = true;
        Bmspointer->recv_mul_freme_complete = false;
        memcpy(&Bmspointer->bms_can_buf[Bmspointer->mul_frame_i],&data[0],len);
        Bmspointer->mul_frame_i += len;

        pack_msg_id = msg_id;
    }
    // 中间帧
    else if(Bmspointer->if_mul_frame == true && bms_SOF == 0 && bms_EOF == 0 && pack_msg_id == msg_id)
    {
        if(Bmspointer->mul_frame_num == frame_num /*&& ((Bmspointer->mul_frame_i + len) <= sizeof(vkbms2))*/ )
        {
            memcpy(&Bmspointer->bms_can_buf[Bmspointer->mul_frame_i],&data[0],len);

            Bmspointer->mul_frame_i += len;
            Bmspointer->mul_frame_num++;
            if(Bmspointer->mul_frame_num == 8)
            {
                Bmspointer->mul_frame_num = 1;
            }

            if(Bmspointer->mul_frame_num >= 8)
            {
                Bmspointer->mul_frame_num = 0;
            }
        }
        else
        {
            Bmspointer->if_mul_frame = false;
        }
    }
    //帧尾
    else if(bms_SOF == 0 && bms_EOF == 1 && Bmspointer->if_mul_frame == true && pack_msg_id == msg_id)
    {
        memcpy(&Bmspointer->bms_can_buf[Bmspointer->mul_frame_i],&data[0],len);
        Bmspointer->mul_frame_i += len;
        Bmspointer->if_mul_frame = false;

        //bms_test = Get_Crc16(&Bmspointer->bms_can_buf[2], Bmspointer->mul_frame_i - 2); 
        //bms_re =  Bmspointer->bms_can_buf[0] + (Bmspointer->bms_can_buf[1] << 8);
        if(Get_Crc16(&Bmspointer->bms_can_buf[2], Bmspointer->mul_frame_i - 2) == (Bmspointer->bms_can_buf[0] + (Bmspointer->bms_can_buf[1] << 8)))
        {
            Bmspointer->recv_mul_freme_complete = true;
        }
    }

    switch (msg_id)
    {
    case 0X01:
        if(Bmspointer->bms_can_buf[0] == 0x56 && Bmspointer->bms_can_buf[1] == 0x31)
        {
            Bmspointer->ack_to_bms = true;
            Bmspointer->source_id = source_id;
        }
        break;
    case 0X02:
        if(Bmspointer->recv_mul_freme_complete == true)
        {   
            memcpy(&Bmspointer->vk_bms2.voltage, &Bmspointer->bms_can_buf[2], sizeof(vkbms2));//接收一整包电池数据
            Bmspointer->recv_mul_freme_complete = false;
        }
        break; 
    case 0X03:
        if(Bmspointer->recv_mul_freme_complete == true)
        {   
            memcpy(&Bmspointer->vk_bms3.bms_company_info, &Bmspointer->bms_can_buf[2], sizeof(vkbms3));
            Bmspointer->recv_mul_freme_complete = false;

            Bmspointer->get_bms3_ack = true;
        }
        break;
    case 0X04:
        memcpy(&Bmspointer->vk_bms4.capacity, &Bmspointer->bms_can_buf[0], sizeof(vkbms4));
        Bmspointer->recv_mul_freme_complete = false;
        Bmspointer->get_bms4_ack = true;
        break;
    case 0X05:
        if(Bmspointer->recv_mul_freme_complete == true)
        {   
            memcpy(&Bmspointer->vk_bms5.health_percent, &Bmspointer->bms_can_buf[2], sizeof(vkbms5));
            Bmspointer->recv_mul_freme_complete = false;
            Bmspointer->get_bms5_ack = true;
        }
        break;
    //秘钥是否匹配
    case 0x07:
        start_msg.key_to_bms = false;
        if(Bmspointer->bms_can_buf[0] == 0)
        {
            start_msg.mos_status = true;
            start_msg.Id_content = 1;
        }
        else
        {
            start_msg.Id_content = 2;
        }
        start_msg.Id = 3;
        start_msg.key_info_checking = true;

        break;
    case 0x08:
         start_msg.Id = 4;
         start_msg.Id_content = Bmspointer->bms_can_buf[0];
         start_msg.key_info_checking = true;
         start_msg.mos_status = false;
        break;
    case 0x09:
        if(Bmspointer->bms_can_buf[0] == 0)
        {
            utc_send_time = 0;
        }
        break;
    default:
        break;
    }
}

