| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2300 人关注过本帖
标题:初学者: gps:GPRMC 经纬度问题
只看楼主 加入收藏
Rick_gu
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:37
专家分:108
注 册:2014-5-29
收藏
 问题点数:0 回复次数:3 
初学者: gps:GPRMC 经纬度问题
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 =((float)(buffer[n+1])
                            +(float)(buffer[n+2])   
                            +(float)(buffer[n+3])
                            +(float)(buffer[n+4])
                            +(float)(buffer[n+6])
                            +(float)(buffer[n+7])
                            +(float)(buffer[n+8])
                            +(float)(buffer[n+9]));
                                                   
                    }   
                }   
                if(m == 4)   
                {   
                    "N";   
                }   
                if(m == 5)
                {   
                    
                    lon =((float)(buffer[n+1])  
                        +(float)(buffer[n+2])   
                        +(float)(buffer[n+3])  
                        +(float)(buffer[n+4])
                        +(float)(buffer[n+5])
                        +(float)(buffer[n+7])
                        +(float)(buffer[n+8])
                        +(float)(buffer[n+9])
                        +(float)(buffer[n+10]));
                    
                      }   
                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;
}



main:

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";
   
    gps_parse_line(&gps, buffer, strlen(buffer));
    gps_get(&gps, &lon, &lat);
    printf("lon: %f, lat: %f\n", lon, lat);
    getchar();


为什么:      纬度:lat 开始值3202.1855     到 gps->lat=lat;  
              经度:lon开始值 11849.0769    到 gps->lon=lon;
lat值变为410.000000;
lon值变为477.000000;
debug时  gps->lat显示         : gps->lat    -1.0737418e+008    float



[ 本帖最后由 Rick_gu 于 2014-5-29 15:40 编辑 ]
搜索更多相关主题的帖子: 经纬度 
2014-05-29 14:47
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
连 struct gps_t 都没有将定义贴出来,你就不能贴出别人能直接编译调试的代码吗?

程序代码:
#include <stdio.h>
#include <string.h>

struct gps_t
{
    double lon;
    double lat;
};

int gps_parse_line( struct gps_t *gps, const char *buffer )
{
    if( 2 != sscanf(buffer,"$GPRMC,%*f,A,%lf,N,%lf",&gps->lat,&gps->lon) )
        return 1; // 失败
    return 0; // 成功
}

int main(int argc, char *argv[])
{
    struct gps_t gps;
    const char *buffer = "$GPRMC,013946.00,A,3202.1855,N,11849.0769,E,0.05,218.30,111105,4.5,W,A*20";
    if( 0 == gps_parse_line(&gps,buffer) )
        printf( "lon: %.4f, lat: %.4f\n", gps.lon, gps.lat );

    return 0;
}

输出:
lon: 11849.0769, lat: 3202.1855
2014-05-30 12:08
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
将 #include <string.h> 那一句删除掉,多余的
2014-05-30 12:09
wfoo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:7
帖 子:120
专家分:134
注 册:2011-8-6
收藏
得分:0 
很明显把字符串转成浮点数那个的发那个有问题
                    lon =((float)(buffer[n+1])  
                        +(float)(buffer[n+2])   
                        +(float)(buffer[n+3])  
                        +(float)(buffer[n+4])
                        +(float)(buffer[n+5])
                        +(float)(buffer[n+7])
                        +(float)(buffer[n+8])
                        +(float)(buffer[n+9])
                        +(float)(buffer[n+10]))
这只是简单的把ASCII码相加。
2014-06-02 11:28
快速回复:初学者: gps:GPRMC 经纬度问题
数据加载中...
 
   



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

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