试图获取我的GPS的位置,但序列号收到0.00000; 0.00000

问题描述 投票:0回答:1

enter image description here

Arduino代码:

#include <SoftwareSerial.h>
#include <TinyGPS.h>
//long   lat,lon; // create variable for latitude and longitude object
float lat,lon ; // create variable for latitude and longitude object
SoftwareSerial gpsSerial(3,4);//rx,tx

TinyGPS gps; // create gps object
void setup(){
Serial.begin(9600); // connect serial
Serial.println("The GPS Received Signal:");
gpsSerial.begin(9600); // connect gps sensor

}

void loop(){


String latitude = String(lat,6);
String longitude = String(lon,6);
Serial.println(latitude+";"+longitude);
delay(1000);

}

我正在尝试获取GPS的位置,但是序列号收到0.00000; 0.00000,我在做什么错?

arduino gps arduino-uno
1个回答
0
投票

您有一个大问题,您永远不会将数据从GPS对象获取到变量中。执行如下:

// create variable for latitude and longitude object
 double lat = 0; // The lib defines it as double!
 double lon = 0; // The lib defines it as double!
unsigned long lastGpsCheck = 0;
const unsigned long delayTime = 1000;
....

void loop(){
// Replaces the CPU stopping delay, does the same without blocking
 if(millis() - lastGpsCheck > delayTime) {
   lat = gps.location.lat(); // This is missing in your code
   lon = gps.location.lon(); // This is missing in your code

   Serial.println( lat,6 );
   Serial.print(";");
   Serial.print(lon,6 );
   lastGpsCkeck = millis();
  }
}

注意:我替换了延迟,尽早学习,不要在循环,子例程或库中使用延迟。在安装程序中可以等待硬件初始化或作为临时调试帮助。避免转换为String类。始终使用fix char数组。字符串类具有错误的内存mgmt并破坏了您的堆(内存泄漏->崩溃),修复了char数组被编译为闪存的情况。

© www.soinside.com 2019 - 2024. All rights reserved.