How to store serial data into a string and the send it

Hi, I am using a gps module with my Arduino Mega 2560. I have the GPS connected to my Serial1, and I am writing a program to received data, verify the checksum and extract the latitude and longitude from the string and store it as a float. I thought the program was working but I soon realized that the reason the checksums match is because they are equal to zero on both sides, meaning that no serial data is being stored into any variables.
I tried doing this:

char data[] = {0};
int a = 0;
void setup() {
  Serial1.begin(9600);
  Serial.begin(9600);
}

void loop() {
  if (Serial1.available()) {
    Serial1.readBytesUntil('*', data, 200);
    do {
      Serial.write(data[a]);
      a++;
    }
    while (data[a] != 0);
  }
}

and I get nothing in the Serial port monitor.
There is definitely no problem with the serial connection, as I get normal data when I run this program:

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

void loop() {
  if (Serial1.available()) {
    Serial.write(Serial1.read());
  }
}

Does anybody have a solution to my problem?
With regards,
Habib

char data[] = {0};

declares an array with one element which is, at best, a waste of time.

One of the examples in serial input basics (probably the second one) should be suitable for your project. They are simple reliable ways to receive data without blocking.

...R

Then, after you define a stupid size array, you lie and tell this function:

    Serial1.readBytesUntil('*', data, 200);

that you've provided room for it to store 200 characters there. Not a snowball's chance in hell of that working.

I suggest that you provide an example of the exact input data that you are typing into the Serial object and what you see coming out versus what you want to see from the serial link, after you correct for the errors pointed out above.

Hi, I what I am trying to do is receive and store NMEA data. This is what is being sent into my Arduino:

$GNRMC,054934.00,A,3955.84845,N,11627.14239,E,0.706,183.80,280815,,,A7B
$GNVTG,183.80,T,,M,0.706,N,1.307,K,A
25
$GNGGA,054934.00,3955.84845,N,11627.14239,E,1,09,1.54,109.0,M,-8.3,M,,5B
$GNGSA,A,3,17,28,03,06,11,24,,,,,,,2.70,1.54,2.22
14
$GNGSA,A,3,86,87,77,,,,,,,,,,2.70,1.54,2.221A
$GPGSV,3,1,12,01,24,054,08,02,05,245,19,03,48,074,29,06,37,249,19
78
$GPGSV,3,2,12,11,14,072,23,17,67,335,41,23,03,111,17,24,09,309,32*7C
$GPGSV,3,3,12,28,59,179,17,32,17,043,,42,38,146,,50,35,140,*7D
$GLGSV,3,1,10,70,23,038,,71,44,096,19,72,22,154,23,76,26,224,6F
$GLGSV,3,2,10,77,36,289,29,78,10,338,,85,12,080,22,86,51,038,36
6C
$GLGSV,3,3,10,87,40,308,42,88,01,280,69
$GNGLL,3955.84845,N,11627.14239,E,054934.00,A,A
79

what I want to do is store the first line ($GNRMC) into a string, and then extract the numbers in the string to different floats. The number after the * is a checksum, which is to be stored in a byte. Then I can calculate the checksum and see if it matches the checksum in the message, and I can use the coordinate data.

the code I am now running is this:

char data[80];
int a = 0;
void setup() {
  Serial1.begin(9600);
  Serial.begin(9600);
}

void loop() {
  if (Serial1.available()) {
    Serial1.readBytesUntil('*', data, 80);
    Serial.println(data);
  }
}

and this is what I'm getting on the serial port monitor:

$GNRMC,055749.00,V,,,,,,,280815,,,N,045,328,146,,50,35,40,25,108,27,24,08,30

6F
$GNVTG,,,,,,,,,N,V,,,,,,,280815,,,N,045,328,146,,50,35,40,25,108,27,24,08,30

2E
$GNGGA,055749.00,,,,,0,08,1.15,,,,,,045,328,146,,50,35,40,25,108,27,24,08,30

4F
$GNGSA,A,1,17,28,0.81,1.15,1.40,,,,,045,328,146,,50,35,40,25,108,27,24,08,30

19
$GNGSA,A,1,86,87,,,,,,,056,,02,08,247,,03,46,069,30,06,40,28,23,05,108,25,24

,08,305,32,A,1,86,87,,,,,,,056,,02,08,247,,03,46,069,30,06,40,28,23,05,108,25,24

7D
$GPGSV,3,3,12,28,54,179,23,32,14,041,,42,38,146,35,140,40,28,23,05,108,25,24

76
,10,77,35,283,20,78,12,335,20,85,09,083,,86,49,045,3540,40,28,23,05,108,25,24

68
$G,88,03,284,20,78,12,335,20,85,09,083,,86,49,045,3540,40,28,23,05,108,25,24

61
$GNGLL,,,,,055749.00,V,N5,20,85,09,083,,86,49,045,3540,40,28,23,05,108,25,24

5E

Thanks.

Hi Robin, thank you very much for the tutorials, they are very helpful, I'm working on modifying the code of the second sketch in the tutorial so there is a start marker as well as an end marker.

arduinohabib:
I'm working on modifying the code of the second sketch in the tutorial so there is a start marker as well as an end marker.

? ? ? ?

The third example uses start- and end-markers ! ! ! !

...R