Well, I'm still flying blind here, because I still don't see your entire code. What I do see, though, is that unterminated strings were the main cause of your problems. You ask "Where was the rest of my string? Well, have a look at the two sizeof() functions in the first code you posted. The sizeof() function will scan the string, looking for a NULL, and if there isn't one it will get the wrong answer.
BTW, my $GPGGA sentences are 81 characters long.
When I grab NMEA data, I look for a '$', then a '\r'. These characters do not need to be in the string, so I don't put them in there. I use the '$' to set a bool indicating I am collecting data, and to set my index to 0, and the '\n' to set a variable telling the rest of my program that I have a complete sentence.
Depending on what I am doing, while I am collecting the sentence, I will either NULL terminate the string after incrementing the index, or NULL terminate it when I get the '\r'. It's easy enough to just use:
response[serial_index]=incoming;
serial_index++; //STORE in response. Its a normal char.
response[serial_index] = '\0';
You are wondering about the NULL termination, and when it happens. You can think of it this way.
An array of char is just an array.
A string (not to be confused with a String) is an array of char with a terminating NULL.
Initializing a char array with individual characters will not NULL terminate what you are thinking is a string. When you initialize with a string constant, like "I'm NULL terminated", the srray of char will be NULL terminated, since a string constant is NULL terminated.
If you put characters into an array, they do not generate a NULL right after them, and it's a good thing, too, because you are dealing with characters in that case. Various functions or marcos will NULL terminate the string, strcpy(), strcat(), itoa(), to name just a few.
I tried a few things out and found that it worked with "readStringUntil('\n'). I guess I could have used "strcpy" to make the returned string into a char array but instead I looked at the source for readStringUntil (I hope that is allowed) and then added it to my program (timedRead was listed as private) so I added that too.
You have to delete that code right away! It's so private you shouldn't even read it!
Just messin' with ya. 8)
Private means that you can't access that function from outside the library. It will be accessed by other functions within the library. What's allowed is basically whatever works.
Now, the reason we'd like to see your entire code... We don't know how you've declared variables, and we don't know what the rest of your code is doing, and how often you call get_nmea_feed(). Since your function is a blocking one, the rest of your code can't run while you are collecting a sentence. Depending on when, exactly, you call the function, you could well be right in the middle of a sentence in the serial buffer.
I would gather my data in loop(). You can still make a separate function you call to gether the sentence, but you should not make it a blocking function. Try something like this (untested):
void loop() {
get_NMEA()
}
byte indx;
bool getting = false;
bool got = false;
void get_NMEA() {
if gps_serial.available {
char c = gps_serial.read();
if (c == '
Note that you call it every time through loop(). In loop() you should test got and deal with the sentence. When you've finished with that data, set got = false, and your next sentence will be gathered.
PaulS, they actually end with a CR, which comes right after the checksum, which is right after the ''. But you can, of course, ignore the checksum and use the '' to indicate the end.) {
getting = true;
return;
}
if (getting && !got) {
if (c == '\r') {
got = true;
getting = false;
return;
}
if (c == 'n' || c == '?') {
return;
}
response[indx] = c;
indx++;
response[indx] = '\0';
}
}
}
Note that you call it every time through loop(). In loop() you should test **got** and deal with the sentence. When you've finished with that data, set **got = false**, and your next sentence will be gathered.
**PaulS**, they actually end with a CR, which comes right after the checksum, which is right after the '*'. But you can, of course, ignore the checksum and use the '*' to indicate the end.