📄 gps.c
字号:
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h> // Must include . or, the result is wrong!!!!!!!!!
#include "serial.h"
#include "gps.h"
#include "comm.h"
#define USE_BEIJING_TIMEZONE
#define MAXMSGLEN 256
char GPS_BUF[MAXMSGLEN];
GPS_INFO gps_info;
int GetCharPos(int num,char* str);
void UTC2BTC(date_time *GPS);
double get_double_number(char *s);
////////////////////////////////////////////////////////////////////////
// Test main()
//
#if 1
#define ENDMINITERM 27 /* ESC to quit miniterm */
volatile int STOP=FALSE;
void child_handler(int s);
void* keyboard(void * data);
void* receive(void * data);
void* show_gps_info(void * data);
int main(int argc,char** argv)
{
struct sigaction sa;
int ok;
pthread_t th_a, th_b, th_c;
void * retval;
GPS_Init();
sa.sa_handler = child_handler;
sa.sa_flags = 0;
sigaction(SIGCHLD,&sa,NULL); /* handle dying child */
pthread_create(&th_a, NULL, keyboard, 0);
pthread_create(&th_b, NULL, receive, 0);
pthread_join(th_a, &retval);
pthread_join(th_b, &retval);
printf("exit main 1\n");
close(fd2);
printf("exit main 2\n");
return(0);
}
void child_handler(int s)
{
printf("stop!!!\n");
STOP=TRUE;
}
void* keyboard(void * data)
{
int c;
c=getchar();
STOP=TRUE;
printf("exit keyboard\n");
return NULL;
}
void* receive(void * data)
{
int i=0, tmp=0;
char c;
char buf[256];
GPS_INFO GPS;
while (STOP==FALSE) {
//tmp = read(fd2,&c,1); /* 直接阻塞读串口 */
tmp = readport(fd2,2000,&c);
buf[i++] = c;
if(c == '\n'){
buf[i]='\0';
i=0;
if( buf[5]=='C' || (buf[4]=='G'&&buf[5]=='A')){ //"GPRMC" "$GPGGA"
strcpy(GPS_BUF, buf);
gps_parse(GPS_BUF,&gps_info);
show_gps(&gps_info);
printf("%s", GPS_BUF);
}
}
}
printf("exit from reading modem\n");
return NULL;
}
#endif
//
// Test main()
////////////////////////////////////////////////////////////////////////
void GPS_Init()
{
fd2 = OpenDev(COM2);
set_speed(fd2,4800); //设置开发平台的串口2波特率
if (set_Parity(fd2,8,1,'N') == FALSE) {
printf("Set Parity Error\n");
exit (0);
}
usleep(1000000);
}
void show_gps(GPS_INFO *GPS)
{
printf("DATE : %d-%02d-%02d \n",GPS->D.year,GPS->D.month,GPS->D.day);
printf("TIME : %02d:%02d:%02d \n",GPS->D.hour,GPS->D.minute,GPS->D.second);
printf("Latitude : %.4f %c\n",GPS->latitude,GPS->NS);
printf("Longitude : %.4f%c\n",GPS->longitude,GPS->EW);
printf("Highness : %.4f \n",GPS->high);
printf("STATUS : %c\n",GPS->status);
}
////////////////////////////////////////////////////////////////////////////////
//解释gps
//0 7 0 4 6 0 6 8 0 90 0 3 0 9
//$GPRMC,091400,A,3958.9870,N,11620.3278,E,000.0,000.0,120302,005.6,W*62
//$GPGGA,091400,3958.9870,N,11620.3278,E,1,03,1.9,114.2,M,-8.3,M,,*5E
void gps_parse(char *line,GPS_INFO *GPS)
////////////////////////////////////////////////////////////////////////////////
{
int tmp;
char c;
char* buf=line;
c=buf[5];
if(buf[0]!='$')return;
if(c=='C'){//"GPRMC"
GPS->D.hour =(buf[ 7]-'0')*10+(buf[ 8]-'0');
GPS->D.minute =(buf[ 9]-'0')*10+(buf[10]-'0');
GPS->D.second =(buf[11]-'0')*10+(buf[12]-'0');
tmp = GetCharPos(9,buf);
GPS->D.day =(buf[tmp+0]-'0')*10+(buf[tmp+1]-'0');
GPS->D.month =(buf[tmp+2]-'0')*10+(buf[tmp+3]-'0');
GPS->D.year =(buf[tmp+4]-'0')*10+(buf[tmp+5]-'0')+2000;
//------------------------------
GPS->status =buf[GetCharPos(2,buf)];
GPS->latitude =get_double_number(&buf[GetCharPos(3,buf)]);
GPS->NS =buf[GetCharPos(4,buf)];
GPS->longitude=get_double_number(&buf[GetCharPos(5,buf)]);
GPS->EW =buf[GetCharPos(6,buf)];
#ifdef USE_BEIJING_TIMEZONE
UTC2BTC(&GPS->D);
#endif
}
if(c=='A'){ //"$GPGGA"
GPS->high = get_double_number(&buf[GetCharPos(9,buf)]);
}
}
double get_double_number(char *s)
{
char buf[128];
int i;
double rev;
i=GetCharPos(1,s);
strncpy(buf,s,i);
buf[i-1]='\0';
rev=atof(buf);
return rev;
}
////////////////////////////////////////////////////////////////////////////////
//
int GetCharPos(int num,char *str)
{
int i,j=0;
int len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]==',')j++;
if(j==num)return i+1;
}
return 0;
}
//#ifdef USE_BEIJING_TIMEZONE
////////////////////////////////////////////////////////////////////////////////
//
void UTC2BTC(date_time *GPS)
{
//***************************************************
//如果秒号先出,再出时间数据,则将时间数据+1
GPS->second++; //
if(GPS->second>59){
GPS->second=0;
GPS->minute++;
if(GPS->minute>59){
GPS->minute=0;
GPS->hour++;
}
}
//***************************************************
GPS->hour+=8;
if(GPS->hour>23)
{
GPS->hour-=24;
GPS->day+=1;
if(GPS->month==2 ||
GPS->month==4 ||
GPS->month==6 ||
GPS->month==9 ||
GPS->month==11 ){
if(GPS->day>30){
GPS->day=1;
GPS->month++;
}
}
else{
if(GPS->day>31){
GPS->day=1;
GPS->month++;
}
}
if(GPS->year % 4 == 0 ){//
if(GPS->day > 29 && GPS->month ==2){
GPS->day=1;
GPS->month++;
}
}
else{
if(GPS->day>28 &&GPS->month ==2){
GPS->day=1;
GPS->month++;
}
}
if(GPS->month>12){
GPS->month-=12;
GPS->year++;
}
}
}
void ReceiveOneBL( GPS_INFO* GPS_info)
{
int i=0, tmp=0;
char c;
char buf[256];
while (1) {
tmp = read(fd2,&c,1);
buf[i++] = c;
if(c == '\n'){
buf[i]='\0';
i=0;
if( buf[5]=='C'&&buf[0]=='$') break; //完整的"GPRMC"
}
}
gps_parse(buf,GPS_info);
return ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -