| | |
| | | |
| | | } |
| | | double d_value; |
| | | #define buff_lenth 1024 |
| | | #define MAX_BASE_STATIONS 10 |
| | | uint8_t DMA_RXBuf_BT[200]={0}; |
| | | uint8_t Forward_BT_buff[buff_lenth]={0}; |
| | | uint8_t report_ancnum_bt=0; |
| | | |
| | | void UsartParseDataHandler(uint8_t data) |
| | | { |
| | | if(state5V_prase_flag&&!g_com_map[MODBUS_MODE]) |
| | | { //升级程序 |
| | | 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; |
| | | |
| | | |
| | | |
| | | |
| | | if(usart_receive_state == UsartReceiveWaitChecksum) |
| | | { //若收到校验和包 |
| | | checksum = 0; |
| | | for(int i = 0; i<pack_length-5; i++) { |
| | | checksum += mUsartReceivePack[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: |
| | | if(pack_index==MODBUS_MODE*2) |
| | | { |
| | | Uart1GpsRecDebugSend(); |
| | | g_com_map[MODBUS_MODE] = 1; |
| | | return; |
| | | } |
| | | //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中,赋值保存的参数并且存入flash |
| | | memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen); |
| | | if(mUsartReceivePack[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]的字节,发送出来 |
| | | SendComMap(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) ) { //若果收到的是正常通讯包 |
| | | mUsartReceivePack[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; |
| | | } |
| | | } else if(gps_prase_flag) |
| | | uint8_t buff[buff_lenth] = {0}; |
| | | uint8_t length = 0; |
| | | uint8_t base_station_count = 0; |
| | | memcpy(buff, m_EUART_DMA_RXBuf, 1024); |
| | | //memcpy(buff, Forward_BT_buff, 1024); |
| | | // 遍历 |
| | | for (int n = 0; n < 1024 - 6; n++) |
| | | { |
| | | static uint8_t index = 0; |
| | | //GPS解析数据 |
| | | // static ST_BLERecv BLE_recvive; |
| | | GGA_DataStruct jdrecv,wdrecv,Posstate; |
| | | |
| | | //接收数据开始分析 |
| | | mUsart2ReceivePack[index] = data; //char数组传进来参数data |
| | | mUsart2ReceivePack_before = mUsart2ReceivePack_now; |
| | | mUsart2ReceivePack_now=data; |
| | | index++; |
| | | if( mUsart2ReceivePack_before == 0x0D && mUsart2ReceivePack_now==0x0A )//接收数据到“0x0D 0x0A”结束 |
| | | // 包头 0x55 0xAA 0x41 |
| | | if (buff[n] == 0x55 && buff[n+1] == 0xAA && buff[n+2] == 0x41) |
| | | { |
| | | //解析该条GPS报文 |
| | | |
| | | analysis_num=sscanf((char*)mUsart2ReceivePack,"$%[^,],%*[^,],%[^,],%*[^,],%[^,],%*[^,],%[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*s",gps_header,wdrecv.m_pData,jdrecv.m_pData,Posstate.m_pData); |
| | | if(!memcmp(gps_header,"GNGGA",5)) |
| | | { |
| | | if(g_com_map[MODBUS_MODE]) |
| | | LOG_INFO(TRACE_MODULE_APP,"%s", mUsart2ReceivePack); |
| | | d_value = strtod(wdrecv.m_pData,NULL); |
| | | if(d_value>1) |
| | | { |
| | | blink_led(&gps_success_state); |
| | | wd=strtod(wdrecv.m_pData,NULL); |
| | | jd=strtod(jdrecv.m_pData,NULL); |
| | | pos_state=atoi(Posstate.m_pData); |
| | | } |
| | | } |
| | | index = 0; |
| | | if(pos_state!=0) |
| | | { |
| | | //4g.jd=jd; |
| | | //4g.wd=wd; |
| | | gps_timeout_flag=1;//不超时接收状态 |
| | | gps_need_data_flag=0;//接收数据完成 |
| | | gps_wait_count=0;//清0接收状态 |
| | | // gps_enable_flag=0;//收到有效数据关闭GPS |
| | | report_ancnum_bt=buff[n+4]; |
| | | length = buff[n+3]; |
| | | if (n + 6 + report_ancnum_bt * 6 + length > 1024) { |
| | | continue; // 如果数据越界,跳过该包 |
| | | } |
| | | memset(mUsart2ReceivePack,0,sizeof(mUsart2ReceivePack)); |
| | | memset(wdrecv.m_pData,0,sizeof(wdrecv.m_pData)); |
| | | memset(jdrecv.m_pData,0,sizeof(jdrecv.m_pData)); |
| | | memset(Posstate.m_pData,0,sizeof(Posstate.m_pData)); |
| | | index=0; |
| | | mUsart2ReceivePack_before=0; |
| | | mUsart2ReceivePack_now=0; |
| | | |
| | | // 将数据复制到 DMA 缓冲区 |
| | | memcpy(DMA_RXBuf_BT, &buff[n+4], length); |
| | | |
| | | // 为了避免多次重复解析同一个数据包,可以在这里添加跳跃 |
| | | n += 6 + report_ancnum_bt * 6 + length - 1; // 跳过当前数据包 |
| | | } |
| | | } |
| | | |
| | | // 清除临时缓冲区 |
| | | memset(buff, 0, buff_lenth); |
| | | |
| | | // if(state5V_prase_flag&&!g_com_map[MODBUS_MODE]) |
| | | // { //升级程序 |
| | | // 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; |
| | | |
| | | |
| | | |
| | | |
| | | // if(usart_receive_state == UsartReceiveWaitChecksum) |
| | | // { //若收到校验和包 |
| | | // checksum = 0; |
| | | // for(int i = 0; i<pack_length-5; i++) { |
| | | // checksum += mUsartReceivePack[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: |
| | | // if(pack_index==MODBUS_MODE*2) |
| | | // { |
| | | // Uart1GpsRecDebugSend(); |
| | | // g_com_map[MODBUS_MODE] = 1; |
| | | // return; |
| | | // } |
| | | // //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中,赋值保存的参数并且存入flash |
| | | // memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen); |
| | | // if(mUsartReceivePack[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]的字节,发送出来 |
| | | // SendComMap(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) ) { //若果收到的是正常通讯包 |
| | | // mUsartReceivePack[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; |
| | | // } |
| | | // } else if(gps_prase_flag) |
| | | // { |
| | | // static uint8_t index = 0; |
| | | ////GPS解析数据 |
| | | //// static ST_BLERecv BLE_recvive; |
| | | // GGA_DataStruct jdrecv,wdrecv,Posstate; |
| | | // |
| | | // //接收数据开始分析 |
| | | // mUsart2ReceivePack[index] = data; //char数组传进来参数data |
| | | // mUsart2ReceivePack_before = mUsart2ReceivePack_now; |
| | | // mUsart2ReceivePack_now=data; |
| | | // index++; |
| | | // if( mUsart2ReceivePack_before == 0x0D && mUsart2ReceivePack_now==0x0A )//接收数据到“0x0D 0x0A”结束 |
| | | // { |
| | | // //解析该条GPS报文 |
| | | // |
| | | // analysis_num=sscanf((char*)mUsart2ReceivePack,"$%[^,],%*[^,],%[^,],%*[^,],%[^,],%*[^,],%[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*s",gps_header,wdrecv.m_pData,jdrecv.m_pData,Posstate.m_pData); |
| | | // if(!memcmp(gps_header,"GNGGA",5)) |
| | | // { |
| | | // if(g_com_map[MODBUS_MODE]) |
| | | // LOG_INFO(TRACE_MODULE_APP,"%s", mUsart2ReceivePack); |
| | | // d_value = strtod(wdrecv.m_pData,NULL); |
| | | // if(d_value>1) |
| | | // { |
| | | // blink_led(&gps_success_state); |
| | | // wd=strtod(wdrecv.m_pData,NULL); |
| | | // jd=strtod(jdrecv.m_pData,NULL); |
| | | // pos_state=atoi(Posstate.m_pData); |
| | | // } |
| | | // } |
| | | // index = 0; |
| | | // if(pos_state!=0) |
| | | // { |
| | | // //4g.jd=jd; |
| | | // //4g.wd=wd; |
| | | // gps_timeout_flag=1;//不超时接收状态 |
| | | // gps_need_data_flag=0;//接收数据完成 |
| | | // gps_wait_count=0;//清0接收状态 |
| | | //// gps_enable_flag=0;//收到有效数据关闭GPS |
| | | // } |
| | | // memset(mUsart2ReceivePack,0,sizeof(mUsart2ReceivePack)); |
| | | // memset(wdrecv.m_pData,0,sizeof(wdrecv.m_pData)); |
| | | // memset(jdrecv.m_pData,0,sizeof(jdrecv.m_pData)); |
| | | // memset(Posstate.m_pData,0,sizeof(Posstate.m_pData)); |
| | | // index=0; |
| | | // mUsart2ReceivePack_before=0; |
| | | // mUsart2ReceivePack_now=0; |
| | | // } |
| | | // } |
| | | |
| | | } |
| | | |