GPS模块:SIRF3
GSM模块:华为GTM900B
开发环境:LINUX
本篇博客的程序是在LINUX环境下把GPS模块接收到的卫星数据读取出来,并提取经纬度,利用gsm模块发送给服务器作为定位数据。
gps模块头文件gps.h
/*************************************************************************
> File Name: gps.h
> Author: kid
> Mail: 280197326@qq.com
> Created Time: 2014年03月27日 星期四 10时35分12秒
************************************************************************/
#ifndef __GPS_H__
#define __GPS_H__
#include <stdio.h>//标准io定义
#include <fcntl.h>//文件控制定义
#include <unistd.h>//unix标准函数定义
#include <stdlib.h>//标准函数库定义
#include <termios.h>//PPSIX终端控制定义
#include <sys/types.h>
#include <sys/time.h>//kill函数需要用到的头文件
#include <signal.h>//kill函数需要用到的头文件
#include <errno.h>//错误号定义
#include <string.h>//字符串处理头文件
int gps_open(char *device);//打开gps设备节点
int gps_close(void);//关闭gps设备节点
void gps_read(void);//读取gps模块
int gps_set_serial(int fd, int speed, int bits, char event, int stop);//设置gps模块串口参数
char *gps_data_analysis(void);//gps数据分析
#endif
gps模块函数实现gps.c
/*************************************************************************
> File Name: gps.c
> Author: kid
> Mail: 280197326@qq.com
> Created Time: 2014年03月27日 星期四 09时27分23秒
************************************************************************/
#include "gps.h"
#define uint unsigned int
#define SIZE 1024
int gps_fd;//gps设备文件描述符
char* buf;//存放gps数据
int gps_open(char *device)//打开gps设备节点
{
gps_fd = open (device, O_RDONLY);
if( gps_fd==-1 ){
perror("open gps error\n");
return 1;
}else{
buf = malloc(SIZE);//分配buf内存空间
if(buf == NULL){
close(gps_fd);
return 1;
}
printf("open gps successfully\n");
return 0;
}
}
int gps_close(void)//关闭gps设备节点
{
int i = -1;
free(buf);
i = close(gps_fd);
if( i!=0 ){
perror("close gps fail\n");
return 1;
}else if( i==0 ){
printf("close gps successfully\n");
return 0;
}
}
void gps_read(void)//读取gps数据
{
int i = -1;
int j = -1;
fd_set readfd;
struct timeval timeout;
FD_ZERO(&readfd);//初始化readfd
FD_SET(gps_fd, &readfd);//把gps_fd加入readfd
timeout.tv_sec = 3;//设置3秒超时
timeout.tv_usec = 0;
j = select(gps_fd+1, &readfd, NULL, NULL, &timeout);//用select对gps_fd进行轮循
if( j>0 ){
if( FD_ISSET(gps_fd, &readfd) ){//如果gps_fd可读
i = read(gps_fd, buf, SIZE);
buf[i] = '\0';
}
}
sleep(1);//等待1s,接受更多数据
}
int gps_set_serial(int fd, int speed, int bit, char event, int stop)//设置串口参数
{
int i = -1;
int j = -1;
struct termios serial;//串口类
uint speed_in;//输入波特率
uint speed_out;//输出波特率
static uint speed_array[] = {0,50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200,230400};//如果不加static会出现重定义错误
i = tcgetattr(fd, &serial);//用tcgetattr函数获取串口终端参数
if( i!=0 ){
perror("get gps serial data error\n");
}else if( i==0 ){
printf("get gps serial data successfully\n");
}
memset(&serial, 0, sizeof(struct termios));
serial.c_cflag |= CLOCAL | CREAD;//设置数据接收器使能和本地模式
serial.c_cflag &= ~CSIZE;//通过位掩码的方式激活
switch( bit )//设置字符大小
{
case 8 : serial.c_cflag |= CS8;
break;
case 7 : serial.c_cflag |= CS7;
break;
}
switch( speed )//设置波特率
{
case 2400 : cfsetispeed(&serial, B2400);
cfsetospeed(&serial, B2400);
break;
case 4800 : cfsetispeed(&serial, B4800);
cfsetospeed(&serial, B4800);
break;
case 9600 : cfsetispeed(&serial, B9600);
cfsetospeed(&serial, B9600);
break;
case 115200 : cfsetispeed(&serial, B115200);
cfsetospeed(&serial, B115200);
break;
}
switch( event )//设置奇偶校验位
{
case 'O' : //奇数
serial.c_cflag |= PARENB;
serial.c_cflag |= PARODD;
serial.c_iflag |= (INPCK | ISTRIP);
break;
case 'E' : //偶数
serial.c_iflag |= (INPCK | ISTRIP);
serial.c_cflag |= PARENB;
serial.c_cflag &= ~PARODD;
break;
case 'N' : //无奇偶校验位
serial.c_cflag &= ~PARENB;