GPS NMEA字符串的解析代码 [英] Parsing code for GPS NMEA string

查看:528
本文介绍了GPS NMEA字符串的解析代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Arduino uno和以下代码解析传入的GPGGA NMEA GPS字符串. 我想做的是仅使用GPGGA NMEA字符串获取纬度,经度和海拔高度的值.在下面的代码中,我进行了某些检查以检查传入的字符串是否为GPGGA,然后将其存储可以使用strtok函数进一步解析数组中的字符串,并且可以轻松找出所有3个GPS坐标.

i am trying to parse the incoming GPGGA NMEA GPS string using Arduino uno and below code. What i am trying to do is that i am using only GPGGA NMEA string to get the values of Latitude, longitude and altitude.In my below code, i had put certain checks to check if incoming string is GPGGA or not, and then store the further string in a array which can be further parsed suing strtok function and all the 3 GPS coordinates can be easily find out.

但是我无法弄清楚如何只存储GPGGA字符串,而不能存储其他字符串.我正在使用for循环,但是它不起作用.

But i am unable to figure out how to store only GPGGA string and not the further string.I am using a for loop but it isn't working.

我不尝试使用任何库.我遇到了某些类似这样的现有代码.

I am not trying to use any library.I had came across certain existing codes like this.

这是GPGGA字符串信息链接

我正在尝试具有以下功能 i)检查输入的字符串是否为GPGGA ii)如果是,则将以下字符串存储到数组中,直到EOL或直到*(后跟数组的校验和),数组长度是可变的(我无法找到解决方案) iii)然后解析存储的数组(完成后,我尝试了其他数组)

i am trying to have following functionlity i) Check if incoming string is GPGGA ii) If yes, then store the following string upto EOL or upto * (followed by checksum for the array) in a array, array length is variable(i am unable to find out solution for this) iii) Then parse the stored array(this is done, i tried this with a different array)

 #include <SoftwareSerial.h>

    SoftwareSerial mySerial(10,11);  // 10 RX / 11 TX

    void setup()
    {
    Serial.begin(9600);
    mySerial.begin(9600);
    }

    void loop()
    {
    uint8_t x;
    char gpsdata[65];

    if((mySerial.available()))
    {
    char c = mySerial.read();
    if(c == '$')
      {char c1 = mySerial.read();
       if(c1 == 'G')
         {char c2 = mySerial.read();
          if(c2 == 'P')
            {char c3 = mySerial.read();
             if(c3 == 'G')
               {char c4 = mySerial.read();
                if(c4 == 'G')
                   {char c5 = mySerial.read();
                    if(c5 == 'A')
                       {for(x=0;x<65;x++)
                        { 
                        gpsdata[x]=mySerial.read();


    while (gpsdata[x] == '\r' || gpsdata[x] == '\n')
                    {
                    break;
                    }

                        }

                       }
                       else{
                          Serial.println("Not a GPGGA string");
                        }
                   }
               }

            }     

         }

      }

    }

    Serial.println(gpsdata);
    }

考虑到Joachim Pileborg,请在代码中编辑for循环.

Edit 1: Considering Joachim Pileborg, editing the for loop in the code.

我正在添加图片以显示代码的未定义输出.

I am adding a pic to show the undefined output of the code.

输入代码:

$GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76
$GPGSA,A,3,10,07,05,02,29,04,08,13,,,,,1.72,1.03,1.38*0A
$GPGSV,3,1,11,10,63,137,17,07,61,098,15,05,59,290,20,08,54,157,30*70
$GPGSV,3,2,11,02,39,223,19,13,28,070,17,26,23,252,,04,14,186,14*79
$GPGSV,3,3,11,29,09,301,24,16,09,020,,36,,,*76
$GPRMC,092750.000,A,5321.6802,N,00630.3372,W,0.02,31.66,280511,,,A*43
$GPGGA,092751.000,5321.6802,N,00630.3371,W,1,8,1.03,61.7,M,55.3,M,,*75
$GPGSA,A,3,10,07,05,02,29,04,08,13,,,,,1.72,1.03,1.38*0A
$GPGSV,3,1,11,10,63,137,17,07,61,098,15,05,59,290,20,08,54,157,30*70
$GPGSV,3,2,11,02,39,223,16,13,28,070,17,26,23,252,,04,14,186,15*77
$GPGSV,3,3,11,29,09,301,24,16,09,020,,36,,,*76
$GPRMC,092751.000,A,5321.6802,N,00630.3371,W,0.06,31.66,280511,,,A*45

推荐答案

在快速检查了有关NMEA 0183协议的链接文章后,这跳到我了:

After a quick check of the linked article on the NMEA 0183 protocol, this jumped out at me:

<CR><LF>结束消息.

这意味着,您应该在寻找该序列,而不仅仅是从串行端口中随意读取.如果找到,则应终止该字符串,然后退出循环.

This means, that instead of just read indiscriminately from the serial port, you should be looking for that sequence. If found, you should terminate the string, and break out of the loop.

此外,您可能想对数据字符串进行零初始化,以轻松查看其中是否确实有任何数据要打印(例如使用strlen).

Also, you might want to zero-initialize the data string to begin with, to easily see if there actually is any data in it to print (using e.g. strlen).

这篇关于GPS NMEA字符串的解析代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆