Hello everybody,

I have a Teensy 2, and i want to be able to log the NMEA message from a EM406 GPS

I have take the sketch from http://www.ladyada.net/make/gpsshield/download.html
and i want to use with HardwareSerial Uart for the serial btw gps and teensy

The compilation seem to be good, the teensy run correctly (16 or 8mhz/sdcard), create the log file fine ... but log only ONE sequence in the file, or sometimes, nothing.

If you have any suggestion ...

Code:
#include <SD.h>
#include "GPSconfig.h"

const int buttonPin = 17;     // Declare PIN 17 for USB mass-storage/charging control
int buttonState = 0;          // State button int

// what to log
#define LOG_RMC 1 // RMC-Recommended Minimum Specific GNSS Data, message 103,04

HardwareSerial Uart = HardwareSerial(); // Pin7 Rx Pin8 Tx

// Set the pins used 
#define led1Pin 15 // Green LED GPS valid position
#define led2Pin 14 // Red LED Writing data on SDcard
#define pin16 16 // USB and charging mode control
#define chipSelect 0

#define BUFFSIZE 90
char buffer[BUFFSIZE];
uint8_t bufferidx = 0;
bool fix = false;
bool gotGPRMC;
uint8_t i;
File logfile;

// read a Hex value and return the decimal equivalent
uint8_t parseHex(char c) {
  if (c < '0')
    return 0;
  if (c <= '9')
    return c - '0';
  if (c < 'A')
    return 0;
  if (c <= 'F')
    return (c - 'A')+10;
}

// blink out an error code
void error(uint8_t errno) {
/*
  if (SD.errorCode()) {
    putstring("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.print(',');
    Serial.println(card.errorData(), HEX);
  }
  */
  while(1) {
    for (i=0; i<errno; i++) {
      digitalWrite(led1Pin, HIGH);
      delay(100);
      digitalWrite(led2Pin, HIGH);
      delay(100);
      digitalWrite(led1Pin, LOW);
      delay(100);
      digitalWrite(led2Pin, LOW);
      delay(100);
    }
    for (; i<10; i++) {
      delay(200);
    }
  }  
}

void setup() {
  pinMode(buttonPin, INPUT_PULLUP); // Set input button pin & pull-up on analog pin 17
  //Serial.begin(9600);
  //Serial.println("\r\nGPSlogger");
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
 
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(0, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    //Serial.println("Card init. failed!");
    error(1);
  }
  strcpy(buffer, "GPSLOG00.LOG");
  for (i = 0; i < 100; i++) {
    buffer[6] = '0' + i/10;
    buffer[7] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(buffer)) {
      break;
    }
  }
  logfile = SD.open(buffer, FILE_WRITE);
  if( ! logfile ) {
    //Serial.print("Couldnt create "); Serial.println(buffer);
    error(3);
  }
  //Serial.print("Writing to "); Serial.println(buffer); 
  //Serial.println("Ready!");
  
  Uart.begin(4800);
  delay(250);

#if (LOG_RMC == 1)
    Serial.print(RMC_ON);
#else
    Serial.print(RMC_OFF);
#endif
  delay(250);
}

void loop() {
    char c;
    uint8_t sum;    

    buttonState = digitalRead(buttonPin); // Read button state
    if (buttonState == LOW) {
    digitalWrite(pin16, HIGH); // Enabling charging mode to LTC4054
    Serial.begin(9600);  // Start serial.begin, open SDCARD to get log
    delay(20000);
    } 
    else {
    Serial.end();  // Normal mode, disable USB for power saving (4ma off)
    }
  
  // read one 'line'
  if (Uart.available()) {
    c = Uart.read();
#if ARDUINO >= 100
    //Serial.write(c);
#else
    //Serial.print(c, BYTE);
#endif
    if (bufferidx == 0) {
      while (c != '$')
        c = Uart.read(); // wait till we get a $
    }
    buffer[bufferidx] = c;

#if ARDUINO >= 100
    //Serial.write(c);
#else
    //Serial.print(c, BYTE);
#endif
    if (c == '\n') {
      //putstring_nl("EOL");
      //Serial.print(buffer);
      buffer[bufferidx+1] = 0; // terminate it

      if (buffer[bufferidx-4] != '*') {
        // no checksum?
        Serial.print('*');
        bufferidx = 0;
        return;
      }
      // get checksum
      sum = parseHex(buffer[bufferidx-3]) * 16;
      sum += parseHex(buffer[bufferidx-2]);

      // check checksum
      for (i=1; i < (bufferidx-4); i++) {
        sum ^= buffer[i];
      }
      if (sum != 0) {
        //putstring_nl("Cxsum mismatch");
        //Serial.print('~');
        bufferidx = 0;
        return;
      }
      // got good data!
      gotGPRMC = strstr(buffer, "GPRMC");
      if (strstr(buffer, "GPRMC")) {
        // find out if we got a fix
        char *p = buffer;
        p = strchr(p, ',')+1;
        p = strchr(p, ',')+1;       // skip to 3rd item

        if (p[0] == 'V') {
          digitalWrite(led1Pin, LOW);
          delay(100);
          fix = false;
        } else {
          digitalWrite(led1Pin, HIGH);
          delay(100);
          digitalWrite(led1Pin, LOW);    // set the LED off
          delay(100);              // wait for a second
          fix = true;
        }
      }
      // rad. lets log it!
      //Serial.print(buffer);
      //Serial.print('#');
      
      if (gotGPRMC)      //If we have a GPRMC string
      {
        // Bill Greiman - need to write bufferidx + 1 bytes to getCR/LF
        bufferidx++;
        
      digitalWrite(led2Pin, HIGH);      // sets the digital pin as output
      delay(100);
            // Bill Greiman - need to write bufferidx + 1 bytes to getCR/LF
    
      logfile.write((uint8_t *) buffer, bufferidx);
      logfile.flush();
      /*
      if( != bufferidx) {
         putstring_nl("can't write!");
         error(4);
      }
      */
      digitalWrite(led2Pin, LOW);
      bufferidx = 0;
      }
       return;
      }
    }
    bufferidx++;
    if (bufferidx == BUFFSIZE-1) {
       Serial.print('!');
       bufferidx = 0;
    }
   }
/* End code */