| | |
| | | //#include "radio.h" |
| | | |
| | | |
| | | |
| | | #define MSG_RW 3 |
| | | #define MSG_RW_TAG_ANC 0x30 |
| | | //#define EUART_RX_BUF_SIZE 100 |
| | | #define Label_id_local 1 //标签id |
| | | #define data_buff_MAX 50 //基站数量 |
| | |
| | | |
| | | uint8_t mUsartReceivePack[100] = {0}; |
| | | uint8_t mUsart2ReceivePack[150] = {0}; |
| | | uint8_t GPS_GGAmessage[150]={0}; |
| | | uint8_t GPS_GSVmessage[150]={0}; |
| | | double jd,wd; |
| | | int analysis_num,pos_state; |
| | | uint8_t state5V_prase_flag=1,gps_prase_flag=1; |
| | |
| | | extern void IO_control_init(void); |
| | | extern void blink_led(uint8_t*state); |
| | | extern void updata_led_power_state(void); |
| | | extern uint8_t gps_success_state; |
| | | extern uint8_t gps_success_state,gps_enable_flag,gps_need_data_flag,gps_timeout_flag; |
| | | extern uint8_t gps_success_state,gps_open_flag; |
| | | extern uint8_t gps_success_state,gps_enable_flag,gps_need_data_flag,gps_timeout_flag,gps_power_state; |
| | | extern uint16_t gps_wait_count; |
| | | typedef enum |
| | | { |
| | |
| | | checksum = Checksum_u16(&send_frame[2],5+data_length); |
| | | memcpy(&send_frame[7+data_length],&checksum,2); |
| | | |
| | | uart_send(UART_ID1, send_frame,data_length+9, NULL); |
| | | uart_send(UART_ID0, send_frame,data_length+9, NULL); |
| | | } |
| | | void UpdateProcess(uint8_t index) |
| | | { |
| | |
| | | |
| | | } |
| | | double d_value; |
| | | uint16_t pwtag_idrec,pwanc_id,pwanc_value; |
| | | uint8_t taguserdata_recflag[TAG_NUM_IN_SYS],pwtag_index = 0; |
| | | extern uint16_t mubiao_anchor_ID; |
| | | struct pwtag_structure pwtag; |
| | | extern uint8_t Get_anchor_appdata_flag; |
| | | extern uint8_t Get_anchor_appdata_num; |
| | | void UsartParseDataHandler(uint8_t data) |
| | | { |
| | | if(state5V_prase_flag&&!g_com_map[MODBUS_MODE]) |
| | |
| | | if(usart_receive_state == UsartReceiveWaitChecksum) |
| | | { //若收到校验和包 |
| | | checksum = 0; |
| | | for(int i = 0; i<pack_length-5; i++) { |
| | | for(int i = 0; i<pack_length-2; i++) { |
| | | checksum += mUsartReceivePack[i]; |
| | | } |
| | | checksum += pack_cmd; |
| | | checksum += pack_length; |
| | | checksum += pack_index; |
| | | checksum += pack_datalen; |
| | | checksum += pack_msgtype; |
| | | checksum += pack_msgtype; |
| | | checksum += pack_length; |
| | | if(((data + checksum)&0xff) == 0xFF) //校验通过 |
| | | { |
| | | switch(pack_msgtype) |
| | | { |
| | | uint16_t serial_tag_id_recv; |
| | | case MSG_RW: |
| | | pack_cmd = mUsartReceivePack[0]; |
| | | pack_index = mUsartReceivePack[1]; |
| | | pack_datalen = mUsartReceivePack[2]; |
| | | switch(pack_cmd) |
| | | { |
| | | case CMD_WRITE: |
| | | if(pack_index==MODBUS_MODE*2) |
| | | { |
| | | Uart1GpsRecDebugSend(); |
| | | g_com_map[MODBUS_MODE] = 1; |
| | | return; |
| | | } |
| | | // 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); |
| | | memcpy((uint8_t*)&g_com_map + pack_index, &mUsartReceivePack[3], pack_datalen); |
| | | |
| | | if(g_com_map[YAOGEIANC_UPWENJIAN_FLAG]==1) |
| | | { |
| | | Get_anchor_appdata_flag=1; |
| | | Get_anchor_appdata_num=0; |
| | | save_com_map_to_flash(); |
| | | delay_ms(300); |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | break; |
| | | } |
| | | |
| | | |
| | | if(mUsartReceivePack[0]==1) |
| | | UpdateProcess(pack_index); |
| | | //返回一个error状态 |
| | |
| | | SendComMap(pack_datalen,pack_index>>1); |
| | | break; |
| | | default: |
| | | break; |
| | | 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) |
| | | break; |
| | | |
| | | case MSG_RW_TAG_ANC: //批量修改标签参数 |
| | | memcpy(&pwtag_idrec,&mUsartReceivePack[2],2); |
| | | if(mUsartReceivePack[0]<0x10) //判断是修改基站还是修改标签。 |
| | | { |
| | | blink_led(&gps_success_state); |
| | | wd=strtod(wdrecv.m_pData,NULL); |
| | | jd=strtod(jdrecv.m_pData,NULL); |
| | | pos_state=atoi(Posstate.m_pData); |
| | | if(mUsartReceivePack[0]==0x06)//如果是升级 |
| | | { |
| | | for(uint8_t i=0; i<PWTAG_MAXGROUPNUM; i++) |
| | | { |
| | | if(pwtag.groupid[i][0]==pwtag_idrec) |
| | | { |
| | | pwtag.duorxie[i]=mUsartReceivePack[0]; |
| | | pwtag.index[i] = mUsartReceivePack[1]; |
| | | pwtag.remain_time[i] = 5; |
| | | memcpy(pwtag.groupid[i],&mUsartReceivePack[2],4); |
| | | pwtag.len[pwtag_index] = pack_length-8; |
| | | memcpy(&pwtag.value[i][0],&g_com_map[YAOGEIANC_UPWENJIAN_DAXIAO],2); |
| | | memcpy(&mubiao_anchor_ID,&mUsartReceivePack[2],2); |
| | | break; |
| | | } |
| | | if(i==PWTAG_MAXGROUPNUM-1) |
| | | { |
| | | if(pwtag_index++>=PWTAG_MAXGROUPNUM) |
| | | { |
| | | pwtag_index = 0; |
| | | } |
| | | pwtag.index[pwtag_index] = mUsartReceivePack[1]; |
| | | pwtag.remain_time[pwtag_index] = 5; |
| | | pwtag.duorxie[pwtag_index]=mUsartReceivePack[0]; |
| | | memcpy(pwtag.groupid[pwtag_index],&mUsartReceivePack[2],4); |
| | | pwtag.len[pwtag_index] = pack_length-8; |
| | | memcpy(&pwtag.value[pwtag_index][0],&g_com_map[YAOGEIANC_UPWENJIAN_DAXIAO],2); |
| | | memcpy(&mubiao_anchor_ID,&mUsartReceivePack[2],2); |
| | | } |
| | | } |
| | | } |
| | | else |
| | | { |
| | | for(uint8_t i=0; i<PWTAG_MAXGROUPNUM; i++) |
| | | { |
| | | if(pwtag.groupid[i][0]==pwtag_idrec) |
| | | { |
| | | pwtag.duorxie[i]=mUsartReceivePack[0]; |
| | | pwtag.index[i] = mUsartReceivePack[1]; |
| | | pwtag.remain_time[i] = 5; |
| | | memcpy(pwtag.groupid[i],&mUsartReceivePack[2],4); |
| | | pwtag.len[pwtag_index] = pack_length-8; |
| | | memcpy(&pwtag.value[i][0],&mUsartReceivePack[6],pack_length-8); |
| | | break; |
| | | } |
| | | if(i==PWTAG_MAXGROUPNUM-1) |
| | | { |
| | | if(pwtag_index++>=PWTAG_MAXGROUPNUM) |
| | | { |
| | | pwtag_index = 0; |
| | | } |
| | | pwtag.index[pwtag_index] = mUsartReceivePack[1]; |
| | | pwtag.remain_time[pwtag_index] = 5; |
| | | pwtag.duorxie[pwtag_index]=mUsartReceivePack[0]; |
| | | memcpy(pwtag.groupid[pwtag_index],&mUsartReceivePack[2],4); |
| | | pwtag.len[pwtag_index] = pack_length-8; |
| | | memcpy(&pwtag.value[pwtag_index][0],&mUsartReceivePack[6],pack_length-8); |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | 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 |
| | | break; |
| | | } |
| | | //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)); |
| | | } |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | index=0; |
| | | mUsart2ReceivePack_before=0; |
| | | mUsart2ReceivePack_now=0; |
| | | } else if(usart_receive_state == UsartReceiveWaitData) { //若果收到的是正常通讯包 |
| | | mUsartReceivePack[index] = data; |
| | | index++; |
| | | if(index == pack_length-2) { //如果收到的index与长度相等 |
| | | usart_receive_state = UsartReceiveWaitChecksum; |
| | | } |
| | | } else if(usart_receive_state == UsartReceiveWaitLength) { //收到长度字节 |
| | | |
| | | pack_length = data; |
| | | pack_index = 0; |
| | | usart_receive_state = UsartReceiveWaitData; |
| | | |
| | | } 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)) { |
| | | 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)) |
| | | // { |
| | | // memcpy(GPS_GGAmessage,mUsart2ReceivePack,strlen(mUsart2ReceivePack)-2); |
| | | // |
| | | // wd=strtod(wdrecv.m_pData,NULL); |
| | | // jd=strtod(jdrecv.m_pData,NULL); |
| | | // if(wd!=0) |
| | | // blink_led(&gps_success_state); |
| | | // pos_state=atoi(Posstate.m_pData); |
| | | // if(pos_state!=0) |
| | | // { |
| | | // gps_timeout_flag=0;//不超时接收状态 |
| | | // |
| | | // gps_need_data_flag=0;//接收数据完成 |
| | | // gps_wait_count=0;//清0接收状态 |
| | | // //gps_power_state=0;//关闭gps |
| | | // if(gps_open_flag){ |
| | | // UDPClient_UploadGPS();//上传GPS超时无效数据 |
| | | // gps_power_state=0;//立即关掉gps,防止串口数据过多导致无法切换休眠 |
| | | // update_led_power_state(); |
| | | // } |
| | | // pos_state=0;//防止多次进入 |
| | | // } |
| | | // |
| | | // } |
| | | //// if(!memcmp(gps_header,"GBGSV",5)) |
| | | //// {memcpy(GPS_GSVmessage,mUsart2ReceivePack,strlen(mUsart2ReceivePack)-2); |
| | | //// GPS_ParseGSV(GPS_GSVmessage,strlen(GPS_GSVmessage)); |
| | | //// |
| | | //// } |
| | | // |
| | | // 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; |
| | | // } |
| | | // } |
| | | } |
| | | |