前阵子做的单片机项目要使用新框架,不允许使用状态机,真是哔了狗了,很多代码要改,最终确定的方案是收到256后,找头找尾,有就处理,没有就往前移128,再读128个字符出来,相较之前的状态机处理效率,显然会下降,但是领导要求如此,那也没辙,同时也优化了数据处理的相关流程,学会了使用memmove,新的机制是,串口接收超时或者接收缓冲区半满或者全满时会置位,主程序中不断查询这个位。
串口1与GPS通信的代码改为如下
void UART1_DATA_PROC(void) { char *pS=NULL,*pE=NULL; int count = 0; uint32_t gUart1RxLen = 0; uint8_t gUart1PrBuf[RX_MAXSIZE] = {0}; uint32_t HALF_RXLEN = UART1_RXBUF_LEN /2; // printf("g g= %d %d\n",gUart1ReadInd,gUart1RxIndex); if(gUart1ReadInd != HALF_RXLEN && gUart1ReadInd != 0) return; if(gUart1RecvInd > gUart1ReadInd) { gUart1RxLen = HALF_RXLEN; memcpy(gUart1PrBuf,gUart1RxBuf + gUart1RxLen,gUart1RxLen); gUart1RecvInd = 0; } else if(gUart1RecvInd == gUart1ReadInd) return; else { gUart1RxLen = HALF_RXLEN; memcpy(gUart1PrBuf,gUart1RxBuf,gUart1RxLen); gUart1RecvInd = gUart1RxLen; } memmove(Uart1RxBuf,Uart1RxBuf+HALF_RXLEN,HALF_RXLEN); memcpy(Uart1RxBuf+HALF_RXLEN,gUart1PrBuf,HALF_RXLEN); if(FirstTime == 0) { FirstTime = 1; return; } memset(Uart1ProcBuff,0,RX_MAXSIZE); pS = (char *)strstr(Uart1RxBuf,CMD_RMC); pE = (char *)strstr(pS,END_FLAG); if(pS != NULL && pE != NULL) { count = pE-pS; if(count > GPS_MAXLENTH) return; memcpy(Uart1ProcBuff,pS,count); GpsDateType = RMC; if(count < GPS_MINLENTH) GpsDateType = NOUSE; GPS_DATA_PROC(); } memset(Uart1ProcBuff,0,RX_MAXSIZE); pS = (char *)strstr(Uart1RxBuf,CMD_GGA); pE = (char *)strstr(pS,END_FLAG); if(pS != NULL && pE != NULL) { count = pE-pS; if(count > GPS_MAXLENTH) return; memcpy(Uart1ProcBuff,pS,count); GpsDateType = GGA; if(count < GPS_MINLENTH) GpsDateType = NOUSE; GPS_DATA_PROC(); } }
串口0与上位机通信代码改为如下
void UART0_DATA_PROC() { uint8_t ret = 0; uint8_t len = 0; uint32_t tUart0RxLen = 0; PM5K_PCM_DEV_PP_YDT1363_3 YdtBuf; if(gUart0RecvInd > gUart0ReadInd) { tUart0RxLen = UART0_RXBUF_LEN - gUart0RecvInd; memcpy(gUart0PrBuf,gUart0RxBuf + gUart0RecvInd,tUart0RxLen); gUart0RxLen += tUart0RxLen; gUart0RecvInd = gUart0ReadInd; if(gUart0RxLen < RCV_LENTH) return; } else if(gUart0RecvInd == gUart0ReadInd) return; else { tUart0RxLen = gUart0ReadInd - gUart0RecvInd; if(gUart0RxLen > 0) memcpy(gUart0PrBuf+gUart0RxLen,gUart0RxBuf + gUart0RecvInd,tUart0RxLen); else memcpy(gUart0PrBuf,gUart0RxBuf + gUart0RecvInd,tUart0RxLen); gUart0RxLen += tUart0RxLen; gUart0RecvInd = gUart0ReadInd; if(gUart0RxLen != RCV_LENTH) { if(gUart0RxLen < RCV_LENTH) { if(gUart0RecvInd == UART0_RXBUF_LEN/2) return; } else { gUart0RxLen = 0; memset(gUart0PrBuf,0,20); return; } } } len = (uint8_t)gUart0RxLen; gUart0RxLen = 0; ret = ProtocolProc_YDT(TRUE,&YdtBuf, gUart0PrBuf, &len); memset(gUart0PrBuf,0,20); if(ret) { PopCmdQueue(ret); } }
时间: 2024-10-13 16:51:02