C++ Arduino对屏幕的写入速度

C++ Arduino对屏幕的写入速度,c++,arduino,gps,C++,Arduino,Gps,我无法让我的GPS更新速度。它显示文本,如果我将GPS速度从位置更新循环中取出,它会显示当前速度一次,然后更新失败。有什么想法吗 void loop() { while (serial_connections.available()) { gps.encode(serial_connections.read()); if (gps.location.isUpdated()) { DText = Serial.println(gps.speed.mps());

我无法让我的GPS更新速度。它显示文本,如果我将GPS速度从位置更新循环中取出,它会显示当前速度一次,然后更新失败。有什么想法吗

void loop() {
  while (serial_connections.available()) {
    gps.encode(serial_connections.read());
    if (gps.location.isUpdated()) {
      DText = Serial.println(gps.speed.mps());
      DSat = Serial.println(gps.satellites.value());
    }
    display.clearDisplay();  // clears last number
    display.display();  // writes clear to screen
    display.setCursor(10, 5);  //Set drawing posision
    display.print(DText);  // what to draw
    display.setCursor(35, 5);
    display.print(" MPS");
    display.setCursor(10, 18);
    display.print(DSat);
    display.setCursor(35, 18);
    display.print(" Sat");
    display.display(); // writes to the screen
    delay (50);
  }
}
它显示当前速度一次,然后无法更新。有什么想法吗

void loop() {
  while (serial_connections.available()) {
    gps.encode(serial_connections.read());
    if (gps.location.isUpdated()) {
      DText = Serial.println(gps.speed.mps());
      DSat = Serial.println(gps.satellites.value());
    }
    display.clearDisplay();  // clears last number
    display.display();  // writes clear to screen
    display.setCursor(10, 5);  //Set drawing posision
    display.print(DText);  // what to draw
    display.setCursor(35, 5);
    display.print(" MPS");
    display.setCursor(10, 18);
    display.print(DSat);
    display.setCursor(35, 18);
    display.print(" Sat");
    display.display(); // writes to the screen
    delay (50);
  }
}
草图将花费所有时间更新显示并等待。下面是正在发生的事情:

1) 当一个字符可用时,它被读取并传递到
encode

2) 然后更新显示,这需要一些时间。你没有给我们整个程序,也没有确定硬件,所以我真的不能说需要多长时间

3) 然后等待50毫秒。在此期间,GPS字符继续到达。它们将存储在输入缓冲区中,直到调用
read()
,或者直到存储了64个字符。然后,它们将被丢弃

在9600点(我猜),可能有50个字符到达。现在输入缓冲区几乎满了

4)
while
循环测试再次执行,读取并解析第二个字符(步骤1),更新显示(没有新信息可用,步骤2),然后再等待50毫秒

15毫秒后,输入缓冲区已满,Arduino开始忽略字符。50毫秒延迟完成后,35个字符丢失(9600处)

这会阻止成功解析收到的(部分)NMEA句子,并且速度不会得到更新。草图将继续使用旧信息更新显示,然后再等待一段时间,这将导致更多字符丢失

循环结构需要重新设计,以便仅当有新信息可用时才更新显示,并且您不应使用延迟:

#include <LiquidCrystal.h> ???
LiquidCrystal display;     ???

#include <NMEAGPS.h>

NMEAGPS gps;
gps_fix fix;

//  Here are three different ways to connect the GPS:
#define gpsPort Serial1

//#include <AltSoftSerial.h>
//AltSoftSerial gpsPort; // two specific pins required (8 & 9 on an UNO)

//#include <NeoSWSerial.h>
//NeoSWSerial gpsPort( 3, 4 );


void setup()
{
  Serial.begin( 9600 );
  gpsPort.begin( 9600 );
}

void loop()
{
  // Read and parse any available characters from the GPS device
  if (gps.available( gpsPort )) {

    // Once per second, a complete fix structure is ready.
    fix = gps.read();

    Serial.print( F("Speed: ") );
    float speed = 0.0;
    if (fix.valid.speed) {
      speed = fix.speed_kph() * 1000.0 / 3600.0;
      Serial.print( speed );
    }
    Serial.println();

    Serial.print( F("Sats: ") );
    if (fix.valid.satellites)
      Serial.println( fix.satellites );
    Serial.println();

    //  Update the display ONCE PER SECOND

    display.clearDisplay();  // clears last number
    display.display();  // writes clear to screen
    display.setCursor(10, 5);  //Set drawing posision
    if (fix.valid.speed)
      display.print( speed );  // what to draw
    display.setCursor(35, 5);
    display.print(" MPS");
    display.setCursor(10, 18);
    if (fix.valid.satellites)
      display.print( fix.satellites );
    display.setCursor(35, 18);
    display.print(" Sat");
    display.display(); // writes to the screen
  }
}
#包括什么???
液晶显示器???
#包括
NMEAGPS;
全球定位系统;
//以下是连接GPS的三种不同方式:
#定义gpsPort序列1
//#包括图书馆。它比所有其他GPS库更小、更快、更可靠和更精确。即使你不使用它,你也应该阅读和的相关页面


NeoGPS、AltSoftSerial和NeoSWSerial都可以从Arduino IDE库管理器的菜单下获得,菜单为草图->包含库->管理库

请重新格式化代码。至少有一个
}
缺少用于指出这一点的标记。它并不是真的丢失了,它的注释代码少于75行。你解决问题了吗?如果是,请提供一个答案。谢谢你的帖子太棒了。我学到了很多,它最终让我得到了更新GPS和串行监视器的屏幕。加上屏幕是SSD1306,GPS是NEO 6。“走进地铁迷你车里。”威廉·奥科姆,很高兴能帮上忙!因为它是迷你型的,NeoGPS真的可以帮你节省一些宝贵的内存。在双引号打印上使用F宏也会有所帮助。您可能需要查看u8g2库。它还可以节省大量RAM,但需要花费很少的执行时间。和如果这解决了你的问题,请按我答案上的绿色三角形“接受”它。否则,它就在这里徘徊,永远萦绕着我……:-)谢谢