首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RFduino不从GPS中拉动NMEA字符串

RFduino不从GPS中拉动NMEA字符串
EN

Stack Overflow用户
提问于 2014-12-29 21:38:00
回答 1查看 693关注 0票数 1

我在使用TinyGPS库解析Lat和Lon时遇到了困难。这个库与RFduino兼容吗?我可以通过将空白草图加载到RFduino,然后打开串行监视器来读取NMEA字符串,因此我知道GPS数据正在通过串行端口,但是当我试图将Lat或Lon放入变量时,它用999999999填充变量。我将这些数据通过BLE发送给一个android。如果我不尝试获取GPS数据,我可以在lat或lon变量中发送我想要的任何值,并且它出现在我的定制Android应用程序中。我在某个地方读到软系列图书馆对rfduino不起作用。这是真的吗?如果没有,我将能够通过硬串口打印我的数据,从而使故障排除变得更加容易。下面我附加了我在RFduino上使用的代码。如有任何建议,将不胜感激。

代码语言:javascript
复制
    //       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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-12 00:07:21

我看到了一个问题:循环()试图读取每个时间循环执行的GPS坐标。这种方法有两个问题: 1)循环不会等到串行数据准备好,2)循环不会等到接收到的GPS数据有效。

从阅读http://arduino.cc/en/Tutorial/ReadASCIIStringhttp://arduiniana.org/libraries/tinygps/开始,我建议重写循环()如下所示:

代码语言:javascript
复制
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接收到新的补丁时才打印坐标。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27696434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档