热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

GPSNMEA字符串的解析代码-ParsingcodeforGPSNMEAstring

iamtryingtoparsetheincomingGPGGANMEAGPSstringusingArduinounoandbelowcode.Whatiam

enter image description herei 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.

我正在尝试使用Arduino uno和下面的代码来解析即将到来的GPGGA NMEA GPS字符串。我要做的是,我只使用GPGGA NMEA字符串来获取纬度、经度和海拔的值。在我下面的代码中,我做了一些检查,检查输入的字符串是否为GPGGA,然后将进一步的字符串存储在一个数组中,使用strtok函数可以对数组进行进一步解析,并且可以很容易地找到所有的3个GPS坐标。

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.

但是我不知道如何只存储GPGGA字符串而不存储进一步的字符串。我使用for循环,但它不起作用。

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

我不想使用任何库。我遇到过一些现有的代码。

Here is the GPGGA string information link

这是GPGGA字符串信息链接

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)

后我试图functionlity i)检查传入的字符串是否GPGGA ii)如果是,然后存储以下字符串到终点或到*(其次是校验和数组)在一个数组,数组的长度是可变的(我无法找出解决方案)iii),那么解析存储阵列(这样做是,我试着用不同的数组)

 #include 

    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);
    }

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

编辑1:考虑Joachim Pileborg,在代码中编辑for循环。

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

我添加了一个pic来显示代码的未定义输出。

Input for 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

7 个解决方案

#1


3  

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

在快速检查了关于NMEA 0183协议的相关文章后,我突然想到:

ends the message.

<如果> 结束消息。

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.

这意味着,您应该查找序列,而不是从串行端口不加选择地读取。如果找到,应该终止字符串,并跳出循环。

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).

此外,您可能需要首先对数据字符串进行零初始化,以便很容易地看到其中是否有要打印的数据(例如,使用strlen)。

#2


0  

Offering this as a suggestion in support of what you are doing...

提出这个建议来支持你正在做的事情……

Would it not be useful to replace all of the nested if()s in your loop with something like:

如果在循环中替换所有嵌套的if()会不会有用:

EDIT added global string to copy myString into once captured

编辑添加的全局字符串将myString复制到一次捕获。

char globalString[100];//declare a global sufficiently large to hold you results


void loop() 
{
    int chars = mySerial.available();
    int i;
    char *myString;
    if (chars>0)
    {
        myString = calloc(chars+1, sizeof(char));
        for(i=0;i

Now, the return value from mySerial.available() tells you exactly how many bytes to read, you can read the entire buffer, and test for validity all in one.

现在,mySerial.available()的返回值告诉您要读取多少字节,您可以读取整个缓冲区,并在其中测试有效性。

#3


0  

Just add these code it will work. use pin 8 and 9 as tx and rx

只要添加这些代码就可以了。使用引脚8和9作为tx和rx

Main.cgps.hgps.c

Main.cgps.hgps.c

#4


0  

Just add these code it will work. use pin 8 and 9 as tx and rx

只要添加这些代码就可以了。使用引脚8和9作为tx和rx

  1. gps.h
  2. gps.h
  3. main.c
  4. c
  5. gps.c
  6. gps.c

#5


0  

I have a project that will need to pull the same information out of the same sentence. I got this out of a log file

我有一个项目需要从同一个句子中提取相同的信息。我是从一个日志文件中得到的

import serial
import time
ser = serial.Serial(1)

ser.read(1)
read_val = ("nothing")

gpsfile="gpscord.dat"
l=0

megabuffer=''
def buffThis(s):
        global megabuffer
        megabuffer +=s
def buffLines():
        global megabuffer
        megalist=megabuffer.splitlines()
        megabuffer=megalist.pop()
        return megalist

def readcom():
        ser.write("ati")
        time.sleep(3)
        read_val = ser.read(size=500)
        lines=read_val.split('\n')
        for l in lines:
                if l.startswith("$GPGGA"):
                        if l[:len(l)-3].endswith("*"):
                                outfile=open('gps.dat','w')
                                outfile.write(l.rstrip())
                                outfile.close()

readcom()

while 1==1:
    readcom()

answer=raw_input('not looping , CTRL+C to abort')

The result is this: gps.dat

结果是:gps.dat。

$GPGGA,225714.656,5021.0474,N,00412.4420,W,0,00,50.0,0.0,M,18.0,M,0.0,0000*5B

#6


0  

Using "malloc" every single time you read a string is an enormous amount of computational overhead. (And didn't see the corresponding free() function call. Without that, you never get that memory back until program termination or system runs out of memory.) Just pick the size of the longest string you will ever need, add 10 to it, and declare that your string array size. Set once and done.

每次读取一个字符串时使用“malloc”都是巨大的计算开销。(并且没有看到相应的free()函数调用。如果没有这个功能,你永远也得不到内存,直到程序终止或系统耗尽内存。只需选择您需要的最长字符串的大小,向它添加10,并声明您的字符串数组大小。设置一次,完成。

There are several C functions for getting substrings out of a string, strtok() using the coma is probably the least overhead.

有几个从字符串中获取子字符串的C函数,使用彗发的strtok()可能是开销最小的。

You are on an embedded microcontroller. Keep it small, keep overhead down. :)

你在一个嵌入式微控制器上。保持小点,保持头顶向下。:)

#7


0  

You could use some functions from the C library libnmea. Theres functions to split a sentence into values by comma and then parse them.

您可以使用C库libnmea中的一些函数。有一些函数可以用逗号将句子分割成值,然后解析它们。


推荐阅读
author-avatar
野蛮生长
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有