GPS模块实验

一、GPS简介

全球定位系统(Global Positioning System,GPS)是一种以空中卫星为基础的高精度无线电导航的定位系统,它在全球任何地方以及近
空间都能够提供准确的地理位置、车行速度及精确的时间信息。GPS主要由三大组成部分:空间部分、地面监控部分和用户设备部分
GPS系统具有高精度、全天候、用广泛等特点。
太空卫星部分由多颗卫星组成,分成多个轨道,绕行地球一周约12小时。每个卫星均持续发射载有卫星轨道数据及时间的无线电波,
供地球上的各种接收机来应用。
地面管制部分,这是为了追踪及控制太空卫星运行所设置的地面管制站,主要工作为负责修正与维护每个卫星能够正常运转的各项参
数据,以确保每个卫星都能够提供正确的讯息给使用者接收机来接收使用者接收机(即用户设备),追踪所有的GPS卫星,并实时的计算出接收机所在位置的坐标、移动速度及时间。我们日常接触到的用户设备部分,这里使用到的GPS模块即为用户设备接收机部分。

GPS模块硬件

GPS模块与外部控制器的通讯接口有多种方式,这里我们使用串口进行通讯,波特率为9600bps,1bit停止位,无校验位,无流控,默认每秒输出一次标准格式数据。
GPS模块外观如下图所示,通过排线与控制器进行供电和通讯。该模块为集成模块,没有相关原理图。

GPS模块数据格式

GPS使用多种标准数据格式,目前最通用的GNSS格式是NMEA0183格式。NMEA0183是最终定位格式,即将二进制定位格式转为统
标准定位格式,与卫星类型无关。这是一套定义接收机输出的标准信息,有几种不同的格式,每种都是独立相关的ASCII格式,逗点
开数据流,数据流长度从30-100字符不等,通常以每秒间隔持续输出。
NVMEA0183格式主要针对民用定位导航,与专业RTCM2.3/3.0和CMR+的GNSS数据格式不同。通过NMEA0183格式,可以实现GNSS
收机与PC或PDA之间的数据交换,可以通过USB和COM口等通用数据接口进行数据传输,其兼容性高,数据传输稳定。这里我们使用
口进行是串口通讯,通信框图如下图所示。

我们使用串口接收数据,收到的数据包含:$ GPGGA(GPS定位数据)、$ GPGLL(地理定位信息)、$GPGSA(当前卫星信息)
$ GPGSV(可见卫星状态信息)、$ GPRMC(推荐最小定位信息)、$ GPVTG(地面速度信息)。
这里我们只分析$GPGGA (Global Positioning System Fix Data)即可,它包含了GPS定位经纬度、质量因子、HDOP、高程、参考站号
字段。其标准格式如下:
$GPGGA,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,M,<10>,M,<11>,<12>*hh < CR > < LF >

$XXGGA语句各字段的含义和取值范围各字段的含义和取值范围见下表所示,XX取值有:

  • GPGGA:单GPS
  • BDGGA:单北斗
  • GLGGA:单GLONASS
  • GNGGA:多星联合定位
字段 含义 取值范围
<1> UTC时间hhmmss.ss 000000.00~235959.99
<2> 纬度,格式:ddmm.mmmm 000.00000~8959.9999
<3> 南北半球 N北纬 S南纬
<4> 经度格式dddmm.mmmm 00000.0000~17959.9999
<5> 东西半球 E表示东经 W表示西经
<6> GPS状态 0=未定位 1=GPS单点定位固定解 2=差分定位 3=PPS解 4=RTK固定解 5=RTK点解 6=估计值 7=手工输入模式 8=模拟模式
<7> 应用解算位置的卫星数 00~12
<8> HDOP 水平图形强度因子 0.500~99.000(大于6不可用)
<9> 海拔高度 -9999.9~99999.9
<10> 地球椭球面相对大地水准面的高度 (高程异常) -9999.9~99999.9
<11> 差分时间 从最近一次接收到差分信号开始的秒数,如果不是差分定位将为空
<12> 参考站号 0000~1023;不使用DGPS时为空

例子:$GPGGA,074529.82,2429.6717,N,11804.6973,E,1,8,1.098,42.110,,,M,,*76

二、代码:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>

int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
    
    
	struct termios newtio, oldtio;
	
	if(tcgetattr(fd, &oldtio) != 0) {
    
    
		perror("SetupSerial 1");
		return -1;
	}

	bzero(&newtio, sizeof(newtio));
	newtio.c_cflag |= CLOCAL|CREAD;
	newtio.c_cflag &= ~CSIZE;

	newtio.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
	newtio.c_oflag &= ~OPOST;

	switch(nBits) {
    
    
	case 7:
		newtio.c_cflag |= CS7;
		break;
	case 8:
		newtio.c_cflag |= CS8;
		break;
	}

	switch(nEvent) {
    
    
	case 0:
		newtio.c_cflag |= PARENB;
		newtio.c_cflag |= PARODD;
		newtio.c_iflag |= (INPCK | ISTRIP);
		break;
	case 'E':
		newtio.c_iflag |= (INPCK|ISTRIP);
		newtio.c_cflag |= PARENB;
		newtio.c_cflag &= ~PARODD;
		break;
	case 'N':
		newtio.c_cflag &= ~PARENB;
		break;
	}

	switch(nSpeed) {
    
    
	case 2400:
		cfsetispeed(&newtio, B2400);
		cfsetospeed(&newtio, B2400);
		break;
	case 4800:
		cfsetispeed(&newtio, B4800);
		cfsetospeed(&newtio, B4800);
		break;
	case 9600:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	case 115200:
		cfsetispeed(&newtio, B115200);
		cfsetospeed(&newtio, B115200);
		break;
	default:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	}


	if(nStop == 1)
		newtio.c_cflag &= ~CSTOPB;
	else if(nStop == 2)
		newtio.c_cflag |= CSTOPB;

	newtio.c_cc[VMIN] = 1;
	newtio.c_cc[VTIME] = 0;

	tcflush(fd, TCIFLUSH);
	if((tcsetattr(fd, TCSANOW, &newtio)) != 0) {
    
    
		perror("com set error");
		return -1;
	}

	return 0;
}

int open_port(char *com)
{
    
    
	int fd;
	fd = open(com, O_RDWR|O_NOCTTY);
	if(-1 == fd) {
    
    
		return -1;
	}

	if(fcntl(fd, F_SETFL, 0) < 0) {
    
    
		printf("fcntl failed\n");
		return -1;
	}
	return fd;
}

int read_gps_raw_data(int fd, char *buf)
{
    
    
	int i = 0;
	int iRet;
	char c;
	int start = 0;

	while(1) {
    
    
		iRet = read(fd, &c, 1);
		if(iRet == 1) {
    
    
			if(c=='$') {
    
    
				start = 1;
			}
			if(start) {
    
    
				buf[i++] = c;
			}
			if(c == '\n' || c == 'r') {
    
    
				start = 0;
				return 0;
			}
		} else {
    
    
			return -1;
		}
	}
}

/* eg. $GPGGA,074529.82,2429.6717,N,11804.6973,E,1,8,1.098,42.110,,,M,,*76 <CR><LF> */
int parse_gps_raw_data(char *buf, char *time, char *lat, char *ns, char *lng, char *ew)
{
    
    
	char tmp[10];

	if(buf[0] != '$') {
    
    
		return -1;
	} else if(strncmp(buf+3, "GGA", 3) != 0) {
    
    
		return -1;
	} else if(strstr(buf, ",,,,,")) {
    
    
		printf("Place the GPS to open area\n");
		return -1;
	} else {
    
    
		scanf(buf, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]",tmp,time,lat,ns,lng,ew);
		return 0;
	}
}

int main(int argc, char *argv[])
{
    
    
	int fd;
	int iRet;
	char c;
	char buf[1000];
	char time[100], lat[100], ns[100], lng[100], ew[100];

	float fLat, fLng;

	if(argc != 2) {
    
    
		printf("Usage: \n");
		printf("%s </dev/ttySAC1 or other>\n", argv[0]);
		return -1;
	}

	fd = open_port(argv[1]);
	if(fd < 0) {
    
    
		printf("open %s err!\n", argv[1]);
		return -1;
	}

	iRet = set_opt(fd, 115200, 8, 'N', 1);
	if(iRet) {
    
    
		printf("set port err!\n");
		return -1;
	}

	printf("Enter a char: ");
	while(1) {
    
    
		/* read line */
		iRet = read_gps_raw_data(fd, buf);
		/* parse line */
		if(iRet == 0) {
    
    
			iRet = parse_gps_raw_data(buf,time,lat,ns,lng,ew);
		}
		/* printf */
		if(iRet == 0) {
    
    
			printf("Time :%s\n", time);
			printf("ns:%s\n", ns);
			printf("ew:%s\n", ew);
			printf("lat:%s\n", lat);
			printf("lng:%s\n", lng);
			/* lat ddmm.mmmm */
			scanf(lat+2, "%f", &fLat);
			fLat = fLat / 60;
			fLat += (lat[0]-'0')*10 + (lat[1]-'0');
			/* lng dddmm.mmmm */
			scanf(lng+3, "%f", &fLng);
			fLng = fLng / 60;
			fLng += (lng[0]-'0')*100 +(lng[1]-'0')*10+(lng[2]-'0');
			printf("Lng,Lat: %06f, %06f\n", fLng, fLat);
		}
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/ch122633/article/details/129596942
GPS