#include <rtthread.h>
#include <stm32f10x.h>
#include <string.h>
#include "uart_app.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
u32 RxCounter=0;
u16 RxStop=0;
u16 RxStart=0;
char RxBuffer[GPS_RX_BUFFER_SIZE];
char TxBuffer[GPS_RX_BUFFER_SIZE];
u16 TxStart=0;
u16 TxStop=0;
char gps_test[]="$GPTXT,01,01,02,ANTSTATUS=OK*3B\r\n$GPRMC,000319.090,V,1234.5678,N,8756.4321,E,,,150209,,,N*7B\r\n" ;
typedef struct _GPS_RMC_Data
{
double utc_time; //时间
char status;
double latitude; //纬度
char ns_indicator;
double longitude; //经度
char ew_indicator;
double speed; //速度
double course; //航向
u32 date; //日期
}GPS_RMC_Data;
GPS_RMC_Data Last_RMC;
void command_process(char *GPS_Buffer);
void gps_process();
void USART1_SendString(char *str,int len)
{
unsigned int lenCounter;
if(len == -1)
{
len = strlen(str);
}
for(lenCounter=0;lenCounter<len;lenCounter++)
{
USART_SendData(USART1, *str);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
str++;
}
}
void USART2_SendString(char *str,int len)
{
unsigned int lenCounter;
if(len == -1)
{
len = strlen(str);
}
for(lenCounter=0;lenCounter<len;lenCounter++)
{
USART_SendData(USART2, *str);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
str++;
}
}
char USART2_ReceStopDect()
{
if(RxStart!=RxCounter)
{
RxStop=RxCounter;
rt_thread_delay(50);
if(RxStop==RxCounter)
{
return (2); //串口数据接收完成
}
else return (1); //串口数据接收未完成
}
else return (0); //串口无新数据
}
void Usart2RecvHandle()
{
u16 i=0;
if(USART2_ReceStopDect()==FINISH) //串口1有新数据
{
while(RxStart!=RxStop)
{
TxBuffer[i]=RxBuffer[RxStart];
i=(i+1)% GPS_RX_BUFFER_SIZE;
RxStart=(RxStart+1)%GPS_RX_BUFFER_SIZE;
TxStop=i;
}
USART1_SendString(TxBuffer,TxStop);
gps_process();
}
}
void USART2_isr()
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //for rx
{
/* Read one byte from the receive data register */
RxBuffer[RxCounter] = (USART_ReceiveData(USART2) & 0xFF);
RxCounter =(RxCounter+1)%GPS_RX_BUFFER_SIZE;
/* Clear the USART1 Receive interrupt */
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}
int Bufferstrsearch(char *ptr2,char *ptr1,u32 start,u32 stop)//查字符串*ptr2是否在*ptr1中,只能用于检测RxBuffer2
//本函数是用来检查字符串*ptr2是否完全包含在*ptr1中
//返回: 0 没有找到
{
u8 i,k1,k2;
u8 tmp1;
u8 flag=0;
u32 Pos=start;
k1=strlen(ptr2);
k2=(stop+1-start+GPS_RX_BUFFER_SIZE)%GPS_RX_BUFFER_SIZE;
for(i=0;i<(k2+1);i++)//不能越过NewStop
{
if(((start+i)%GPS_RX_BUFFER_SIZE)==((RxStop+1)%GPS_RX_BUFFER_SIZE))
{
if(i!=k2)
return -1;
}
}
if(k2<k1)
{
return(-1);
}
for(i=0;i<=k2-k1;i++)
{
if(*ptr2==*(ptr1+Pos%GPS_RX_BUFFER_SIZE))
{
flag=1;
for(tmp1=0;tmp1<k1;tmp1++)
{
if((*(ptr2+tmp1))!=(*(ptr1+(tmp1+Pos)%GPS_RX_BUFFER_SIZE)))
flag=0;
}
if(flag==1)
{
return Pos;
//return 1;
}
}
Pos++;
}
return(-1);
}
u32 pos; u32 tsp=100;
void gps_process()
{
u32 i;
char test[50];
pos=Bufferstrsearch("$GPRMC",TxBuffer,TxStart,TxStop);
sprintf(test,"\r\n%d\r\n",pos);
USART1_SendString(test, strlen(test));
if(pos != -1)
{
for(i=pos;i<TxStop;i++) //最后一条指令位置
{
if((i > pos) &&TxBuffer[i-1] == '\r' && TxBuffer[i] == '\n')
{
GPIO_ResetBits(GPIOC, GPIO_Pin_13 );
GPIO_ResetBits(GPIOA, GPIO_Pin_1 );
command_process(TxBuffer);
// command_process(TxBuffer);
break;
}
}
// command_process(TxBuffer);
}
}
void command_process(char *GPS_Buffer)
{
char command[100];
char location_data[30];
u32 i,j;
for(i=0,j=pos;j<strlen(GPS_Buffer);i++,j++) //两个逗号间补零
{
command[i]=GPS_Buffer[j];
if((GPS_Buffer[j]=='\n')&&(GPS_Buffer[j-1]=='\r'))
{
break;
}
if((GPS_Buffer[j]==',')&&(GPS_Buffer[j+1]==','))
{ command[++i]='0'; }
}
USART1_SendString(command,strlen(command));
sscanf(command, "$GPRMC,%lf,%c,%lf,%c,%lf,%c,%lf,%lf,%d,%*lf,%*c,%*c,%*s", \
&(Last_RMC.utc_time),&(Last_RMC.status),&(Last_RMC.latitude),&(Last_RMC.ns_indicator),&(Last_RMC.longitude),&(Last_RMC.ew_indicator),&(Last_RMC.speed),&(Last_RMC.course),&(Last_RMC.date));
memset(location_data,0,30);
sprintf(location_data,"\r\n%10.4f,%10.4f\r\n",(Last_RMC.latitude),(Last_RMC.longitude));
USART1_SendString(location_data,strlen(location_data));
}