我在使用TinyGPS库解析Lat和Lon时遇到了麻烦。这个库是否与RFduino兼容?我可以通过向RFduino加载空白草图然后打开串行监视器来读取NMEA字符串,因此我知道GPS数据正在通过串行端口,但是当我尝试将Lat或Lon变为变量时,它用999999999填充变量。我通过BLE将这些数据发送到android。如果我不尝试获取GPS数据,我可以在lat或lon变量中发送我想要的任何值,它出现在我的自定义Android应用程序中。我在某处读到软件库对rfduino不起作用。这是真的?如果没有,我将能够通过硬串口打印我的数据,使故障排除更容易。下面我附上了我在RFduino上使用的代码。任何建议将不胜感激。
// CODE //
#include <RFduinoBLE.h>
#include <TinyGPS.h>
TinyGPS gps;
long lat = 5; //Load lat/lon with junk value for testing
long lon = 6;
char latBuf[20];
char lonBuf[20];
void setup() {
// this is the data we want to appear in the advertisement
// (if the deviceName and advertisementData are too long to fix into the 31 byte
// ble advertisement packet, then the advertisementData is truncated first down to
// a single byte, then it will truncate the deviceName)
RFduinoBLE.advertisementData = "ledbtn";
// start the BLE stack
RFduinoBLE.begin();
Serial.begin(9600);//For GPS Communication
}
void loop(){
char c = byte(Serial.read());
gps.encode(c);
gps.get_position(&lat,&lon); // get latitude and longitude
// send position as char[]
String latString = String(lat);
String lonString = String(lon);
latString.toCharArray(latBuf, 20);
lonString.toCharArray(lonBuf, 20);
RFduinoBLE.send(lonBuf, 20);
}
void RFduinoBLE_onDisconnect()
{
}
void RFduinoBLE_onReceive(char *data, int len)
{
RFduinoBLE.send(lonBuf, 20);
}
答案 0 :(得分:1)
我看到一个问题:loop()试图在每次循环执行时读出GPS坐标。这种方法有两个问题:1)循环不会等到串行数据就绪,2)循环不等到接收的GPS数据有效。
从阅读http://arduino.cc/en/Tutorial/ReadASCIIString和http://arduiniana.org/libraries/tinygps/我建议将loop()重写为以下内容:
loop() {
char c;
float fLat, fLon;
unsigned long fix_age;
static unsigned long previous_fix_age = 0;
// If nothing to read; do nothing.
// Read as many characters as are available.
while (Serial.available() > 0) {
// Tell the GPS library about the new character.
c = Serial.read();
gps.encode(c);
gps.f_get_position(&flat, &flon, &fix_age);
if (fix_age != TinyGPS::GPS_INVALID_AGE && fix_age != previous_fix_age) {
// new GPS data is valid, new, and ready to be printed
previous_fix_age = fix_age; // remember that we've reported this data.
String latString = String(lat);
...the rest of the code you already have to print the lat and lon.
}
}
}
有关previous_fix_age的代码在那里,只有当从GPS收到新的修复时,循环才会打印坐标。