标签归档:GPS

Linux下串口GPS设备的GPRMC数据处理

#include
#include
#include
#include
#include
#include
#include
#include
#include

struct gps_info
{
	double utc_time;
	char status;
	double latitude_value;
	char latitude;
	double longtitude_value;
	char longtitude;
	double utc_data;
}rmc_info;//GPRMC数据

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;

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

	switch( nEvent )
	{
	case 'O':
		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;
	case 460800:
		cfsetispeed(&newtio, B460800);
		cfsetospeed(&newtio, B460800);
		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[VTIME]  = 0;//
	newtio.c_cc[VMIN] = 100;//
/*
	newtio.c_cc[VTIME] = 30;    //30 seconds
	newtio.c_cc[VMIN] = 100;     //least bytes
	newtio.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);//非规范模式

	newtio.c_cc[VTIME] = 30;    //30 seconds
	newtio.c_cc[VTIME] = 3;    //3 seconds
*/
	tcflush(fd,TCIFLUSH);
	if((tcsetattr(fd,TCSANOW,&newtio))!=0)
	{
		perror("com set error");
		return -1;
	}
	printf("set done!\n\r");
	return 0;
}

int main()
{
	int i;
	int fd_gps;
	int set_gps;
	int nread_gps;
	char tmp;
	char *buff_gps;
	char *rmc_str;
    char buff_gps[512];//Edit by 2013.8.9
    char *tmp=buff_gps];//Edit by 2013.8.9

	fd_gps=open("/dev/ttyUSB0",O_RDWR|O_NONBLOCK);//打开串口
	if (fd_gps==-1)
		exit(1);

	set_gps=set_opt(fd_gps,4800,8,'N',1);//设置串口属性
	if (set_gps==-1)
		exit(1);

while(1)
	{
/*		memset(buff_gps,0,512);
		nread_gps=read(fd_gps,buff_gps,512);//读串口

		printf("%s\n\n\n",buff_gps);
		if(nread_gps>0)
		{
			if((rmc_str = strstr(buff_gps,"$GPRMC"))!=NULL)
				{
					for(i=0;i