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

查看:27
本文介绍了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) 检查传入的字符串是否为 GPGGAii) 如果是,则将以下字符串存储到 EOL 或 upto *(后跟数组的校验和)中,数组长度是可变的(我无法找到解决方案)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:

结束消息.

这意味着,您应该寻找该序列,而不是从串行端口随意读取.如果找到,您应该终止该字符串,并跳出循环.

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天全站免登陆