Arduino GPS无法读取-如何排除故障?

时间:2019-04-28 13:28:16

标签: c arduino gps

我正在使用Elecrow A9 board with GSM and GPS,并试图写一个草图,使我可以通过SMS对其进行控制。我的第一步是让它从GPS读取位置并将其打印为串行。草图可以编译,上载和运行,但不会进入while (gps.available( A9board )) {循环。

有什么我想念的吗?

这是我的素描的相关部分:

#include <NMEAGPS.h>

#define A9board Serial1  // Best:  Serial1 on a Mega, Leo or Due
//      or
//#include <AltSoftSerial.h>
//AltSoftSerial A9board; Next best, but you must use pins 8 & 9!  This is very efficient.
//      or
//#include <NeoSWSerial.h>
//NeoSWSerial A9thinker( 10, 11 ); // 2nd best on whatever two pins you want.  Almost as efficient.

NMEAGPS gps;
gps_fix fix;

//  Finite-State Machine declarations
enum state_t
  {
    WAITING,           // until time to check again
    GPS_ON_WAIT,       // after AT+GPS=1
    GPS_READING,       // after AT+GPSRD=1
    GPS_READING_WAIT,  // after location received, AT+GPSRD=0
    GPS_OFF_WAIT,      // after AT+GPS=0
    SENDING_SMS
    // other states?
  };
state_t  state = WAITING; // start here
uint32_t stateTime      ; // used for timeouts, instead of delay

const uint32_t CHECK_LOCATION_TIME = 5000; // ms, how often to check

void echoA9chars()
{
  if (A9board.available())
    Serial.write( A9board.read() ); // just echo what we are receiving
}

void ignoreA9chars()
{
  if (A9board.available())
    A9board.read(); // ignore what we are receiving
}

void setup()
{
  Serial.begin( 115200 );
  A9board.begin( 9600 );
  stateTime = millis();
}

void loop()
{
  switch (state) {

    case WAITING:
      //Serial.println("WAITING"); 
      echoA9chars();

      if (millis() - stateTime >= CHECK_LOCATION_TIME) {

        // Turn GPS data on
        A9board.println( F("AT+GPS=1") );

        stateTime = millis();
        state     = GPS_ON_WAIT;
      }
      break;

    case GPS_ON_WAIT:
      //Serial.println("GPS_ON_WAIT"); 
      echoA9chars();

      // Wait for the GPS to turn on and acquire a fix
      if (millis() - stateTime >= 5000) { // 5 seconds

        //  Request GPS data
        A9board.println( F("AT+GPSRD=1") );
        Serial.print( F("Waiting for GPS fix...") );

        stateTime = millis();
        state     = GPS_READING;
      }
      break;

    case GPS_READING:
      Serial.println("GPS_READING"); 
      while (gps.available( A9board )) { // parse the NMEA data
        Serial.println("gps available");
        fix = gps.read(); // this structure now contains all the GPS fields

        Serial.print(fix.status); 

        if (fix.status) {
            Serial.println("Fix status");  
            Serial.print(fix.status);  
        }

        if (fix.valid.location) {
          Serial.println();
          // Now that we have a fix, turn GPS data off
          A9board.println(  F("AT+GPSRD=0") );

          stateTime = millis();
          state     = GPS_READING_WAIT;
        }
      }

      if (millis() - stateTime > 1000) {
        Serial.print( '.' ); // still waiting for fix, print a dot.
        stateTime = millis();
      }
      break;

0 个答案:

没有答案