0

I am trying to get the incoming serial data in Arduino from a GPS. I suppose to get the following data:

$GPRMC,073522.000,A,2607.5285,N,12739.9649,E,0.43,263.10,131217,,,A*6C

However, it is not stable. Following is the code. Appreciate your help.

#include <SoftwareSerial.h>
SoftwareSerial gps(2, 3); // RX, TX
int N = 70; // size of array
char incomingSerialData[70]; //32
int incomingSerialDataIndex = 0;

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

void loop() {
  while(gps.available()) {
    delay(10);
    incomingSerialData[incomingSerialDataIndex] = gps.read();
    incomingSerialDataIndex++;
    if (incomingSerialDataIndex > N-1) incomingSerialDataIndex = 0;
  }
  Serial.println(incomingSerialData);
}
  • 1
    have you done any research about storing values in an array? – jsotola Dec 14 '17 at 7:07
  • Yes, I did plenty ways. However, all same unstable. – zalt Dec 14 '17 at 7:13
  • 3
    unstable does not describe the observed results in any meaningful way. in other words, it does not mean anything other than it does not work – jsotola Dec 14 '17 at 7:25
  • 2
    delay(10) is a very bad idea. See Reading Serial on the Arduino. – Edgar Bonet Dec 14 '17 at 9:41
  • 3
    Most sane people use one of the many NEMA parsing libraries to do the work for them... – Majenko Dec 14 '17 at 10:41
1

NMEA sentences cannot be longer than 80 visible characters + terminators. Even with a CRLF, that's a max of 82 characters. You should be fine with a fixed-sized char array of 82.

  • The size of array is set to be 70 since that sentence has 70 characters. – zalt Dec 14 '17 at 7:15
  • And that's a mistake since NMEA sentences can be longer than 70. – dda Dec 14 '17 at 7:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.