Hey everyone,
I'm trying to send some GPS coordinates to my Uno board using the serial port. I begin by sending a start command by sending . Then, each coordinate I send is also enclosed in <> and the latitude and longitude are separated by a comma with no space. After all the coordinates have been received, I send a stop command by sending . So, I need to parse the coordinates out. I want to save the coordinates into the EEPROM, but I haven't implemented that because I am trying to figure out why my board keeps rebooting.
Here is my code:
void loop() {
while(Serial.available() > 0) {
inChar = Serial.read();
if(inChar == '<') {
memset(inData, 0, sizeof(inData));
readSerial = true;
charNum = 0;
}
if(readSerial) {
inData[charNum] = inChar;
charNum++;
}
if(inChar == '>') {
charNum++;
inData[charNum] = '\0';
readSerial = false;
break;
}
if(strcmp(inData, "<stop>") == 0) {
readInCoordinates = false;
}
if(strcmp(inData, "<start>") == 0) {
Serial.println("read in coordinates");
readInCoordinates = true;
}
}
void parseCoordinates() {
int parseNum = 1;
int latNum = 0;
int lonNum = 0;
char lat[20];
char lon[20];
boolean parseLat = true;
if(strcmp(inData, "<start>") != 0) {
while(inData[parseNum] != '>' && strcmp(inData, "<start>") != 0 && strcmp(inData, "<stop>") !=0) {
if(inData[parseNum] == ',') {
parseLat = false;
parseNum++;
lat[latNum] = '\0';
}
if(parseLat) {
lat[latNum] = inData[parseNum];
latNum++;
}
else {
lon[lonNum] = inData[parseNum];
lonNum++;
}
lon[lonNum+1] = '\0';
parseNum++;
}
Serial.print("lat: ");
Serial.println(lat);
Serial.print("lon: ");
Serial.println(lon);
}
}
Thanks everyone!