概述
#include/*标准输入输出定义*/
#include/*标准函数库定义*/
#include/*Unix 标准函数定义*/
#include
#include
#include
#include
#include
#include
//#define dev "/dev/ttyS0"
#define BUFF_SIZE 512
#define MAX_COM_NUM 3
intSectionID=0,i=0;
struct data
{
char GPS_time[20]; //UTC时间
char GPS_sv; //使用卫星
char GPS_wd[12]; //纬度
char GPS_jd[12]; //经度
//char GPS_warn; //定位警告
char GPS_speed[5]; //速度
char GPS_date[8]; //UTC日期
}GPS_DATA;
int set_com_config(int fd,int baud_rate,int data_bits,char parity,int stop_bits)
{
struct termios new_cfg,old_cfg;
int speed;
//保存并测试现有串口参数设置,在这里如果串口号出错,会有相关的出错信息
if(tcgetattr(fd,&old_cfg)!=0)
{
perror("tcgetattr");
return -1;
}
tcflush(fd, TCIOFLUSH);
new_cfg=old_cfg;
cfmakeraw(&new_cfg);//配置为原始模式
new_cfg.c_cflag&=~CSIZE;
//设置波特率
switch(baud_rate)
{
case 2400:
{
speed=B2400;
break;
}
case 4800:
{
speed=B4800;
break;
}
case 9600:
{
speed=B9600;
break;
}
case 19200:
{
speed=B19200;
break;
}
case 38400:
{
speed=B38400;
break;
}
case 115200:
{
speed=B115200;
break;
}
}
cfsetispeed(&new_cfg,speed);
cfsetospeed(&new_cfg,speed);
//设置数据位
switch(data_bits)
{
case 7:
{
new_cfg.c_cflag|=CS7;
break;
}
case 8:
{
new_cfg.c_cflag|=CS8;
break;
}
}
//设置停止位
switch(stop_bits)
{
case 1:
{
new_cfg.c_cflag &=~CSTOPB;
break;
}
case 2:
{
new_cfg.c_cflag |=CSTOPB;
break;
}
}
//设置奇偶校验位
switch(parity)
{
case 'o':
case 'O':
{
new_cfg.c_cflag|=(PARODD|PARENB);
new_cfg.c_iflag|=(INPCK |ISTRIP);
break;
}
case 'e':
case 'E':
{
new_cfg.c_cflag |=PARENB;
new_cfg.c_cflag &=~PARODD;
new_cfg.c_iflag |=(INPCK | ISTRIP);
break;
}
case 's':
case 'S':
{
new_cfg.c_cflag &=~PARENB;
new_cfg.c_cflag &=~CSTOPB;
break;
}
case 'n':
case 'N':
{
new_cfg.c_cflag &=~PARENB;
new_cfg.c_iflag &=~INPCK;
break;
}
}
new_cfg.c_cc[VTIME] =10;
new_cfg.c_cc[VMIN] =5;
//处理未接收字符
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&new_cfg))!=0)
{
perror("tcsetattr");
return -1;
}
return 0;
}
//打开串口函数
int open_port(int com_port)
{
int fd;
#if (COM_TYPE== GNR_COM) //使用普通串口
char* dev[] = {"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
#else//使用USB转串口
char* dev[] = {"/dev/ttyUSB0","/dev/ttyUSB1","/dev/ttyUSB2"};
#endif
if((com_port<0)||(com_port>MAX_COM_NUM))
{
return -1;
}
//打开串口
if((fd=open(dev[com_port-1],O_RDWR|O_NOCTTY|O_NDELAY))<0)
{
perror("open serial port");
return -1;
}
//恢复串口为堵塞状态
if(fcntl(fd,F_SETFL,0)<0)
{
perror("fcntl F_SETFLn");
return -1;
}
//测试是否为终端设备
if(isatty(STDIN_FILENO)==0)
{
perror("standard input is not a terminal device");
}
return fd;
}
void print_info(void)
{
//打印选择界面,即引导的字符号
printf("Now the receive time is %sn",GPS_DATA.GPS_time);
printf("The star is %c 3n",GPS_DATA.GPS_sv);
printf("The earth latitude is :%sn",GPS_DATA.GPS_wd);
printf("The earth longitude is :%sn",GPS_DATA.GPS_jd);
printf("The train speed is:%s km/hn",GPS_DATA.GPS_speed);
printf("The date is:%sn",GPS_DATA.GPS_date);
}
void GPS_resolve_GPRMC(char data)
{
//$GPRMC,092427.604,V,4002.1531,N,11618.3097,E,0.000,0.00,280812,,E,N*08
if(data==',')
{
++SectionID;
i=0;
}
else
{
switch(SectionID)
{
case 1://02:48:13
GPS_DATA.GPS_time[i++]=data;
if(i==2 ||i==5)
{
GPS_DATA.GPS_time[i++]=':';
}
GPS_DATA.GPS_time[8]='