I'm trying to connect two GPS modules (Adafruit Ultimate GPS) to an Arduino Uno. The modules use serial communication. The Uno has only one hardware serial port, which needs to be left open for programming and talking to the serial monitor, so I'm trying to use two software serial ports. After some reading around, it seems like you cannot listen to two soft serials at simultaneously, but you can use the .listen() function to listen to one at a time, which is fine for my application. The issue is the .listen() function listens until it catches the '?' character, and there is no '?' character at the end of the GPS messages. There is, however, a '\n' character at the end, so I could use that if I could figure out how to.
That being said, I'm open to solutions other than using the .listen() function for two soft serial ports.
My current code is below, it sorta works, as in I can get complete messages sometimes, but I get a lot of partial messages too.
Let me know if I can provide any missing info and I appreciate any suggestions.
PS. I know the Mega would make short work of this, but at the moment I don't have one and can't get a hold of one quickly.
#include <SoftwareSerial.h>
SoftwareSerial GPS1(3,2);
SoftwareSerial GPS2(8,7);
bool x = 0;
byte counter;
void setup() {
while (!Serial); // wait for Serial to be ready
Serial.begin(115200);
Serial.println("Starting");
GPS1.begin(57600);
GPS2.begin(57600);
setupGPS();
GPS1.listen();
}
void loop() {
char data1[100] = {0};
while(!GPS1.available());
while (GPS1.available()) {
if(!x) {
//Serial.print("GPS 1 ");
counter = 0;
x = 1;
}
data1[counter] = GPS1.read();
counter++;
}
if(x){
if(data1[counter-1] == '\n') {
Serial.println("GPS 1 Good String");
}
else {
Serial.println("GPS 1 Invalid String");
}
x = 0;
GPS1.stopListening();
GPS2.listen();
}
//***************************************************************
char data2[100] = {0};
while(!GPS2.available());
while (GPS2.available()) {
if(!x) {
counter = 0;
x = 1;
}
data2[counter] = GPS2.read();
counter++;
}
if(x){
if(data2[counter-1] == '\n') {
Serial.println("GPS 2 Good String");
}
else {
Serial.println("GPS 2 Invalid String");
}
x = 0;
GPS2.stopListening();
GPS1.listen();
}
}
void setupGPS() {
GPS1.println("$PMTK251,57600*2C");//set baud rate of gps
GPS2.println("$PMTK251,57600*2C");
//GPS1.println("$PMTK251,9600*17");
//GPS2.println("$PMTK251,9600*17");
//GPS1.flush();
//GPS2.flush();
//GPS1.begin(57600);
//GPS2.begin(57600);
//Serial.println("Configuring GPS Dataflow");
GPS1.println("$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29");//set data output of gps
GPS2.println("$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29");
//GPS1.println("$PMTK220,100*2F");//10hz
//GPS2.println("$PMTK220,100*2F");//10hz
//GPS1.println("$PMTK220,200*2C");//5hz
//GPS2.println("$PMTK220,200*2C");//5hz
GPS1.println("$PMTK220,1000*1F");//1hz//set data rate of gps
GPS2.println("$PMTK220,1000*1F");//1hz
}