Obtaining information from a GPS using NMEA, with only one Serial Port

Hello,

I’m working on obtaining information from my GPS, but I’m currently using the Sparkfun Redboard (an Arduino Uno equivalent) and I’m having difficulty obtaining the data from the GPS, because the board only has one serial port, which is being used to communicate between the board and my computer. I’m trying to use the SoftwareSerial function to use the digital pins 10 and 11 for RX and TX, but nonetheless my code gets stuck when attempting to verify that a serial port is available. Any insight on how to fix the problem would be very welcome, thanks!

 #include <string.h>
 #include <ctype.h>
 #include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
 int ledPin = 13;                  // LED test pin
 int rxPin = 10;                    // RX PIN 
 int txPin = 11;                    // TX TX
 int byteGPS=-1;
 char linea[300] = "";
 char comandoGPR[7] = "$GPRMC";
 int cont=0;
 int bien=0;
 int conta=0;
 int indices[13];
 void setup() {
   pinMode(ledPin, OUTPUT);       // Initialize LED pin
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
   Serial.begin(4800);
   for (int i=0;i<300;i++){       // Initialize a buffer for received data
     linea[i]=' ';
   }   
 }
 void loop() {
   digitalWrite(ledPin, HIGH);
   byteGPS=Serial.read();         // Read a byte of the serial port
   if (byteGPS == -1) {           // See if the port is empty yet
     delay(100); 
   } else {
     // note: there is a potential buffer overflow here!
     linea[conta]=byteGPS;        // If there is serial port data, it is put in the buffer
     conta++;                      
     Serial.write(byteGPS); 
     if (byteGPS==13){            // If the received byte is = to 13, end of transmission
       // note: the actual end of transmission is <CR><LF> (i.e. 0x13 0x10)
       digitalWrite(ledPin, LOW); 
       cont=0;
       bien=0;
       // The following for loop starts at 1, because this code is clowny and the first byte is the <LF> (0x10) from the previous transmission.
       for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
         if (linea[i]==comandoGPR[i-1]){
           bien++;
         }
       }
       if(bien==6){               // If yes, continue and process the data
         for (int i=0;i<300;i++){
           if (linea[i]==','){    // check for the position of the  "," separator
             // note: again, there is a potential buffer overflow here!
             indices[cont]=i;
             cont++;
           }
           if (linea[i]=='*'){    // ... and the "*"
             indices[12]=i;
             cont++;
           }
         }
         Serial.println("");      // ... and write to the serial port
         Serial.println("");
         Serial.println("---------------");
         for (int i=0;i<12;i++){
           switch(i){
             case 0 :Serial.print("Time in UTC (HhMmSs): ");break;
             case 1 :Serial.print("Status (A=OK,V=KO): ");break;
             case 2 :Serial.print("Latitude: ");break;
             case 3 :Serial.print("Direction (N/S): ");break;
             case 4 :Serial.print("Longitude: ");break;
             case 5 :Serial.print("Direction (E/W): ");break;
             case 6 :Serial.print("Velocity in knots: ");break;
             case 7 :Serial.print("Heading in degrees: ");break;
             case 8 :Serial.print("Date UTC (DdMmAa): ");break;
             case 9 :Serial.print("Magnetic degrees: ");break;
             case 10 :Serial.print("(E/W): ");break;
             case 11 :Serial.print("Mode: ");break;
             case 12 :Serial.print("Checksum: ");break;
           }
           for (int j=indices[i];j<(indices[i+1]-1);j++){
             Serial.print(linea[j+1]); 
           }
           Serial.println("");
         }
         Serial.println("---------------");
       }
       conta=0;                    // Reset the buffer
       for (int i=0;i<300;i++){    //  
         linea[i]=' ';             
       }                 
     }
   }
 }

but nonetheless my code gets stuck when attempting to verify that a serial port is available.

Perhaps you could point out the part of the code that does that. I don't see anything like that being done.

You could use an FTDI cable, connected to the software serial pins, to communicate with the PC while using the hardware serial pins to communicate with the GPS.

Or, you could flash an LED whenever you got data from the GPS, to verify that you are getting data, when using the hardware serial pins. It wouldn't tell you what the data was, but at least you'd know that you were getting data.

Or, you could use an LCD display...

Lots of ways to skin this cat.

PaulS: Perhaps you could point out the part of the code that does that. I don't see anything like that being done.

In void loop(),

byteGPS=Serial.read(); // Read a byte of the serial port if (byteGPS == -1) { // See if the port is empty yet delay(100);

is where it gets stuck, because the serial port is being used by the USB. We're planning on using a USB wifi cable to connect the Arduino with the computer, as there are many more sensors that need to connect with the Arduino, so I don't think FTDI or LCD display would work for me...

I guess, basically my question is, when I use SoftwareSerial for pins 10 and 11, are they treated as Serial pins throughout the rest of my code, or do I have to make more modifications to my code so that they're treated as Serial?

srivathsa92: In void loop(),

byteGPS=Serial.read(); // Read a byte of the serial port if (byteGPS == -1) { // See if the port is empty yet delay(100);

is where it gets stuck, because the serial port is being used by the USB. We're planning on using a USB wifi cable to connect the Arduino with the computer, as there are many more sensors that need to connect with the Arduino, so I don't think FTDI or LCD display would work for me...

I guess, basically my question is, when I use SoftwareSerial for pins 10 and 11, are they treated as Serial pins throughout the rest of my code, or do I have to make more modifications to my code so that they're treated as Serial?

Serial.read() will hang until a byte is available in the read buffer. change to

if(!Serial.available()) delay(100);

also since you have defined SoftwareSerial to us pins,10,11 you cannot use SPI for any of your sensor.

When ever you are transmitting with SoftwareSerial, nothing else is done until the transmit is complete If the Hardware serial is receiving data it may be lost, Software Serial turns off interrupts, this can cause the loss of reception data by the hardware serial.

Chuck.

Is the serial output of the GPS TTL or RS232?