From b13574876322e59f75f26ac38f72c6b19c9d4aba Mon Sep 17 00:00:00 2001 From: chen <15335560115@163.com> Date: 星期五, 27 六月 2025 14:02:08 +0800 Subject: [PATCH] 刚解决完网关下发会导致异常问题,但蓝牙那边mk给蓝牙发未配置成功未解决,开始调lora功耗 --- keil/include/drivers/serial_at_cmd_app.c | 660 ++++++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 449 insertions(+), 211 deletions(-) diff --git a/keil/include/drivers/serial_at_cmd_app.c b/keil/include/drivers/serial_at_cmd_app.c index 46b369f..4813082 100644 --- a/keil/include/drivers/serial_at_cmd_app.c +++ b/keil/include/drivers/serial_at_cmd_app.c @@ -72,7 +72,7 @@ send_frame[1] = 0xAA; send_frame[2] = 0x03; send_frame[3] = data_length+5; - send_frame[4] = CMD_REPLY; +// send_frame[4] = CMD_REPLY; send_frame[5] = index; send_frame[6] = data_length; memcpy(&send_frame[7], &g_com_map[index], data_length); @@ -93,7 +93,7 @@ send_frame[1] = 0xAA; send_frame[2] = 0x03; send_frame[3] = data_length+5; - send_frame[4] = CMD_REPLY; +// send_frame[4] = CMD_REPLY; send_frame[5] = index; send_frame[6] = data_length; memcpy(&send_frame[7], &g_com_map[index], data_length); @@ -143,7 +143,7 @@ extern uint8_t enable_sleep_count,sleep_flag; void UsartParseDataHandler(uint8_t data) { - static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0; +static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0; uint16_t checksum = 0; static uint8_t pack_datalen = 0,pack_length = 0,pack_index = 0,pack_msgtype = 0,pack_cmd = CMD_READ; static uint8_t index = 0; @@ -182,7 +182,7 @@ enable_sleep_count=0; //read包中data字节,即mUsartReceivePack[0]表示数据长度; //从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来 - SendComMap0(pack_datalen,pack_index>>1); + SendComMap(pack_datalen,pack_index>>1); break; default: break; @@ -225,219 +225,457 @@ usart_receive_state = UsartReceiveWaitHead0; pack_index = 0; pack_length = 0; - } + } } - -void Usart2ParseDataHandler(uint8_t data)//UART蓝牙数据分析处理 +void uart0_send_ComMap_to_BLE(uint8_t data_length, uint8_t index) { - //打印接收到的蓝牙模块信息 - //printf("这个是传入的数据%c",data); -// static UsartRecvPackState usart2_receive_state = UsartReceiveWaitHead0; - //static UsartRecvPackState usart2_receive_state = UsartReceiveWaitData; - - static ST_BLERecv BLE_recvive;//创建蓝牙状态结构体 - - + static uint8_t send_frame[100]; uint16_t checksum = 0; - - static uint8_t pack_datalen = 0,pack_length = 0,pack_index = 0,pack_msgtype = 0,pack_cmd = CMD_READ; - static uint8_t index = 0; + send_frame[0] = 0x55; + send_frame[1] = 0xAA; + send_frame[2] = 0x40; + //send_frame[3] = CMD_REPLY; + send_frame[4] = data_length+6; + send_frame[5] = index; + memcpy(&send_frame[6], &g_com_map[index], data_length); + checksum = Checksum_u16(&send_frame[2],4+data_length); + memcpy(&send_frame[6+data_length],&checksum,2); -// uint8_t dev_id[14],dev_ssi[5],i,k=0; -// //char char_mac[14],char_ssi[6]; -// int j=0; -// //uint16_t temp_16,temp_16_id,temp_16_distance; -// uint8_t temp8_H,temp8_L; -// //float temp_float; -// //double ssi_double;//,distance_double; -// //int32_t distance_int; -// uint16_t Label_id=0; //标签id + uart_send(UART_ID0, send_frame,data_length+8, NULL); +} +void mk_write_to_ble(uint8_t data_length, uint8_t index) +{ +static uint8_t send_frame[100]; + uint16_t checksum = 0; + send_frame[0] = 0x55; + send_frame[1] = 0xAA; + send_frame[2] = 0x40; + send_frame[3] = CMD_WRITE; + send_frame[4] = data_length+6; + send_frame[5] = index; + memcpy(&send_frame[6], &g_com_map[index], data_length); + checksum = Checksum_u16(&send_frame[2],4+data_length); + memcpy(&send_frame[6+data_length],&checksum,2); - - //接收数据开始分析 - mUsart2ReceivePack[index] = data; //char数组传进来参数data - mUsart2ReceivePack_before = mUsart2ReceivePack_now; - mUsart2ReceivePack_now=data; - index++; - if( mUsart2ReceivePack_before == 0x0D && mUsart2ReceivePack_now==0x0A )//接收数据到“0x0D 0x0A”结束 - { - mUsart2ReceivePack[index]=0; - //printf("0D0A进入后的数据为%s到此结束",mUsart2ReceivePack); -// if(strncmp("RF-CRAZY",(char*)mUsart2ReceivePack,8)==0) -// { -// BLE_recvive.m_eState=BLE_RECV_STATE_MAC;//如果为名字后判断并改为MAC接收状态 -// } -// - switch(BLE_recvive.m_eState) - { case BLE_RECV_STATE_IDLE: - if(strncmp("RF-CRAZY",(char*)mUsart2ReceivePack,8)==0) - BLE_recvive.m_eState=BLE_RECV_STATE_MAC;//如果为名字后判断并改为MAC接收状态 - break; - case BLE_RECV_STATE_MAC://MAC接受处理状态 - strcpy(BLE_recvive.m_macHeader,(char*)mUsart2ReceivePack+5);//复制到蓝牙结构体中,+5是为了跳过MAC字符和冒号未处理0D0A - //处理MAC字符不让换行 - char*p=BLE_recvive.m_macHeader+strlen(BLE_recvive.m_macHeader)-2; - *p='\0'; - //printf("处理过后的MAC为%s",BLE_recvive.m_macHeader); - BLE_recvive.m_eState=BLE_RECV_STATE_RSSI; - break; - case BLE_RECV_STATE_RSSI://RSSI接收处理状态 - strcpy(BLE_recvive.m_rssiHeader2,(char*)mUsart2ReceivePack+6);//复制到蓝牙结构体中,+5是为了跳过MAC字符和冒号未处理0D0A - //printf(" 处理过后的RSSI为%s",BLE_recvive.m_rssiHeader2); - BLE_recvive.m_eState=BLE_RECV_STATE_IDLE; - break; - } - -// if(mUsart2ReceivePack[0] ==0x2B ) //“+”扫描到开始或结束字符串的首个字符 -// { -// if (mUsart2ReceivePack[5] ==0x20) -// { -// printf("UART扫描结束 :+SCAN END\r\n"); //本次扫描结束 -// CT_sum++; //包序值 -// if(CT_sum>255) CT_sum=0; -// -// //发送数据缓存 -// Lora_TXD_bff[0] = 0x55; -// Lora_TXD_bff[1] = 0xAA; -// Lora_TXD_bff[2] = 0x12; -// Lora_TXD_bff[3] = 8+4*numb_base; //数据长度 8+4*基站数量 -// -// Label_id=Label_id_local; //标签id -// memcpy(&Lora_TXD_bff[4],&Label_id ,2); -// -// Lora_TXD_bff[6] = CT_sum; //标签包序 -// Lora_TXD_bff[7] = 0x32; //按键/电量,测试数据50 -// Lora_TXD_bff[8] = 0; //LORA等待应答 - -// Lora_TXD_bff[9]= numb_base; //基站数量 - -// for(i=10;i<(10+numb_base*2);i=i+2) -// { -// memcpy(&Lora_TXD_bff[i+0] ,&data_buff[(i-10)/2][0] ,2); //基站ID -// memcpy(&Lora_TXD_bff[i+numb_base*2],&data_buff[(i-10)/2][1] ,2); //基站距离 -// } -// for( i = 2;i<(4+8+4*numb_base); i++) //去包头后 校验 累加取反 -// { -// checksum += Lora_TXD_bff[i]; -// } -// checksum = ~checksum; -// memcpy(&Lora_TXD_bff[2+8+4*numb_base],&checksum ,2); //和取反 校验 -// -// Radio_init(); //唤醒 为发送状态 -// Radio.Send( Lora_TXD_bff, (4+8+4*numb_base)); //LORA模块上传数据 -// - -// //基站距离 清零 -// for(i=0;i<numb_base;i++) -// { -// //for(k=1;k<2;k++) //清距离数据 -// for(k=0;k<2;k++) //清ID号和距离数据 -// { -// data_buff[i][k]=0; -// } -// } -// numb_base =0; -// data_buff_start =0; -// -// printf("\r\n"); -// //delay_ms(5); -// Uart2_SendString( Uart2_str); //发下一次扫描指令 -// } -// -// else if(mUsart2ReceivePack[5] ==0x5F) //“_"下划线 -// { -// printf("UART扫描开始 :+SCAN_MANU:\r\n"); -// } -// } -// else -// { -// j=memcmp(id_cmpare,mUsart2ReceivePack,8); //比较目标蓝牙地址 前4个字节 -// if(j==0)//跟目标蓝牙mac地址相同 -// { -// -// //拷贝mac地址 -// for(i=0;i<12;i++) -// { -// char_mac[i] = mUsart2ReceivePack[i]; -// } -// char_mac[12]='\0'; -// -// -// //拷贝信号 -// for(i=15;i<18;i++) -// { -// char_ssi[i-15] = mUsart2ReceivePack[i]; -// } -// if(mUsart2ReceivePack[18]==0x0D || mUsart2ReceivePack[18]==0x20) //信号是-两位数 -// { -// char_ssi[3]= '\0' ; -// char_broadcast_data[0]= '\0'; -// if (mUsart2ReceivePack[18]==0x0D) //信号是-两位数,无广播内容 -// { -// CT_satrt=0; -// } -// else if(mUsart2ReceivePack[18]==0x20) //信号是-两位数,有广播内容,空格后一位开始,到0x0D 结束。 -// { -// CT_satrt=19; -// } -// ssi_double = -((char_ssi[1]-0x30)*10 + (char_ssi[2]-0x30)); //2位信号值int -// } -// else if(mUsart2ReceivePack[18]>=0x30 && mUsart2ReceivePack[18]<=0x39 ) //信号是-三位数 -// { -// char_ssi[3]=mUsart2ReceivePack[18]; -// char_ssi[4]= '\0' ; -// char_broadcast_data[0]= '\0'; -// if (mUsart2ReceivePack[19]==0x0D) //信号是-三位数,无广播内容 -// { -// CT_satrt=0; -// } -// else if(mUsart2ReceivePack[19]==0x20) //信号是-三位数,有广播内容,空格后一位开始,到0x0D 结束。 -// { -// CT_satrt=20; -// } -// ssi_double = -((char_ssi[1]-0x30)*100 + (char_ssi[2]-0x30)*10 + (char_ssi[3]-0x30));//3位信号值int -// } -// -// printf("mac= %s ",char_mac); -// printf("ssi= %s ",char_ssi); - -// //基站ID处理 -// temp_16_id = (char_mac[8]-0x30)*1000 + (char_mac[9]-0x30)*100 + (char_mac[10]-0x30)*10 + (char_mac[11]-0x30); -// printf("ID = %d ",temp_16_id ); -// //基站距离处理 -// temp_16_distance = 0.003083 * exp(-0.09909*ssi_double)*100;//单位cm -// printf("距离 = %d cm \r\n",temp_16_distance ); - -// //比较ID号并拷贝数据到缓存区 -// for(i=0;i<data_buff_MAX;i++) -// { -// if(data_buff[i][0] == temp_16_id) //检测到已存ID -// { -// data_buff[i][1] = temp_16_distance; -// data_buff_start=0; -// break; -// } -// else if (data_buff[i][0]==0) -// { -// data_buff_start=i+1; -// numb_base=data_buff_start; //拷贝扫描到基站的数量 -// break; -// } -// } -// if(data_buff_start!=0) -// { -// data_buff[data_buff_start-1][0] = temp_16_id; -// data_buff[data_buff_start-1][1] = temp_16_distance; -// -// } -// } -// } - index=0; - mUsart2ReceivePack_before=0; - mUsart2ReceivePack_now=0; - } -//printf("处理过后的数组为%s\n",mUsart2ReceivePack); + uart_send(UART_ID0, send_frame,data_length+8, NULL); } +void read_com_map(uint8_t data_length, uint8_t index) +{ +static uint8_t send_frame[100]; + uint16_t checksum = 0; + send_frame[0] = 0x55; + send_frame[1] = 0xAA; + send_frame[2] = 0x40; + send_frame[3] = CMD_READ; + send_frame[4] = data_length+6; + send_frame[5] = index; + checksum = Checksum_u16(&send_frame[2],4); + uart_send(UART_ID0, send_frame,8, NULL); +} +uint16_t pack_checksum_test=0,test_calculate = 0; +void Usart3ParseDataHandler(uint8_t data)//UART蓝牙数据分析处理 +{ + // 全局变量 +static Usart0ReceiveState usart0_receive_state = Usart0ReceiveWaitHead0; +static uint8_t index = 0; // 缓冲区索引 +static uint8_t pack_cmd_type = 0; // 指令类型 +static uint8_t pack_msg_type = 0; // 消息类型 +static uint8_t pack_data_len = 0; // 数据长度 +static uint8_t pack_param_addr = 0; // 参数地址 +static uint16_t pack_checksum = 0; // 接收到的校验和 +static uint16_t calculated_checksum = 0; // 计算得到的校验和 +static uint8_t send_frame[256]; // 用于计算校验和的帧 +static uint8_t frame_index = 0; // 帧缓冲区索引 +static uint8_t pack_datalen = 0; + + switch(usart0_receive_state) { + case Usart0ReceiveWaitHead0: + if(data == 0x55) { + usart0_receive_state = Usart0ReceiveWaitHead1; + // 初始化帧缓冲区 + frame_index = 0; + send_frame[frame_index++] = data; + } + break; + + case Usart0ReceiveWaitHead1: + if(data == 0xAA) { + usart0_receive_state = Usart0ReceiveWaitCmdType; + send_frame[frame_index++] = data; + } else { + usart0_receive_state = Usart0ReceiveWaitHead0; + } + break; + + case Usart0ReceiveWaitCmdType: + if(data == 64) { + pack_cmd_type = data; + usart0_receive_state = Usart0ReceiveWaitMsgType; + send_frame[frame_index++] = data; + } else { + usart0_receive_state = Usart0ReceiveWaitHead0; + } + break; + + case Usart0ReceiveWaitMsgType: + if(data == 0x2||data==0x1||data==0x3) { // 读写回复消息类型 + pack_msg_type = data; + usart0_receive_state = Usart0ReceiveWaitDataLen; + send_frame[frame_index++] = data; + } else { + usart0_receive_state = Usart0ReceiveWaitHead0; + } + break; + + case Usart0ReceiveWaitDataLen: + pack_data_len = data; + index = 0; + usart0_receive_state = Usart0ReceiveWaitParamAddr; + send_frame[frame_index++] = data; + break; + + case Usart0ReceiveWaitParamAddr: + pack_param_addr = data; + if(pack_msg_type==CMD_READ) + { + usart0_receive_state = Usart0ReceiveWaitChecksum1; + pack_datalen=pack_data_len-6; + }else{ + usart0_receive_state = Usart0ReceiveWaitData; + } + send_frame[frame_index++] = data; + break; + + case Usart0ReceiveWaitData: + send_frame[frame_index++] = data; + mUsart2ReceivePack[index++] = data; + + if(index == pack_data_len-6) { + usart0_receive_state = Usart0ReceiveWaitChecksum1; + pack_datalen=pack_data_len-6; + } + break; + + case Usart0ReceiveWaitChecksum1: + pack_checksum = data << 8; // 高字节 + pack_checksum_test = data << 8; // 高字节 + send_frame[frame_index++] = data; + usart0_receive_state = Usart0ReceiveWaitChecksum2; + break; + case Usart0ReceiveWaitChecksum2: + pack_checksum |= data; // 低字节 + pack_checksum_test |= data; // 低字节 + // 计算校验和 (从第3个字节开始,长度为4+数据长度) + if(pack_msg_type!=CMD_READ) + { + calculated_checksum = Checksum_u16(&send_frame[2],pack_datalen+4); + }else{ + calculated_checksum = Checksum_u16(&send_frame[2],4); + } + test_calculate=calculated_checksum; + // 验证校验和 + if(pack_checksum == calculated_checksum) { + // 校验通过,处理数据 + switch(pack_msg_type) + { + //case CMD_REPLY: + case CMD_WRITE: + //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中,赋值保存的参数并且存入flash + + memcpy((uint8_t*)&g_com_map + pack_param_addr*2, mUsart2ReceivePack, pack_datalen); + + //返回一个error状态 + //SendComMap(pack_datalen,pack_index); + save_com_map_to_flash(); + //delay_ms(100); +// NVIC_SystemReset(); + break; + case CMD_READ: + uart0_send_ComMap_to_BLE(pack_datalen,pack_param_addr); + + break; +// case CMD_REPLY: +// memcpy((uint8_t*)&g_com_map + pack_param_addr, mUsart2ReceivePack, pack_datalen); +// save_com_map_to_flash(); +// break; + default: + break; + } + } + + // 重置状态机 + usart0_receive_state = Usart0ReceiveWaitHead0; + index = 0; + frame_index = 0; + break; + + default: + usart0_receive_state = Usart0ReceiveWaitHead0; + index = 0; + frame_index = 0; + break; + } + + +// if(usart_receive_state == UsartReceiveWaitChecksum) { //若收到校验和包 +// checksum = 0; +// for(int i = 0; i<pack_length-5; i++) { +// checksum += mUsart2ReceivePack[i]; +// } +// checksum += pack_cmd; +// checksum += pack_length; +// checksum += pack_index; +// checksum += pack_datalen; +// checksum += pack_msgtype; +// if(((data + checksum)&0xff) == 0xFF) //校验通过 +// { +// switch(pack_cmd) +// { +// case CMD_WRITE: +// //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中,赋值保存的参数并且存入flash +// enable_sleep_count=0; +// memcpy((uint8_t*)&g_com_map + pack_index, mUsart2ReceivePack, pack_datalen); +// +// if(mUsart2ReceivePack[0]==1) +// UpdateProcess(pack_index); +// //返回一个error状态 +// //SendComMap(pack_datalen,pack_index); +// save_com_map_to_flash(); +// //delay_ms(100); +// //NVIC_SystemReset(); +// break; +// case CMD_READ: +// //read包中data字节,即mUsartReceivePack[0]表示数据长度; +// //从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来 +// SendComMap0(pack_datalen,pack_index>>1); +// break; +// default: +// break; +// } +// } +// usart_receive_state = UsartReceiveWaitHead0; +// pack_index = 0; +// pack_length = 0; +// index=0; +// } else if((usart_receive_state == UsartReceiveWaitData) ) { //若果收到的是正常通讯包 +// mUsart2ReceivePack[index] = data; +// index++; +// if(index == pack_length-5) { //如果收到的index与长度相等 +// usart_receive_state = UsartReceiveWaitChecksum; +// } +// } else if(usart_receive_state == UsartReceiveWaitDataLen) { //收到指令类型字节 +// pack_datalen = data; +// usart_receive_state = UsartReceiveWaitData; +// }else if(usart_receive_state == UsartReceiveWaitIndex) { //收到指令类型字节 +// pack_index = data; +// usart_receive_state = UsartReceiveWaitDataLen; +// } else if(usart_receive_state == UsartReceiveWaitCMD) { //收到指令类型字节 +// pack_cmd = data; +// usart_receive_state = UsartReceiveWaitIndex; +// } else if(usart_receive_state == UsartReceiveWaitLength) { //收到长度字节 +// +// pack_length = data; +// pack_index = 0; +// usart_receive_state = UsartReceiveWaitCMD; +// +// } else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) { //收到第一个包头 +// usart_receive_state = UsartReceiveWaitHead1; +// } else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) { //收到第二个包头 +// usart_receive_state = UsartReceiveWaitMsgType; +// }else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) { +// usart_receive_state = UsartReceiveWaitLength; +// pack_msgtype = data; +// } +// else { +// usart_receive_state = UsartReceiveWaitHead0; +// pack_index = 0; +// pack_length = 0; +// } +} +extern uint16_t set_mk_time; +void Send_Reply_To_Ble(void) +{ +static uint8_t send_frame[100]; +// uint16_t checksum = 0; + send_frame[0] = 0x55; + send_frame[1] = 0xAA; + send_frame[2] = 0x41; + send_frame[3] = CMD_MK_REPLY; + uart_send(UART_ID0, send_frame,4, NULL); +} +void mk_send_message_to_BLE(void) +{ +static uint8_t send_frame[100]; + uint16_t checksum = 0; + send_frame[0] = 0x55; + send_frame[1] = 0xAA; + send_frame[2] = 0x40; + send_frame[3] = CMD_MK_TO_BLE; + send_frame[4]=8;//数据长度 + send_frame[5]=0; + send_frame[6]=0; + send_frame[7]=0; + memcpy(&send_frame[8],&set_mk_time,2); + checksum=Checksum_u16(&send_frame[2],8); + memcpy(&send_frame[10],&checksum,2); + //LOG_INFO(TRACE_MODULE_APP,"发给ble的checksum %#x\r\n",checksum); + uart_send(UART_ID0, send_frame,12, NULL); + uart_send(UART_ID1, send_frame,12, NULL); +} +extern uint8_t bat_percent; +extern uint32_t dev_id; + +void Usart0ParseDataHandler(uint8_t data)//UART蓝牙数据分析处理 +{ + // 全局变量 +static Usart0ReceiveState usart0_receive_state = Usart0ReceiveWaitHead0; + +static uint8_t index = 0; // 缓冲区索引 +static uint8_t pack_cmd_type = 0; // 指令类型 +static uint8_t pack_msg_type = 0; // 消息类型 +static uint8_t pack_data_len = 0; // 数据长度 +static uint8_t pack_param_addr = 0; // 参数地址 +static uint16_t pack_checksum = 0; // 接收到的校验和 +static uint16_t calculated_checksum = 0; // 计算得到的校验和 +static uint8_t send_frame[256]; // 用于计算校验和的帧 +static uint8_t frame_index = 0; // 帧缓冲区索引 +static uint8_t pack_datalen = 0; + + LOG_INFO(TRACE_MODULE_APP,"收到 %#x\r\n",data); + switch(usart0_receive_state) { + case Usart0ReceiveWaitHead0: + if(data == 0x55) { + usart0_receive_state = Usart0ReceiveWaitHead1; + // 初始化帧缓冲区 + frame_index = 0; + send_frame[frame_index++] = data; + } + break; + + case Usart0ReceiveWaitHead1: + if(data == 0xAA) { + usart0_receive_state = Usart0ReceiveWaitCmdType; + send_frame[frame_index++] = data; + } else { + usart0_receive_state = Usart0ReceiveWaitHead0; + } + break; + + case Usart0ReceiveWaitCmdType: + if(data == 64) { + pack_cmd_type = data; + usart0_receive_state = Usart0ReceiveWaitMsgType; + send_frame[frame_index++] = data; + } else { + usart0_receive_state = Usart0ReceiveWaitHead0; + } + break; + + case Usart0ReceiveWaitMsgType: + if(data == 0x2||data==0x1) { // 读写回复消息类型 + pack_msg_type = data; + usart0_receive_state = Usart0ReceiveWaitDataLen; + send_frame[frame_index++] = data; + } else { + usart0_receive_state = Usart0ReceiveWaitHead0; + } + break; + + case Usart0ReceiveWaitDataLen: + pack_data_len = data; + index = 0; + usart0_receive_state = Usart0ReceiveWaitData; + send_frame[frame_index++] = data; + break; + +// case Usart0ReceiveWaitParamAddr: +// pack_param_addr = data; +// if(pack_msg_type==CMD_READ) +// { +// usart0_receive_state = Usart0ReceiveWaitChecksum1; +// pack_datalen=pack_data_len-6; +// }else{ +// usart0_receive_state = Usart0ReceiveWaitData; +// } +// send_frame[frame_index++] = data; +// break; + + case Usart0ReceiveWaitData: + send_frame[frame_index++] = data; + mUsart2ReceivePack[index++] = data; + + if(index == pack_data_len-3) { + usart0_receive_state = Usart0ReceiveWaitChecksum1; + //pack_datalen=pack_data_len; + } + break; + + case Usart0ReceiveWaitChecksum1: + pack_checksum = data; // 高字节 + pack_checksum_test = data; // 高字节 + send_frame[frame_index++] = data; + usart0_receive_state = Usart0ReceiveWaitChecksum2; + LOG_INFO(TRACE_MODULE_APP,"收到校验1 %#x\r\n",data); + break; + case Usart0ReceiveWaitChecksum2: + pack_checksum |= data<<8; // 低字节 + pack_checksum_test |= data<<8; // 低字节 + LOG_INFO(TRACE_MODULE_APP,"收到校验2 %#x\r\n",data); + // 计算校验和 (从第3个字节开始,长度为4+数据长度) +// if(pack_msg_type!=CMD_REPLY) +// { + calculated_checksum = Checksum_u16(&send_frame[2],pack_data_len); +// }else{ +// //calculated_checksum = Checksum_u16(&send_frame[2],4); +// +// } + test_calculate=calculated_checksum; + LOG_INFO(TRACE_MODULE_APP,"收到校验 %#x,%#x\r\n",calculated_checksum,pack_checksum); + // 验证校验和 + if(pack_checksum == calculated_checksum) { + // 校验通过,处理数据 + switch(pack_msg_type) + { + case CMD_BLE_TO_MK: + memcpy(&dev_id,&mUsart2ReceivePack[0],2); + bat_percent=mUsart2ReceivePack[2]; + memcpy(&set_mk_time,&mUsart2ReceivePack[3],2); + memcpy(&g_com_map[DEV_ID],&dev_id,2); + memcpy(&g_com_map[SET_START_TIME],&set_mk_time,2); + Send_Reply_To_Ble(); + LOG_INFO(TRACE_MODULE_APP,"收到蓝牙数据 set_mk_time %d,dev_id %04x,bat_percent %d\r\n",set_mk_time,dev_id,bat_percent); + save_com_map_to_flash(); + //memcpy((uint8_t*)&g_com_map + pack_param_addr*2, mUsart2ReceivePack, pack_datalen); + + + break; +// case CMD_MK_TO_BLE: +// //uart0_send_ComMap_to_BLE(pack_datalen,pack_param_addr); +// +// break; +//// case CMD_REPLY: +//// memcpy((uint8_t*)&g_com_map + pack_param_addr, mUsart2ReceivePack, pack_datalen); +//// save_com_map_to_flash(); +//// break; + default: + break; + } + } + + // 重置状态机 + usart0_receive_state = Usart0ReceiveWaitHead0; + index = 0; + frame_index = 0; + break; + + default: + usart0_receive_state = Usart0ReceiveWaitHead0; + index = 0; + frame_index = 0; + break; + } + +} -- Gitblit v1.9.3