stm32串口通信,收发字符串,并对其进行解析

串口以字符串接收和发送

将传输的数据转化为整数(正负)

stm32发送端

	motor_position = Read_Encoder_Angle(Encoder);
  	sensor_position = Get_Adc_Average_Angle(Adc);
    motor_velocity = Read_Encoder_Speed(Encoder);
	sensor_velocity = Get_Adc_Average_Speed(); 
	sprintf(data_str, "%-8.4f, %-8.4f, %-8.4f, %-8.4f\n", motor_position , sensor_position , motor_velocity , sensor_velocity );
	Usart_SendString(USART1, data_str);

stm32接收端

接收数据格式为:\t16900\r\n,其中第一位“\t”为帧头,第二位数据1为奇偶校验位,最后两位数据"\r\n"为帧尾,中间数据为实际传输数据,即“6900”。

void USART1_IRQHandler(void)                	//串口1中断服务程序
	{
	

	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //接收中断(接收到的数据必须是0x0d 0x0a结尾)
		{
		Res = USART_ReceiveData(USART1);	//读取接收到的数据 0x2D,0x31,0x30,0x30,0x30,0x0D,0x0A

				if((USART_RX_STA&0x8000)==0)//接收未完成
					{
					if(USART_RX_STA&0x4000)//接收到了0x0D
						{
							if(Res!=0x0A){
								USART_RX_STA=0;//接收错误,重新开始
								memset(USART_RX_BUF,0,USART_REC_LEN);
							}
							else{
								USART_RX_STA|=0x8000;	//接收完成了
								
								check_flag = count_odd_numbers(2);
								USART_RX_BUF[1] = USART_RX_BUF[1] - '0';
								if(USART_RX_BUF[0] == '\t'&& USART_RX_BUF[1] == check_flag ) // 判断帧头是否正确、判断奇偶校验位是否正确 || USART_RX_BUF[1] == check_flag
								{
									float value = 0;
									int16_t sign = 1;
										int a = 2;
											for (int i = a; i < (USART_RX_STA&0X3FFF); i++) {
												 value = value * 10 + USART_RX_BUF[i] - '0';		
											}
											action = sign * value;
											USART_RX_STA = 0;	
											memset(USART_RX_BUF,0,USART_REC_LEN);
							}
							else{
								USART_RX_STA = 0;	
								memset(USART_RX_BUF,0,USART_REC_LEN);
							}
						}								
						}
					else //还没收到0X0D
						{	
							if(Res==0x0D)
								USART_RX_STA|=0x4000;
							else
								{					
										USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
										USART_RX_STA++;
										if(USART_RX_STA>(USART_REC_LEN-1))
										{
											USART_RX_STA=0;//接收数据错误,重新开始接收
											memset(USART_RX_BUF,0,USART_REC_LEN);
										}					
								}		 
						}
					}

				 } 
}

将传输的数据转化为浮点数(正负)

stm32发送端

	motor_position = Read_Encoder_Angle(Encoder);
  	sensor_position = Get_Adc_Average_Angle(Adc);
    motor_velocity = Read_Encoder_Speed(Encoder);
	sensor_velocity = Get_Adc_Average_Speed(); 
	sprintf(data_str, "%-8.4f, %-8.4f, %-8.4f, %-8.4f\n", motor_position , sensor_position , motor_velocity , sensor_velocity );
	Usart_SendString(USART1, data_str);

stm32接收端

接收数据格式为:\t11.0246\r\n,其中第一位“\t”为帧头,第二位数据1为奇偶校验位,最后两位数据"\r\n"为帧尾,中间数据为实际传输数据,即“1.0246”。

void USART1_IRQHandler(void)                	//串口1中断服务程序
	{
	

	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //接收中断(接收到的数据必须是0x0d 0x0a结尾)
		{
		Res = USART_ReceiveData(USART1);	//读取接收到的数据 0x2D,0x31,0x30,0x30,0x30,0x0D,0x0A

				if((USART_RX_STA&0x8000)==0)//接收未完成
					{
					if(USART_RX_STA&0x4000)//接收到了0x0D
						{
							if(Res!=0x0A){
								USART_RX_STA=0;//接收错误,重新开始
								memset(USART_RX_BUF,0,USART_REC_LEN);
							}
							else{
								USART_RX_STA|=0x8000;	//接收完成了
								
								check_flag = count_odd_numbers(2);
								USART_RX_BUF[1] = USART_RX_BUF[1] - '0'; // 串口接收的是ASCII码,将其与'0'作差,则获得相应的整数。
								if(USART_RX_BUF[0] == '\t'&& USART_RX_BUF[1] == check_flag ) // 判断帧头是否正确、判断奇偶校验位是否正确 || USART_RX_BUF[1] == check_flag
								{
									float value = 0;
									int16_t sign = 1;
										int a = 2;
										if(USART_RX_BUF[2] == '-')
										{
											sign = -1;
											a = 3;
										}
											for (int i = a; i < (USART_RX_STA&0X3FFF); i++) {
												if(USART_RX_BUF[i] != '.')
											{
												 value = value * 10 + USART_RX_BUF[i] - '0';	
												 value +=  (USART_RX_BUF[i] - '0') * pow(10, -(i - a));
											}
												else
												{
													a = a+ 1;
												}
												
											}
											action = sign * value;
											USART_RX_STA = 0;	
											memset(USART_RX_BUF,0,USART_REC_LEN);
							}
							else{
								USART_RX_STA = 0;	
								memset(USART_RX_BUF,0,USART_REC_LEN);
							}
						}								
						}
					else //还没收到0X0D
						{	
							if(Res==0x0D)
								USART_RX_STA|=0x4000;
							else
								{					
										USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
										USART_RX_STA++;
										if(USART_RX_STA>(USART_REC_LEN-1))
										{
											USART_RX_STA=0;//接收数据错误,重新开始接收
											memset(USART_RX_BUF,0,USART_REC_LEN);
										}					
								}		 
						}
					}

				 } 
}

你可能感兴趣的:(倒立摆simtoreal,嵌入式,stm32,嵌入式硬件,单片机)