| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4505 人关注过本帖
标题:求大神帮帮改改代码求经纬度的 GRPMC
只看楼主 加入收藏
Rick_gu
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:37
专家分:108
注 册:2014-5-29
收藏
 问题点数:0 回复次数:0 
求大神帮帮改改代码求经纬度的 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


搜索更多相关主题的帖子: include thread common 经纬度 
2014-06-03 16:45
快速回复:求大神帮帮改改代码求经纬度的 GRPMC
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026133 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved