国赛题目看着简单其实还是挺复杂的,所以说不能掉以轻心,目前遇到的问日主要有以下几点:
本次题主要注重的是信息交互,与A板通信的有电脑主机和B板,所以处理好这里面的交互过程很重要
国赛中避免不了会收到其他选手的信息,这个是外界干扰这个也是必须要考虑的,可以试着给传递的信息添加一个头部,处理带有特定头部的信息,当然肯定会接收一些垃圾信息,发现接收字符串不为空格要及时清空防止收到自己板子发送的消息的时候受影响
void Function_ReceiveHandle(){
if(RECEIVEMSG[0] == '#'){
if(MEMKEFLAG == 1){
//OLED_ShowString(2, RECEIVEMSG);
MEMTEMP[0] = RECEIVEMSG[1];
MEMTEMP[1] = RECEIVEMSG[2];
MEMTEMP[2] = RECEIVEMSG[3];
MEMTEMP[3] = RECEIVEMSG[4];
MEMTEMP[4] = '\0';
}
if(MEMKEFLAG == 2){
unsigned char i = 1;
unsigned char j = 0;
while(RECEIVEMSG[i] != '#'){
MEMAXTEMP[j ++] = RECEIVEMSG[i ++];
}
MEMAXTEMP[j] = '\0';
i = i + 1;
unsigned char k = 0;
while(RECEIVEMSG[i] != '\0'){
MEMINTEMP[k ++] = RECEIVEMSG[i ++];
}
MEMINTEMP[k] = '\0';
//OLED_ShowString(0, MEMAXTEMP);
//OLED_ShowString(2, MEMINTEMP);
}
if(MEMKEFLAG == 4){
MEMK1FLAG = RECEIVEMSG[1];
}
if(MEMK2FLAG == 5){
MEMK2FLAG = RECEIVEMSG[1];
}
}
if(RECEIVEMSG[0] != '\0') for(unsigned char i = 0; i < 10; i ++) RECEIVEMSG[i] = '\0'; // 清空接收到垃圾信息
}
当然LORA通信是这样,UART通信也是如此,处理完收到的消息后就将接收字符清空,防止对后面接收产生影响如第一次接收123456,第二次接收999那么第二次会显示999456
国赛难点就在如何处理接收的长字符串,本次处理过程很是丑陋
void Function_ReceiveHandle(){
if(UARTRECEIVEMSG[0] != '\0'){ // 非空
if((UARTRECEIVEMSG[0] == 'M' && UARTRECEIVEMSG[1] == 'A' && UARTRECEIVEMSG[2] == 'X')
|| (UARTRECEIVEMSG[0] == 'M' && UARTRECEIVEMSG[1] == 'I' && UARTRECEIVEMSG[2] == 'N')
){
if(UARTRECEIVEMSG[3] != '\0' && UARTRECEIVEMSG[3] >= '0' && UARTRECEIVEMSG[3] <= '9'){
TEMP = UARTRECEIVEMSG[3] - '0';
}
if(UARTRECEIVEMSG[4] != '\0'){
if(UARTRECEIVEMSG[4] >= '0' && UARTRECEIVEMSG[4] <= '9'){
TEMP = TEMP * 10 + UARTRECEIVEMSG[4] - '0';
}else TEMP = 0;
}
//if(UARTRECEIVEMSG[5] != '\0') TEMP = 0;
}
if(TEMP != 0){
if(UARTRECEIVEMSG[1] == 'I') MINTEMP = TEMP;
else MAXTEMP = TEMP;
Function_UartSendMsg((unsigned char* )"OK", 2);
}else if(TEMP == 0){
Function_UartSendMsg((unsigned char* )"ERROR", 5);
}
TEMP = 0;
OLED_ShowString(2, UARTRECEIVEMSG);
for(unsigned char i = 0; i < 20; i ++) UARTRECEIVEMSG[i] = '\0';
}
}
用的是if else语句很是繁琐,后面想优化优化看能不能合理利用c中的字符串处理函数
对于数据接收完整性来看LORA通信接收数据还是很完整的,毕竟有FIFO线,现在问题来到串口串口接收数据因为国赛代码量变多,在执行其他代码的时间块也会变多,所以串口单靠延时接收数据已经不能确保接收完整性了,用中断接收又不能接收不定长的数据,所以准备学习串口DMA接收数据了