#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int fd;
typedef struct GPSINFO{
char utc_time[64];
char status;
float latitude_value;
char latitude;
float longtitude_value;
char longtitude;
float speed;
float azimuth_angle;
char utc_data[64];
}GPSINFO;
int gps_read(char *buf)
{
char buffer[1024]= {0};
char sbuf[128] ={0};
char * gpsbuf;
GPSINFO gpsinfo;
int i = 0;
int timeout = 10;
while(1) {
int count;
count = read(fd, buffer, sizeof(buffer));
printf("%s",buffer);
if(count < 0) {
perror("read");
continue;
}
if('A' == gpsinfo.status) {
sprintf(sbuf, "X%c%fY%c%fT", gpsinfo.latitude, gpsinfo.latitude_value,gpsinfo.longtitude,gpsinfo.longtitude_value);
gpsinfo.utc_time[strlen(gpsinfo.utc_time)] = 'W';
int size = strlen(sbuf);
i = 0;
while(gpsinfo.utc_time[i] != 'W') {
sbuf[size + i] = gpsinfo.utc_time[i];
i++;
}
} else {
continue;
}
break;
}
sbuf[strlen(sbuf)] = 'W';
memcpy(buf, sbuf, sizeof(sbuf));
return 0;
}
int main()
{
struct termios opt;
char buf[128];
int i=0;
for (i=0; i<10; i++) {
sprintf(buf, "/dev/ttyUSB%d", i);printf("USP port:%s\n", buf);
if((fd = open(buf,O_RDWR)) < 0) {
printf("open error\n");
continue;
} else {
printf("open ok\n");
break;
}
}
if (i==10) return -1;
tcgetattr(fd, &opt);
cfsetispeed(&opt, B4800);
cfsetospeed(&opt, B4800);
opt.c_cflag |= (CLOCAL | CREAD);
opt.c_cflag &= ~CRTSCTS;
opt.c_cflag &= ~CSIZE;
opt.c_cflag |= CS8;
opt.c_iflag |= IGNPAR;
opt.c_cflag &= ~CSTOPB;
opt.c_oflag = 0;
opt.c_lflag = 0;
tcsetattr(fd, TCSANOW, &opt);
gps_read(buf);
return 0;
}
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int fd;
typedef struct GPSINFO{
char utc_time[64];
char status;
float latitude_value;
char latitude;
float longtitude_value;
char longtitude;
float speed;
float azimuth_angle;
char utc_data[64];
}GPSINFO;
int gps_read(char *buf)
{
char buffer[1024]= {0};
char sbuf[128] ={0};
char * gpsbuf;
GPSINFO gpsinfo;
int i = 0;
int timeout = 10;
while(1) {
int count;
count = read(fd, buffer, sizeof(buffer));
printf("%s",buffer);
if(count < 0) {
perror("read");
continue;
}
if('A' == gpsinfo.status) {
sprintf(sbuf, "X%c%fY%c%fT", gpsinfo.latitude, gpsinfo.latitude_value,gpsinfo.longtitude,gpsinfo.longtitude_value);
gpsinfo.utc_time[strlen(gpsinfo.utc_time)] = 'W';
int size = strlen(sbuf);
i = 0;
while(gpsinfo.utc_time[i] != 'W') {
sbuf[size + i] = gpsinfo.utc_time[i];
i++;
}
} else {
continue;
}
break;
}
sbuf[strlen(sbuf)] = 'W';
memcpy(buf, sbuf, sizeof(sbuf));
return 0;
}
int main()
{
struct termios opt;
char buf[128];
int i=0;
for (i=0; i<10; i++) {
sprintf(buf, "/dev/ttyUSB%d", i);printf("USP port:%s\n", buf);
if((fd = open(buf,O_RDWR)) < 0) {
printf("open error\n");
continue;
} else {
printf("open ok\n");
break;
}
}
if (i==10) return -1;
tcgetattr(fd, &opt);
cfsetispeed(&opt, B4800);
cfsetospeed(&opt, B4800);
opt.c_cflag |= (CLOCAL | CREAD);
opt.c_cflag &= ~CRTSCTS;
opt.c_cflag &= ~CSIZE;
opt.c_cflag |= CS8;
opt.c_iflag |= IGNPAR;
opt.c_cflag &= ~CSTOPB;
opt.c_oflag = 0;
opt.c_lflag = 0;
tcsetattr(fd, TCSANOW, &opt);
gps_read(buf);
return 0;
}
二.硬件连接:
三.内核配置:
四.插上GPS模块(USB接口USBHOST1)
终端显示如下:
五.执行测试程序: