Banner Ausblenden
Kleines Logo

Willkommen auf PSI-Online

GPS-Sensor

GPS-Daten auslesen

 

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

 int ledPin = 13;                  // LED test pin
 int gpsrxPin = 10;                    // RX PIN
 int gpstxPin = 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];
 int firstOne=0;
 int k = 0;
 
 int gpsValues[] = {0,2,4};
 
 SoftwareSerial gps(gpsrxPin, gpstxPin);

 void setup() {
   pinMode(ledPin, OUTPUT);       // Initialize LED pin
   pinMode(gpsrxPin, INPUT);
   pinMode(gpstxPin, OUTPUT);
  
   Serial.begin(38400);
   gps.begin(38400);
  
  
   for (int i=0;i<300;i++){       // Initialize a buffer for received data
     linea[i]=' ';
   }  
 }
 
 
 
 void getGPSData (char gpsData[]){
   if(gps.available() == 63){
      while(gps.available()){
        byteGPS=gps.read();
        linea[conta]=byteGPS;        // If there is serial port data, it is put in the buffer
        conta++;                     
        Serial.write(byteGPS);
      }    
       cont=0;
       bien=0;
       for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
         if (linea[i-1]==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
             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<3;i++){
           k = gpsValues[i];
           switch(k){
             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;
           }
           gpsData[i] = '';
           for (int j=indices[k];j<(indices[k+1]-1);j++){
             Serial.print(linea[j+1]);
             gpsData[i] = gpsData[i] + linea[j+1];
           }
           Serial.println("");
         }
         Serial.println("---------------");
       }else{
         Serial.println("Fehler beim Einlesen");
       }
      
       conta=0;                    // Reset the buffer
       for (int i=0;i<300;i++){    // 
         linea[i]=' ';            
       } 
    }
  
 }

 void loop() {
  
    char gpsData[3];
    getGPSData(gpsData);
    for(int i=0; i<3; i++){
      Serial.write(i);
      Serial.write(":");
      Serial.println(gpsData[i]);
    }
  
 }

Erstellt: Andreas Hecker (08.05.2013) Letzte Änderung: Andreas Hecker (08.05.2013)