STM32--解析GPS数据包代码

头文件

#ifndef __gps_H
#define __gps_H

#define USART_REC_LEN  			200 //定义最大接收字节数
#define GPS_Buffer_Length 		80	//定义数组长度
#define UTCTime_Length 			11
#define latitude_Length 		11
#define N_S_Length 				2	//定义为2以便在printf%s时不出现乱码
#define longitude_Length 		12
#define E_W_Length 				2 
#define false 					0
#define true 					1

typedef struct SaveData 
{
	char GPS_Buffer[GPS_Buffer_Length];
	char isGetData;							//是否获取到GPS数据
	char isParseData;						//是否解析完成
	char UTCTime[UTCTime_Length];			//UTC时间
	char latitude[latitude_Length];			//纬度
	char N_S[N_S_Length];					//N/S
	char longitude[longitude_Length];		//经度
	char E_W[E_W_Length];					//E/W
	char isUsefull;							//定位信息是否有效
} _SaveData;

void errorLog(int num);
void parseGpsBuffer(void);
void printGpsBuffer(void);
	
#endif


C文件

#include "stdio.h"
#include "sys.h"
#include "string.h"
#include "gps.h"
#include "delay.h"

u16  point1 = 0;						//接收数组标号
char USART2_RX_BUF[USART_REC_LEN];   	//接收缓冲,最大USART_REC_LEN个字节.
_SaveData Save_Data;					//GPS结构体变量,存储结构体信息

/**
  * @brief  解析GPS发过来的数据
  *
  * @retval None
  */
void parseGpsBuffer(void)
{
	char *subString;
	char *subStringNext;
	char i = 0;
	if (Save_Data.isGetData)	//判断是否获取到GPS数据
	{
		Save_Data.isGetData = false;
		printf("****************************************\r\n");
//		printf("%s\n", Save_Data.GPS_Buffer);	//将获取到的数据打印出来
		for (i = 0; i <= 6; i++)				//获取6段数据
		{
			if (i == 0)
			{
				if ((subString = strstr(Save_Data.GPS_Buffer, ",")) == NULL)	//查找逗号
					errorLog(1);	//解析错误
			}
			else
			{
				subString++;		//偏移到逗号后面
				if ((subStringNext = strstr(subString, ",")) != NULL)	//查找下一个逗号
				{
					char usefullBuffer[2]; 
					switch(i)
					{
						case 1:
							memcpy(Save_Data.UTCTime, subString, subStringNext - subString); //获取UTC时间
							break;	
						case 2:
							memcpy(usefullBuffer, subString, subStringNext - subString); //获取定位状态,A=有效定位,V=无效定位
							break;	
						case 3:
							memcpy(Save_Data.latitude, subString, subStringNext - subString); //获取纬度信息(度分)
							break;	
						case 4:
							memcpy(Save_Data.N_S, subString, subStringNext - subString); //获取纬度半球(N/S)
							break;	
						case 5:
							memcpy(Save_Data.longitude, subString, subStringNext - subString); //获取经度信息(度分)
							break;	
						case 6:
							memcpy(Save_Data.E_W, subString, subStringNext - subString); //获取经度半球(E/W)
							break;	
						default:
							break;
					}
					subString = subStringNext;
					Save_Data.isParseData = true;		//解析数据成功标志
					if(usefullBuffer[0] == 'A')			//有效定位
					{
						Save_Data.isUsefull = true;
					}
					else if(usefullBuffer[0] == 'V')	//无效定位
					{
						Save_Data.isUsefull = false;
					}	
				}
				else
				{
					errorLog(2);	//解析错误
				}
			}
		}
	}
}

/**
  * @brief  打印解析后的GPS数据
  *
  * @retval None
  */
void printGpsBuffer(void)
{
	if (Save_Data.isParseData)	//判断GPS数据是否解析完成
	{
		printf("Save_Data.UTCTime = ");
		printf("%s\n", Save_Data.UTCTime);
		printf("\r\n");

		if(Save_Data.isUsefull)
		{
			Save_Data.isUsefull = false;
			printf("Save_Data.latitude = ");
			printf("%s\n", Save_Data.latitude);
			printf("\r\n");


			printf("Save_Data.N_S = ");
			printf("%s\n", Save_Data.N_S);
			printf("\r\n");

			printf("Save_Data.longitude = ");
			printf("%s\n", Save_Data.longitude);
			printf("\r\n");

			printf("Save_Data.E_W = ");
			printf("%s\n", Save_Data.E_W);
			printf("\r\n");
		}
		else
		{
			printf("GPS DATA is not usefull!\r\n");
		}
	}
}

void errorLog(int num)
{
	while (1)
	{
		printf("ERROR %d\r\n",num);
	}
}

void USART2_IRQHandler(void)	//将$GPRMC这一帧数据存储到GPS_Buffer中
{
	u8 from_gps_char;	      
	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
	{	 
		from_gps_char = USART_ReceiveData(USART2);
		
		if(from_gps_char == '$')
		{
			point1 = 0;
			memset(USART2_RX_BUF, 0, USART_REC_LEN); //清空			
		}
	  	USART2_RX_BUF[point1++] = from_gps_char;		//将接收到的值存到数组中
		
		if(USART2_RX_BUF[0] == '$' && USART2_RX_BUF[4] == 'M' && USART2_RX_BUF[5] == 'C')	//确定是否收到"GPRMC/GNRMC"这一帧数据
		{
			if(from_gps_char == '\n')									   
			{
				memset(Save_Data.GPS_Buffer, 0, GPS_Buffer_Length);	 //清空GPS_Buffer缓冲区
				memcpy(Save_Data.GPS_Buffer, USART2_RX_BUF, point1); //将接收到的数据保存到GPS_Buffer缓冲区
				Save_Data.isGetData = true;		//确认接收完成一帧有效数据
				point1 = 0;
				memset(USART2_RX_BUF, 0, USART_REC_LEN);	//清空				
			}	
		}
		if(point1 >= USART_REC_LEN)
		{
			point1 = USART_REC_LEN;
		}	
	}  				 											 
}   


你可能感兴趣的:(STM32)