新手入门
- 积分
- 14
- 金钱
- 14
- 注册时间
- 2019-6-8
- 在线时间
- 4 小时
|
2金钱
现有一个甲醛传感器,通过STM32F103串口中断读取数据,向传感器发送气体查询指令,则传感器立即返回相应数据,但在数据读取过程中时常出现数据顺序错乱,导致无法进行甲醛值计算,正常与异常读值状态如下
正常读取的数据
send hcho_voc inquire command:11 1 1 ed //气体查询指令
HCHO:216.53
VOC:0.00
--->>value16:16 d 1 0 1d 0 0 fe a4 0 0 0 0 0 0 1d //正常的数据回传格式,标头固定位16 d 1
send hcho_voc inquire command:11 1 1 ed
HCHO:567.47
VOC:0.00
--->>value16:16 d 1 0 4c 0 0 fe a4 0 0 0 0 0 0 ee //正常的数据回传格式,标头固定位16 d 1
异常读取的数据1
send hcho_voc inquire command:11 1 1 ed
HCHO:0.00
VOC:0.00
--->>value16:0 0 0 0 60 16 d 1 0 0 0 0 2 7a 0 0 //异常的数据回传格式
send hcho_voc inquire command:11 1 1 ed
HCHO:0.00
VOC:0.00
--->>value16:0 0 0 0 60 16 d 1 0 0 0 0 2 7a 0 0
异常读取的数据2
send hcho_voc inquire command:11 1 1 ed
HCHO:0.00
VOC:0.00
--->>value16:0 0 0 2 7a 0 0 0 0 0 0 60 16 d 1 0
send hcho_voc inquire command:11 1 1 ed
HCHO:0.00
VOC:0.00
--->>value16:0 0 0 2 7a 0 0 0 0 0 0 60 16 d 1 0
串口中断处理函数如下
void UART5_IRQHandler(void)
{
int total;
while(USART_GetITStatus(UART5,USART_IT_RXNE)!=RESET)
{
USART_ClearFlag(UART5,USART_FLAG_RXNE);
user_input_hv[tvoc_len] = USART_ReceiveData(UART5);
tvoc_len++;
}
if(tvoc_len>15)
{
tvoc_len=0;
tvoc_open=1;
}
if((tvoc_open==1)&&(user_input_hv[0]==0x16)&&(user_input_hv[1]==0x0D))
{
tvoc_open=0;
total=user_input_hv[0]+user_input_hv[1]\
+user_input_hv[2]+user_input_hv[3]\
+user_input_hv[4]+user_input_hv[5]\
+user_input_hv[6]+user_input_hv[7]\
+user_input_hv[8]+user_input_hv[9]\
+user_input_hv[10]+user_input_hv[11]\
+user_input_hv[12]+user_input_hv[13]\
+user_input_hv[14];
// printf("temp:%d\r\n",total);
total=total&0x00ff;
// printf("tempx:%d\r\n",total);
if(user_input_hv[15]==(256-total))
{
hcho=(float)(user_input_hv[3]*256+user_input_hv[4])/100;
hcho=hcho*(22.4/30)*1000;//ppb
voc=(float)(user_input_hv[5]*256+user_input_hv[6])/100;
temprature=(user_input_hv[7]*256+user_input_hv[8])/10;
relative_temp=(user_input_hv[9]*256+user_input_hv[10])/10;
}
}
}
main()函数中打印处理如下
/******************************************/
while(1)
{
USART_Cmd(UART5, ENABLE);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
Send_Hcoc_Voc_Inquire();//查询气体参数指令
delay_ms(300);
USART_ITConfig(UART5, USART_IT_RXNE,DISABLE);
USART_Cmd(UART5, DISABLE);
HCHO=Read_Hcho();
VOC=Read_Voc();
printf("HCHO:%.2f\r\n",HCHO);
printf("VOC:%.2f\r\n",VOC);
/************/
printf("--->>value16:");
for(bx=0;bx<16;bx++)printf("%0x ",user_input_hv[bx]);
printf("\r\n");
}
|
|