求大神帮帮改改代码求经纬度的 GRPMC
头文件:#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <common/threads.h>
#include "serial.h"
struct gps_t
{
int fd;
unsigned char buffer[8192];
int size;
struct thread_t thread;
float lon, lat;
};
int gps_open(struct gps_t *gps, char *dev, int baud_rate);
int gps_read_line(struct gps_t *gps, char *buffer, int size);
int gps_parse_line(struct gps_t *gps, char *buffer, int size);
int gps_get(struct gps_t *gps, float *lon, float *lat);
void gps_close(struct gps_t *gps);
#include <string.h>
#include "gps.h"
static void *recieve_thread(void *arg)
{
struct gps_t *gps = (struct gps_t *)arg;
char buffer[2048];
int len = 0;
while(1)
{
len = gps_read_line(gps, buffer, sizeof(buffer));
if(len > 0)
{
buffer[len] = '\0';
gps_parse_line(gps, buffer, len);
}
}
return NULL;
}
int gps_open(struct gps_t *gps, char *dev, int baud_rate)
{
memset(gps, 0, sizeof(struct gps_t));
gps->fd = serial_open(dev, baud_rate);
thread_init(&gps->thread);
thread_create(&gps->thread, recieve_thread, gps);
return gps->fd;
}
int gps_read_line(struct gps_t *gps, char *buffer, int size)
{
char data[2048];
int i, j, len = 0;
while(1)
{
for(i = 0; i < gps->size; i++)
{
if(gps->buffer[i] == '\r' || gps->buffer[i] == '\n')
{
len = i + 1;
if(i > 0)
{
memcpy(buffer, gps->buffer, i);
}
for(j = 0; j < gps->size - len; j++)
{
gps->buffer[j] = gps->buffer[j + len];
}
gps->size -= len;
return i;
}
}
len = serial_read(gps->fd, data, sizeof(data));
if(len > 0)
{
memcpy(gps->buffer + gps->size, data, len);
gps->size += len;
}
}
return 0;
}
int gps_parse_line(struct gps_t *gps, char *buffer, int size)
{
float lon, lat;
char gps_status;
char n=0,m=0,f=0;
if((buffer[1] == 'G')
&&(buffer[2] == 'P')
&&(buffer[3] == 'R')
&&(buffer[4] == 'M')
&&(buffer[5] == 'C'))//GPRMC
{
for(n=0;n<100;n++)
{
if(buffer[n] == ',')
{
m ++;
if(m == 2)
{
gps_status = buffer[n+1];
}
if(m == 3)
{
if(gps_status == 'A')
{
lat=??
}
}
if(m == 4)
{
"N";
}
if(m == 5)
{
lon=??
}
if(m == 6)
{
"E";
}
}
}
}
gps->lat=lat;
gps->lon=lon;
return 0; // 成功
}
int gps_get(struct gps_t *gps, float *lon, float *lat)
{
*lon=gps->lon;
*lat=gps->lat;
return 0;
}
void gps_close(struct gps_t *gps)
{
}
main:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "gps.h"
int main(int argc, char *argv[])
{
float lon, lat;
struct gps_t gps;
char *buffer = "$GPRMC,013946.00,A,3202.1855,N,11849.0769,E,0.05,218.30,111105,4.5,W,A*20";
//char *buffer = "$GPRMC,013946.00,A,32°02'11,N,118°49'4,E,0.05,218.30,111105,4.5,W,A*20";
gps_parse_line(&gps, buffer, strlen(buffer));
gps_get(&gps, &lon, &lat);
printf("lon: %f, lat: %f\n", lon, lat);
getchar();
return 0;
}
主要就是 ,3202.1855,N,11849.0769,E 经纬度 输出这样是这样 纬度 lat 32°02'1266
经度 lon 118°49'1284