【问题标题】:Trying to extract longitude and latitude from gps using arduino and neo 6m module but the loop goes up to infinity尝试使用arduino和neo 6m模块从gps中提取经度和纬度,但循环上升到无穷大
【发布时间】:2023-03-12 12:30:01
【问题描述】:

我是 arduino 的新手,并尝试使用 arduino 的 neo 6m 模块提取 gps 坐标,但循环一直运行到无穷大。你能帮我解释一下为什么它没有坏吗?

void gpsEvent()
{
    gpsString = "";
    while (1)
    {
        while (gps.available() > 0)  //Serial incoming data from GPS
        {
            char inChar = (char)gps.read();
            gpsString += inChar;//store incoming data from GPS to temparary string str[]
            i++;
            // Serial.print(inChar);
            if (i < 7)                      
            {
                if (gpsString[i-1] != test[i-1])    //check for right string
                {
                    i = 0;
                    gpsString = "";
                }
            }

            if (inChar == '\r')
            {
                if (i > 60)
                {
                    gps_status = 1;
                    break;
                }
                else
                {
                    i = 0;
                }
            }
        }

        if (gps_status)
            break;
     }
}

void get_gps()
{
    gps_status = 0;
    int x = 0;
    while (gps_status == 0)
    {
        gpsEvent();
        int str_lenth = i;
        coordinate2dec();
        i = 0;
        x = 0;
        str_lenth = 0;
    }
}

我在 void setup() 循环中调用了get_gps(); 来初始化系统,但是用于从数据中提取正确字符串的 gpsEvent 函数正在运行直到无限,请您帮忙。代码参考来自https://circuitdigest.com/microcontroller-projects/arduino-based-accident-alert-system-using-gps-gsm-accelerometer 但我自己做了一些改动,但在 gps 模块的编程中没有。

【问题讨论】:

    标签: arduino gps iot android-gps


    【解决方案1】:

    我认为其中一个错误是gpsString += inChar;

    这不是 Python。您正在将字符的值添加到字符串指针。

    您应该创建一个最大长度的缓冲区,插入一个字符并检查缓冲区溢出。

    另外i 似乎没有定义。在 C 中,像你一样使用全局变量是非常糟糕的做法。在函数中保留一个i。再次检查字符串长度。

    一般来说,您似乎在使用一种您对编写简单程序还不够了解的语言(字符串操作是 C 的基础)。要么学习更好的 C 语言,要么寻找 gps 库的 python 实现(或只是链接)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多