Does SerialPort library work with the Atmel Studio VisualMicro plug in?

I am not sure this is the right place to ask this question. I am asking it here because fat16lib is the apparent author of the SerialPort Library that I am using. I am attempting to run the dataLogger example that comes with the library and had some stuff working but the Serial.txt file that is generated is empty. I was attempting to use the VisualMicro debugger to look at some things and it gives me the following errors. I am pretty new to Arduino and have limited programming experience so I am not exactly sure what its telling me. It looks like 2 things are defining something.

Compiling 'SerialLoggerWithMega' for 'Arduino Mega 2560 or Mega ADK'
core.a(HardwareSerial.cpp.o)*:In function `__vector_25'
HardwareSerial.cpp:multiple definition of `__vector_25'
SerialPort.cpp.o:C:\Program Files\arduino-1.0.5\libraries\SerialPort\SerialPort.cpp:214: first defined here
ld.exe:Disabling relaxation: it will not work with multiple definitions
core.a(HardwareSerial.cpp.o)*:In function `__vector_36'
HardwareSerial.cpp:multiple definition of `__vector_36'
SerialPort.cpp.o:C:\Program Files\arduino-1.0.5\libraries\SerialPort\SerialPort.cpp:231: first defined here
core.a(HardwareSerial.cpp.o)*:In function `__vector_51'
HardwareSerial.cpp:multiple definition of `__vector_51'
SerialPort.cpp.o:C:\Program Files\arduino-1.0.5\libraries\SerialPort\SerialPort.cpp:237: first defined here
core.a(HardwareSerial.cpp.o)*:In function `__vector_54'
HardwareSerial.cpp:multiple definition of `__vector_54'
SerialPort.cpp.o:C:\Program Files\arduino-1.0.5\libraries\SerialPort\SerialPort.cpp:243: first defined here
core.a(HardwareSerial.cpp.o)*:In function `__vector_26'
HardwareSerial.cpp:multiple definition of `__vector_26'
SerialPort.cpp.o:C:\Program Files\arduino-1.0.5\libraries\SerialPort\SerialPort.cpp:271: first defined here
core.a(HardwareSerial.cpp.o)*:In function `__vector_37'
HardwareSerial.cpp:multiple definition of `__vector_37'
SerialPort.cpp.o:C:\Program Files\arduino-1.0.5\libraries\SerialPort\SerialPort.cpp:281: first defined here
core.a(HardwareSerial.cpp.o)*:In function `__vector_52'
HardwareSerial.cpp:multiple definition of `__vector_52'
SerialPort.cpp.o:C:\Program Files\arduino-1.0.5\libraries\SerialPort\SerialPort.cpp:287: first defined here
core.a(HardwareSerial.cpp.o)*:In function `__vector_55'
HardwareSerial.cpp:multiple definition of `__vector_55'
SerialPort.cpp.o:C:\Program Files\arduino-1.0.5\libraries\SerialPort\SerialPort.cpp:293: first defined here
Error creating .elf

Here is the logger code that I am using.

// Serial data logger example.
// Maximum baud rate for a 328 Arduino is 57600.
// Maximum baud rate for a Mega Arduino is 115200.
const uint32_t BAUD_RATE = 57600;

// Maximum time between sync() calls in milliseconds.  If Serial is always
// active, you must provide a way to stop the program and close the file.
const uint32_t MAX_SYNC_TIME_MSEC = 1000;

// Pin number for error blink LED.
// Set ERROR_LED_PIN to -1 for no error LED.
const int8_t ERROR_LED_PIN = 22;

#include <SdFat.h>
#include <SerialPort.h>

#if defined(__AVR_ATmega1280__)\
|| defined(__AVR_ATmega2560__)
// Mega, use 4096 byte RX buffer
SerialPort<0, 4096, 0> NewSerial1;
#else // Mega
// not a Mega, use 1024 RX byte buffer
SerialPort<0, 1024, 0> NewSerial1;
#endif
 
SdFat sd;
SdFile file;

//------------------------------------------------------------------------------
// Error codes repeat as errno short blinks with a delay between codes.
const uint8_t ERROR_INIT   = 1;  // SD init error
const uint8_t ERROR_OPEN   = 2;  // file open error
const uint8_t ERROR_SERIAL = 3;  // serial error
const uint8_t ERROR_WRITE  = 4;  // SD write or sync error
void errorBlink(uint8_t errno) {
	uint8_t i;
	while (ERROR_LED_PIN < 0);
	while (1) {
		for (i = 0; i < errno; i++) {
			digitalWrite(ERROR_LED_PIN, HIGH);
			delay(200);
			digitalWrite(ERROR_LED_PIN, LOW);
			delay(200);
		}
		delay(1600);
	}
}
//------------------------------------------------------------------------------
void setup() {
	pinMode(ERROR_LED_PIN, OUTPUT);
	NewSerial1.begin(BAUD_RATE);
	
	if (!sd.begin()) {
		errorBlink(ERROR_INIT);
	}
	if (!file.open("SERIAL.txt", O_WRITE | O_CREAT | O_AT_END)) {
		errorBlink(ERROR_OPEN);
	}
	if (file.fileSize() == 0) {
		// Make sure first cluster is allocated.
		file.write((uint8_t)0);
		file.rewind();
		file.sync();
	}
}
//------------------------------------------------------------------------------
// Time of last sync call.
uint32_t syncTime = 0;

uint8_t buf[32];
void loop() {
	if (NewSerial1.getRxError()) {
		errorBlink(ERROR_SERIAL);
	}
	uint8_t n = NewSerial1.read(buf, sizeof(buf));
	Serial.println(n);
	if (n > 0) {
		if (file.write(buf, n) != n) {
			errorBlink(ERROR_WRITE);
		}
		// Don't sync if active.
		return;
	}
	if ((millis() - syncTime) < MAX_SYNC_TIME_MSEC) return;
	
	if (!file.sync()) {
		errorBlink(ERROR_WRITE);
	}
	syncTime = millis();
}

Since I can’t seem to use the debugger I put the Serial.println(n); in there to see the number of bytes read off the buffer but even with the debugger set to none I keep getting the above errors. Not sure what is going on. Thanks for any help.

Looking again at this code for the Data logger example, I can’t seem to figure out where the data actually get written. In the loop

void loop() {
	if (NewSerial1.getRxError()) {
		errorBlink(ERROR_SERIAL);
	}
	uint8_t n = NewSerial1.read(buf, sizeof(buf));
	Serial.println(n);
	if (n > 0) {
		if (file.write(buf, n) != n) {
			errorBlink(ERROR_WRITE);
		}
		// Don't sync if active.
		return;
	}
	if ((millis() - syncTime) < MAX_SYNC_TIME_MSEC) return;
	
	if (!file.sync()) {
		errorBlink(ERROR_WRITE);
	}
	syncTime = millis();
}

I dont see how this would write anything:

	if (n > 0) {
		if (file.write(buf, n) != n) {
			errorBlink(ERROR_WRITE);
		}
		// Don't sync if active.
		return;

Does the file.write(buf, n) != n actually do the writing? Sorry for my limited knowledge. Thanks for the help.

You can not mix the standard Serial library with SerialPort like this:

Serial.println(n);

This will cause the Arduino Hardware Serial interrupt handlers to be loaded and there will be an interrupt vector conflict.

This is the same serial port as Arduino Serial so I don’t think debug print would work. I probably should have called it NewSerial, sorry.

SerialPort<0, 4096, 0> NewSerial1;

I would use UART0 for debug. You can then print debug messages like this after changing the name for USART0:

NewSerial.print(n);

Use USART 1, 2, or 3 as your logging port. Here is how to define USART1 as NewSerial1

SerialPort<1, 4096, 0> NewSerial1;

Thanks for the pointers. You will have to forgive my ignorance but I was wondering how this line of code works. I am new to arduino and wondered what the <> operators are doing.

SerialPort<0, 4096, 0> NewSerial1;

Is this saying, SerialPort, 0, use the bigger buffer? Then what is the 0> NewSerial1 doing? Sorry for not knowing this and being such a noob. Just wondering what that line is doing in words.

This is the same serial port as Arduino Serial so I don’t think debug print would work. I probably should have called it NewSerial, sorry.

I made the change to NewSerial1 because I have the shield covering the serial0 ports.

So in order to be able to have the Serial1 connection be the data logging line from the other Mega and be able to use the NewSerial.println functionality do I set this up like this?

#if defined(__AVR_ATmega1280__)\
|| defined(__AVR_ATmega2560__)
// Mega, use 4096 byte RX buffer
SerialPort<0, 4096, 0> NewSerial;
SerialPort<1, 4096, 0> NewSerial1;
#else // Mega
// not a Mega, use 1024 RX byte buffer
SerialPort<0, 1024, 0> NewSerial;
#endif

When I do this and put the NewSerial.println(n); in to look at n, nothing comes through on the monitor. Also the Error LED pin seems to be blinking continuously, prolly 10Hz.

I was also wondering why the Serial.bin file is empty when I run it. Thanks.

SerialPort uses C++ templates. This is a way of using constants in members of a class and allows the compiler to optimize the code better.

The code is documented in SerialPort.html.

Here is the documentation for the template.

SerialPort< PortNumber, RxBufSize, TxBufSize >

PortNumber is the USART number, 0-3 on Mega.

RxBufSize is the size of the receive buffer. Zeros means unbuffered.

TxBufSize is the size of the transmit buffer. Zero means unbuffered.

I am trying to debug the code that I have written and every time I try to create another Serial port to be able to print statements too the SD fails to init. I have added the debug serial port as you have suggested

#if defined(__AVR_ATmega1280__)\
|| defined(__AVR_ATmega2560__)
// Mega, use 4096 byte RX buffer
SerialPort<0, 4096, 0> NewSerial;
SerialPort<1, 4096, 0> NewSerial1;
#else // Mega
// not a Mega, use 1024 RX byte buffer
SerialPort<0, 1024, 0> NewSerial;
#endif

When I do this my Error LED goes crazy. So I put a println in the errorBlink function to see if it would print anything.

void errorBlink(uint8_t errno) {
	uint8_t i;
	while (ERROR_LED_PIN < 0);
	while (1) {
		for (i = 0; i < errno; i++) {
			digitalWrite(ERROR_LED_PIN, HIGH);
			delay(200);
			digitalWrite(ERROR_LED_PIN, LOW);
			delay(200);
			
		}
		delay(1600);
		NewSerial.println(errno);
	}
}

My output is a constant 1 showing that the SD card is not able to init. Why would adding another Serial port for debugging matter? Also, I can’t find any documentation on meaning on of the SD LED on the data logger being on. Its the Red LED between the battery holder and the SD card reader. Its on solid right now. I assume its a bad thing but can’t find documentation on its meaning.

I am still having issues with the SD card not initializing when I add another serial port. This is what I have now.

// Serial data logger example.
// Maximum baud rate for a 328 Arduino is 57600.
// Maximum baud rate for a Mega Arduino is 115200.
const uint32_t BAUD_RATE = 115200;

// Maximum time between sync() calls in milliseconds.  If Serial is always
// active, you must provide a way to stop the program and close the file.
const uint32_t MAX_TXSYNC_TIME_MSEC = 1000;
const uint32_t MAX_RXSYNC_TIME_MSEC = 2000;

// Pin number for error blink LED.
// Set ERROR_LED_PIN to -1 for no error LED.
const int8_t ERROR_LED_PIN = 22;
const int8_t CHIP_SELECT = 53;  // Set to 53 cause using with a Mega

#include <SPI.h>
#include <SdFat.h>
#include <Sd2Card.h>
#include <SerialPort.h>
#include <Wire.h>
#include "RTClib.h"

#if defined(__AVR_ATmega1280__)\
|| defined(__AVR_ATmega2560__)
// Mega, use 4096 byte RX buffer
SerialPort<0, 4096, 0> NewSerial;
SerialPort<1, 4096, 0> NewSerial1;
SerialPort<2, 4096, 0> NewSerial2;
#else // Mega
// not a Mega, use 1024 RX byte buffer
SerialPort<0, 1024, 0> NewSerial;
#endif


 
SdFat sd;
SdFile logfile;
SdFile logfile2;

RTC_DS1307 RTC;

//------------------------------------------------------------------------------
// Error codes repeat as errno short blinks with a delay between codes.
const uint8_t ERROR_INIT   = 1;  // SD init error
const uint8_t ERROR_OPEN   = 2;  // file open error
const uint8_t ERROR_SERIAL = 3;  // serial error
const uint8_t ERROR_WRITE  = 4;  // SD write or sync error
const uint8_t ERROR_RTC	   = 5;  // DTC did not begin
const uint8_t ERROR_RXOPEN = 6;  // Rx File open error
void errorBlink(uint8_t errno) {
	NewSerial.println("Got here");
	uint8_t i;
	while (ERROR_LED_PIN < 0);
	while (1) {
		for (i = 0; i < errno; i++) {
			digitalWrite(ERROR_LED_PIN, HIGH);
			delay(200);
			digitalWrite(ERROR_LED_PIN, LOW);
			delay(200);
			NewSerial.println(errno);
		}
		delay(1600);
	}
}

//------------------------------------------------------------------------------
void setup() {
	
	
	pinMode(ERROR_LED_PIN, OUTPUT);
	pinMode(CHIP_SELECT, OUTPUT);
	
	NewSerial.begin(BAUD_RATE);
	NewSerial1.begin(BAUD_RATE);
	NewSerial2.begin(BAUD_RATE);
	
	if (!sd.begin(CHIP_SELECT)) {
		errorBlink(ERROR_INIT);
	}

	// Create new TX file on SD card
	char TxfileName[] = "GAR_TX00.csv";
	for (uint8_t i = 0; i <100; i++){
		TxfileName[6] = i/10 + '0';
		TxfileName[7] = i%10 + '0';
		if (! sd.exists(TxfileName)){
			// Only open new file if it doesn't exist
			if (!logfile.open(TxfileName, O_WRITE | O_CREAT | O_AT_END)){
			errorBlink(ERROR_OPEN);}
			break;  //leave loop
		}
	}

	// Create new RX file on SD card
	char RxfileName[] = "GAR_RX00.csv";
	for (uint8_t i = 0; i <100; i++){
		RxfileName[6] = i/10 + '0';
		RxfileName[7] = i%10 + '0';
		if (! sd.exists(RxfileName)){
			// Only open new file if it doesn't exist
			if (!logfile.open(RxfileName, O_WRITE | O_CREAT | O_AT_END)){
			errorBlink(ERROR_RXOPEN);}
			break;  //leave loop
		}
	}
	
	if (logfile.fileSize() == 0) {
		// Make sure first cluster is allocated.
		logfile.write((uint8_t)0);
		logfile.rewind();
		logfile.sync();
	}
	
	if (logfile2.fileSize() == 0) {
		// Make sure first cluster is allocated.
		logfile2.write((uint8_t)0);
		logfile2.rewind();
		logfile2.sync();
	}
	
	// Connect to the RTC
	Wire.begin();
	if (!RTC.begin()){
		logfile.println("RTC Failed");
		logfile2.println("RTC Failed");
		errorBlink(ERROR_RTC);
	}
	
	// Get RTC time at start up
	DateTime now;
	
	// Fetch the current time
	now = RTC.now();
	// Log the time
	//logfile.print(now.secondstime()); // seconds since 2000
	//logfile.print(",");
	logfile.print(now.year(), DEC);
	logfile.print("/");
	logfile.print(now.month(), DEC);
	logfile.print("/");
	logfile.print(now.day(), DEC);
	logfile.print(" ");
	logfile.print(now.hour(), DEC);
	logfile.print(":");
	logfile.print(now.minute(), DEC);
	logfile.print(":");
	logfile.print(now.second(), DEC);
	logfile.println("");
	// Other File
	logfile2.print(now.year(), DEC);
	logfile2.print("/");
	logfile2.print(now.month(), DEC);
	logfile2.print("/");
	logfile2.print(now.day(), DEC);
	logfile2.print(" ");
	logfile2.print(now.hour(), DEC);
	logfile2.print(":");
	logfile2.print(now.minute(), DEC);
	logfile2.print(":");
	logfile2.print(now.second(), DEC);
	logfile2.println("");
	
}


//------------------------------------------------------------------------------

void getRTCtime(){
	
	DateTime now;
	
	// Fetch the current time
	now = RTC.now();
	return;
	
}

	
//------------------------------------------------------------------------------
// Time of last sync call.
uint32_t txSyncTime = 0;
uint32_t rxSyncTime = 0;

uint8_t buf[32];

void loop() {
	DateTime now;
	
	uint8_t n=0;
	uint8_t m=0;
	
// Get the Garmin TX traffic	
	if (NewSerial1.getRxError()) {
		errorBlink(ERROR_SERIAL);
	}
	
	n = NewSerial1.read(buf, sizeof(buf));
	
	if (n > 0) { // If there is something in the buffer
		
		// Attempt to log the correct time stamp to file
		now = RTC.now();
		logfile.timestamp(T_WRITE, now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
		
		// Commented this out 3/13/14 to try to get the HEX values saved to the file.
		//if (logfile.write(buf, n) != n) {
		//errorBlink(ERROR_WRITE);
		//}
		
		for (int i=0; i<sizeof(buf); i++){
			if (buf[i]<0x10) {logfile.print("0");
			}
			logfile.print(buf[i],HEX);
			logfile.print(" ");
		}
		
		// Get the current micros time
		logfile.print(",");
		logfile.println(micros());
				
	// Don't sync if active.
	return;
}
if ((millis() - txSyncTime) < MAX_TXSYNC_TIME_MSEC) return;

if (!logfile.sync()) {
	errorBlink(ERROR_WRITE);
}
txSyncTime = millis();

// Log the RX side *****************************************************************
// Get the Garmin RX traffic
if (NewSerial2.getRxError()) {
	errorBlink(ERROR_SERIAL);
}

m = NewSerial2.read(buf, sizeof(buf));

if (m > 0) { // If there is something in the buffer
	
	// Attempt to log the correct time stamp to file
	now = RTC.now();
	logfile2.timestamp(T_WRITE, now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
	
	// Commented this out 3/13/14 to try to get the HEX values saved to the file.
	//if (logfile.write(buf, n) != n) {
	//errorBlink(ERROR_WRITE);
	//}
	
	for (int i=0; i<sizeof(buf); i++){
		if (buf[i]<0x10) {logfile2.print("0");
		}
		logfile2.print(buf[i],HEX);
		logfile2.print(" ");
	}
	
	// Get the current micros time
	logfile2.print(",");
	logfile2.println(micros());
	
	// Don't sync if active.
	return;
}
if ((millis() - rxSyncTime) < MAX_RXSYNC_TIME_MSEC) return;

if (!logfile2.sync()) {
	errorBlink(ERROR_WRITE);
}
rxSyncTime = millis();
}

Its not just adding the serial debug port that caused the issue. I added the second serial port to log the other side of the UART and that is when I see the error light going nuts. Another thing I don’t understand is that the light is blinking on and off but it is not waiting the 1600 ms like it should based on this code.

void errorBlink(uint8_t errno) {
	NewSerial.println("Got here");
	uint8_t i;
	while (ERROR_LED_PIN < 0);
	while (1) {
		for (i = 0; i < errno; i++) {
			digitalWrite(ERROR_LED_PIN, HIGH);
			delay(200);
			digitalWrite(ERROR_LED_PIN, LOW);
			delay(200);
			NewSerial.println(errno);
		}
		delay(1600);
	}
}

The errno is sent when the the SD card doesn’t initialize with a value of 1. I see the “Got Here” message on the serial window once. Then I see the errno printed in the serial window at a pretty fast rate, I don’t have a scope handy, but it never gets to the delay 1600. Shouldn’t that just blink on then off then wait the 1600 and start all over again? I am not seeing that. I think that is a clue to what is going on but I don’t know what it means. I appreciate any help you guys can give me. Thanks.

Rick

Looks like you are allocating too much buffer for your serial ports.

SerialPort<0, 4096, 0> NewSerial;
SerialPort<1, 4096, 0> NewSerial1;
SerialPort<2, 4096, 0> NewSerial2;

This is 3*4096 or 12288 bytes. The Mega only has 8 KB of SRAM so I think you are just having repeated crashes due to the stack overlapping serial buffers. This explains the delay(1600) problem.

Thanks for the heads up on that. That did fix my constant reset problem. I am now having issues with the .sync() function. I am attempting to log the other side of the communication to a separate file and have added the code to do so. I am opening and closing the files where I believe it to be appropriate but since I have added this code I now get an error when trying to sync the tx file on the SD card. Here is the whole code.

// Serial data logger example.
// Maximum baud rate for a 328 Arduino is 57600.
// Maximum baud rate for a Mega Arduino is 115200.
const uint32_t BAUD_RATE = 115200;

// Maximum time between sync() calls in milliseconds.  If Serial is always
// active, you must provide a way to stop the program and close the file.
const uint32_t MAX_TXSYNC_TIME_MSEC = 1000;
const uint32_t MAX_RXSYNC_TIME_MSEC = 2000;

// Pin number for error blink LED.
// Set ERROR_LED_PIN to -1 for no error LED.
const int8_t ERROR_LED_PIN = 22;
const int8_t CHIP_SELECT = 53;  // Set to 53 cause using with a Mega

// File names to save files on the SD card
const char* TxFileName;
const char* RxFileName;

#include <SPI.h>
#include <SdFat.h>
#include <Sd2Card.h>
#include <SerialPort.h>
#include <Wire.h>
#include "RTClib.h"

#if defined(__AVR_ATmega1280__)\
|| defined(__AVR_ATmega2560__)
// Mega, use 4096 byte RX buffer
SerialPort<0, 2048, 0> NewSerial;
SerialPort<1, 2048, 0> NewSerial1;
SerialPort<2, 2048, 0> NewSerial2;
#else // Mega
// not a Mega, use 1024 RX byte buffer
SerialPort<0, 1024, 0> NewSerial;
#endif


 
SdFat sd;
SdFile logfile;
//SdFile logfile2;

RTC_DS1307 RTC;

//------------------------------------------------------------------------------
// Error codes repeat as errno short blinks with a delay between codes.
const uint8_t ERROR_INIT   = 1;  // SD init error
const uint8_t ERROR_OPEN   = 2;  // file open error
const uint8_t ERROR_SERIAL = 3;  // serial error
const uint8_t ERROR_TXWRITE  = 4;  // SD write or sync error
const uint8_t ERROR_RTC	   = 5;  // DTC did not begin
const uint8_t ERROR_RXOPEN = 6;  // Rx File open error
const uint8_t ERROR_RXWRITE = 7; // Can't write or sync the RX file

void errorBlink(uint8_t errno) {
	uint8_t i;
	while (ERROR_LED_PIN < 0);
	while (1) {
		for (i = 0; i < errno; i++) {
			digitalWrite(ERROR_LED_PIN, HIGH);
			delay(200);
			digitalWrite(ERROR_LED_PIN, LOW);
			delay(200);
			NewSerial.println(errno);
		}
		delay(1600);
	}
}

//------------------------------------------------------------------------------
void setup() {
	
	
	pinMode(ERROR_LED_PIN, OUTPUT);
	pinMode(CHIP_SELECT, OUTPUT);
	
	NewSerial.begin(BAUD_RATE);
	NewSerial1.begin(BAUD_RATE);
	NewSerial2.begin(BAUD_RATE);
	
	if (!sd.begin(CHIP_SELECT)) {
		errorBlink(ERROR_INIT);
	}

	// Create new TX file on SD card
	char TxfileName[] = "GAR_TX00.csv";
	for (uint8_t i = 0; i <100; i++){
		TxfileName[6] = i/10 + '0';
		TxfileName[7] = i%10 + '0';
		if (! sd.exists(TxfileName)){
			// Only open new file if it doesn't exist
			if (!logfile.open(TxfileName, O_WRITE | O_CREAT | O_AT_END)){
			errorBlink(ERROR_OPEN);}
			break;  //leave loop
		}
	}
	
	
	if (logfile.fileSize() == 0) {
		// Make sure first cluster is allocated.
		logfile.write((uint8_t)0);
		logfile.rewind();
		logfile.sync();
	}
	
	// Create new RX file on SD card
	// Close the TX file first
	logfile.close();
	char RxfileName[] = "GAR_RX00.csv";
	for (uint8_t i = 0; i <100; i++){
		RxfileName[6] = i/10 + '0';
		RxfileName[7] = i%10 + '0';
		if (! sd.exists(RxfileName)){
			// Only open new file if it doesn't exist
			if (!logfile.open(RxfileName, O_WRITE | O_CREAT | O_AT_END)){
			errorBlink(ERROR_RXOPEN);}
			break;  //leave loop
		}
	}
	
	if (logfile.fileSize() == 0) {
		// Make sure first cluster is allocated.
		logfile.write((uint8_t)0);
		logfile.rewind();
		logfile.sync();
	}
	
	// Close the Rx file
	logfile.close();	
	
	// Connect to the RTC
	Wire.begin();
	if (!RTC.begin()){
		// Save to Tx file
		logfile.open(TxfileName, O_WRITE);
		logfile.println("RTC Failed");
		logfile.close();
		
		// Save to Rx file
		logfile.open(RxfileName, O_WRITE);
		logfile.println("RTC Failed");
		logfile.close();
		errorBlink(ERROR_RTC);
	}
	
	// Get RTC time at start up
	DateTime now;
	
	// Fetch the current time
	now = RTC.now();
	// Log the time
	//logfile.print(now.secondstime()); // seconds since 2000
	//logfile.print(",");
	logfile.open(TxfileName, O_WRITE);
	logfile.print(now.year(), DEC);
	logfile.print("/");
	logfile.print(now.month(), DEC);
	logfile.print("/");
	logfile.print(now.day(), DEC);
	logfile.print(" ");
	logfile.print(now.hour(), DEC);
	logfile.print(":");
	logfile.print(now.minute(), DEC);
	logfile.print(":");
	logfile.print(now.second(), DEC);
	logfile.println("");
	logfile.close();
	// Other File
	logfile.open(RxfileName, O_WRITE);
	logfile.print(now.year(), DEC);
	logfile.print("/");
	logfile.print(now.month(), DEC);
	logfile.print("/");
	logfile.print(now.day(), DEC);
	logfile.print(" ");
	logfile.print(now.hour(), DEC);
	logfile.print(":");
	logfile.print(now.minute(), DEC);
	logfile.print(":");
	logfile.print(now.second(), DEC);
	logfile.println("");
	logfile.close();
	
}


//------------------------------------------------------------------------------
// Time of last sync call.
uint32_t txSyncTime = 0;
uint32_t rxSyncTime = 0;

uint8_t buf[32];

void loop() {
	DateTime now;
	
	uint8_t n=0;
	uint8_t m=0;
	
// Get the Garmin TX traffic	
	if (NewSerial1.getRxError()) {
		errorBlink(ERROR_SERIAL);
	}
	
	n = NewSerial1.read(buf, sizeof(buf));
	
	if (n > 0) { // If there is something in the buffer
		
		// Need to open the Ts file to write out the data
		logfile.open(TxFileName, O_WRITE);
		
		// Attempt to log the correct time stamp to file
		now = RTC.now();
		logfile.timestamp(T_WRITE, now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
		
		// Commented this out 3/13/14 to try to get the HEX values saved to the file.
		//if (logfile.write(buf, n) != n) {
		//errorBlink(ERROR_WRITE);
		//}
		
		for (int i=0; i<sizeof(buf); i++){
			if (buf[i]<0x10) {logfile.print("0");
			}
			logfile.print(buf[i],HEX);
			logfile.print(" ");
		}
		
		// Get the current micros time
		logfile.print(",");
		logfile.println(micros());
				
	// Don't sync if active.
	return;
}
if ((millis() - txSyncTime) < MAX_TXSYNC_TIME_MSEC) return;

if (!logfile.sync()) {
	NewSerial.println("can't sync to the Tx file");
	errorBlink(ERROR_TXWRITE);
}
txSyncTime = millis();

logfile.close();

// Log the RX side *****************************************************************
// Get the Garmin RX traffic
if (NewSerial2.getRxError()) {
	errorBlink(ERROR_SERIAL);
}

m = NewSerial2.read(buf, sizeof(buf));

if (m > 0) { // If there is something in the buffer
	
	logfile.open(RxFileName, O_WRITE);
	// Attempt to log the correct time stamp to file
	now = RTC.now();
	logfile.timestamp(T_WRITE, now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
	
	// Commented this out 3/13/14 to try to get the HEX values saved to the file.
	//if (logfile.write(buf, n) != n) {
	//errorBlink(ERROR_WRITE);
	//}
	
	for (int i=0; i<sizeof(buf); i++){
		if (buf[i]<0x10) {logfile.print("0");
		}
		logfile.print(buf[i],HEX);
		logfile.print(" ");
	}
	
	// Get the current micros time
	logfile.print(",");
	logfile.println(micros());
	
	// Don't sync if active.
	return;
}
if ((millis() - rxSyncTime) < MAX_RXSYNC_TIME_MSEC) return;

if (!logfile.sync()) {
	NewSerial.println("can't sync to the Rx file");
	errorBlink(ERROR_RXWRITE);
}
rxSyncTime = millis();

logfile.close();
}

I am bombing out at

if (!logfile.sync()) {
	NewSerial.println("can't sync to the Tx file");
	errorBlink(ERROR_TXWRITE);
}

The file is not getting the correct time stamp either. I am not sure why. This code is in the set up function.

// Get RTC time at start up
	DateTime now;
	
	// Fetch the current time
	now = RTC.now();
	// Log the time
	//logfile.print(now.secondstime()); // seconds since 2000
	//logfile.print(",");
	logfile.open(TxfileName, O_WRITE);
	logfile.print(now.year(), DEC);
	logfile.print("/");
	logfile.print(now.month(), DEC);
	logfile.print("/");
	logfile.print(now.day(), DEC);
	logfile.print(" ");
	logfile.print(now.hour(), DEC);
	logfile.print(":");
	logfile.print(now.minute(), DEC);
	logfile.print(":");
	logfile.print(now.second(), DEC);
	logfile.println("");
	logfile.close();
	// Other File
	logfile.open(RxfileName, O_WRITE);
	logfile.print(now.year(), DEC);
	logfile.print("/");
	logfile.print(now.month(), DEC);
	logfile.print("/");
	logfile.print(now.day(), DEC);
	logfile.print(" ");
	logfile.print(now.hour(), DEC);
	logfile.print(":");
	logfile.print(now.minute(), DEC);
	logfile.print(":");
	logfile.print(now.second(), DEC);
	logfile.println("");
	logfile.close();

I thought this would do it.

As always I appreciate any help that anyone can give. Thanks.

Rick

Where does TxFileName get assigned a value? The TxFilename variable in setup is a different variable.

Thanks for the catch. I didn't notice that. I made the change so that the variables are right. I am still bombing out in the same spot. I am also confused as to why the RTC.now function is saving the wrong time stamp. It is 1/1/2000 12:00:00 AM. It is getting written to the SD card like it should but it's wrong. The

// Need to open the Ts file to write out the data
		logfile.open(TxFileName, O_WRITE);
		
		// Attempt to log the correct time stamp to file
		now = RTC.now();
		logfile.timestamp(T_WRITE, now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());

doesn't appear to be working either. I am not getting an error at

Wire.begin();
	if (!RTC.begin()){
		// Save to Tx file
		logfile.open(TxFileName, O_WRITE);
		logfile.println("RTC Failed");
		logfile.close();
		errorBlink(ERROR_RTC);
		
		// Save to Rx file
		logfile.open(RxFileName, O_WRITE);
		logfile.println("RTC Failed");
		logfile.close();
		errorBlink(ERROR_RTC);
	}

So it should be getting the correct time. Not sure what is going on here.

Not sure what is going on here.

Nor, in the absence of updated code, are we.

Point taken.

// Serial data logger example.
// Maximum baud rate for a 328 Arduino is 57600.
// Maximum baud rate for a Mega Arduino is 115200.
const uint32_t BAUD_RATE = 115200;

// Maximum time between sync() calls in milliseconds.  If Serial is always
// active, you must provide a way to stop the program and close the file.
const uint32_t MAX_TXSYNC_TIME_MSEC = 1000;
const uint32_t MAX_RXSYNC_TIME_MSEC = 2000;

// Pin number for error blink LED.
// Set ERROR_LED_PIN to -1 for no error LED.
const int8_t ERROR_LED_PIN = 22;
const int8_t CHIP_SELECT = 53;  // Set to 53 cause using with a Mega

// File names to save files on the SD card
const char* TxFileName;
const char* RxFileName;

#include <SPI.h>
#include <SdFat.h>
#include <Sd2Card.h>
#include <SerialPort.h>
#include <Wire.h>
#include "RTClib.h"

#if defined(__AVR_ATmega1280__)\
|| defined(__AVR_ATmega2560__)
// Mega, use 4096 byte RX buffer
SerialPort<0, 2048, 0> NewSerial;
SerialPort<1, 2048, 0> NewSerial1;
SerialPort<2, 2048, 0> NewSerial2;
#else // Mega
// not a Mega, use 1024 RX byte buffer
SerialPort<0, 1024, 0> NewSerial;
#endif
 
SdFat sd;
SdFile logfile;

RTC_DS1307 RTC;

//------------------------------------------------------------------------------
// Error codes repeat as errno short blinks with a delay between codes.
const uint8_t ERROR_INIT   = 1;  // SD init error
const uint8_t ERROR_OPEN   = 2;  // file open error
const uint8_t ERROR_SERIAL = 3;  // serial error
const uint8_t ERROR_TXWRITE  = 4;  // SD write or sync error
const uint8_t ERROR_RTC	   = 5;  // DTC did not begin
const uint8_t ERROR_RXOPEN = 6;  // Rx File open error
const uint8_t ERROR_RXWRITE = 7; // Can't write or sync the RX file

void errorBlink(uint8_t errno) {
	uint8_t i;
	while (ERROR_LED_PIN < 0);
	while (1) {
		for (i = 0; i < errno; i++) {
			digitalWrite(ERROR_LED_PIN, HIGH);
			delay(200);
			digitalWrite(ERROR_LED_PIN, LOW);
			delay(200);
			NewSerial.println(errno);
		}
		delay(1600);
	}
}

//------------------------------------------------------------------------------
void setup() {
	
	
	pinMode(ERROR_LED_PIN, OUTPUT);
	pinMode(CHIP_SELECT, OUTPUT);
	
	NewSerial.begin(BAUD_RATE);
	NewSerial1.begin(BAUD_RATE);
	NewSerial2.begin(BAUD_RATE);
	
	if (!sd.begin(CHIP_SELECT)) {
		errorBlink(ERROR_INIT);
	}

	// Create new TX file on SD card
	char TxFileName[] = "GAR_TX00.csv";
	for (uint8_t i = 0; i <100; i++){
		TxFileName[6] = i/10 + '0';
		TxFileName[7] = i%10 + '0';
		if (! sd.exists(TxFileName)){
			// Only open new file if it doesn't exist
			if (!logfile.open(TxFileName, O_WRITE | O_CREAT | O_AT_END)){
			errorBlink(ERROR_OPEN);}
			break;  //leave loop
		}
	}	
	
	if (logfile.fileSize() == 0) {
		// Make sure first cluster is allocated.
		logfile.write((uint8_t)0);
		logfile.rewind();
		logfile.sync();
	}
	
	// Create new RX file on SD card
	// Close the TX file first
	logfile.close();
	char RxFileName[] = "GAR_RX00.csv";
	for (uint8_t i = 0; i <100; i++){
		RxFileName[6] = i/10 + '0';
		RxFileName[7] = i%10 + '0';
		if (! sd.exists(RxFileName)){
			// Only open new file if it doesn't exist
			if (!logfile.open(RxFileName, O_WRITE | O_CREAT | O_AT_END)){
			errorBlink(ERROR_RXOPEN);}
			break;  //leave loop
		}
	}
	
	if (logfile.fileSize() == 0) {
		// Make sure first cluster is allocated.
		logfile.write((uint8_t)0);
		logfile.rewind();
		logfile.sync();
	}
	
	// Close the Rx file
	logfile.close();	
	
	// Connect to the RTC
	Wire.begin();
	if (!RTC.begin()){
		// Save to Tx file
		logfile.open(TxFileName, O_WRITE);
		logfile.println("RTC Failed");
		logfile.close();
		errorBlink(ERROR_RTC);
		
		// Save to Rx file
		logfile.open(RxFileName, O_WRITE);
		logfile.println("RTC Failed");
		logfile.close();
		errorBlink(ERROR_RTC);
	}
	
	// Get RTC time at start up
	DateTime now;
	
	// Fetch the current time
	now = RTC.now();
	// Log the time
	//logfile.print(now.secondstime()); // seconds since 2000
	//logfile.print(",");
	logfile.open(TxFileName, O_WRITE);
	logfile.print(now.year(), DEC);
	logfile.print("/");
	logfile.print(now.month(), DEC);
	logfile.print("/");
	logfile.print(now.day(), DEC);
	logfile.print(" ");
	logfile.print(now.hour(), DEC);
	logfile.print(":");
	logfile.print(now.minute(), DEC);
	logfile.print(":");
	logfile.print(now.second(), DEC);
	logfile.println("");
	logfile.close();
	// Other File
	logfile.open(RxFileName, O_WRITE);
	logfile.print(now.year(), DEC);
	logfile.print("/");
	logfile.print(now.month(), DEC);
	logfile.print("/");
	logfile.print(now.day(), DEC);
	logfile.print(" ");
	logfile.print(now.hour(), DEC);
	logfile.print(":");
	logfile.print(now.minute(), DEC);
	logfile.print(":");
	logfile.print(now.second(), DEC);
	logfile.println("");
	logfile.close();
	
	// DEBUGGING: Print out to the serial port to see what is going the time is.
//
	//NewSerial.print(now.year(), DEC);
	//NewSerial.print("/");
	//NewSerial.print(now.month(), DEC);
	//NewSerial.print("/");
	//NewSerial.print(now.day(), DEC);
	//NewSerial.print(" ");
	//NewSerial.print(now.hour(), DEC);
	//NewSerial.print(":");
	//NewSerial.print(now.minute(), DEC);
	//NewSerial.print(":");
	//NewSerial.print(now.second(), DEC);
	//NewSerial.println("");

	
	
}


//------------------------------------------------------------------------------
// Time of last sync call.
uint32_t txSyncTime = 0;
uint32_t rxSyncTime = 0;

uint8_t buf[32];

void loop() {
	DateTime now;
	
	uint8_t n=0;
	uint8_t m=0;
	
// Get the Garmin TX traffic	
	if (NewSerial1.getRxError()) {
		errorBlink(ERROR_SERIAL);
	}
	
	n = NewSerial1.read(buf, sizeof(buf));
	
	if (n > 0) { // If there is something in the buffer
		
		// Need to open the Ts file to write out the data
		logfile.open(TxFileName, O_WRITE);
		
		// Attempt to log the correct time stamp to file
		now = RTC.now();
		logfile.timestamp(T_WRITE, now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
		
		// Commented this out 3/13/14 to try to get the HEX values saved to the file.
		//if (logfile.write(buf, n) != n) {
		//errorBlink(ERROR_WRITE);
		//}
		
		for (int i=0; i<sizeof(buf); i++){
			if (buf[i]<0x10) {logfile.print("0");
			}
			logfile.print(buf[i],HEX);
			logfile.print(" ");
		}
		
		// Get the current micros time
		logfile.print(",");
		logfile.println(micros());
				
	// Don't sync if active.
	return;
}
if ((millis() - txSyncTime) < MAX_TXSYNC_TIME_MSEC) return;

if (!logfile.sync()) {
	NewSerial.println("can't sync to the Tx file");
	errorBlink(ERROR_TXWRITE);
}
txSyncTime = millis();

logfile.close();

// Log the RX side *****************************************************************
// Get the Garmin RX traffic
if (NewSerial2.getRxError()) {
	errorBlink(ERROR_SERIAL);
}

m = NewSerial2.read(buf, sizeof(buf));

if (m > 0) { // If there is something in the buffer
	
	logfile.open(RxFileName, O_WRITE);
	// Attempt to log the correct time stamp to file
	now = RTC.now();
	logfile.timestamp(T_WRITE, now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
	
	// Commented this out 3/13/14 to try to get the HEX values saved to the file.
	//if (logfile.write(buf, n) != n) {
	//errorBlink(ERROR_WRITE);
	//}
	
	for (int i=0; i<sizeof(buf); i++){
		if (buf[i]<0x10) {logfile.print("0");
		}
		logfile.print(buf[i],HEX);
		logfile.print(" ");
	}
	
	// Get the current micros time
	logfile.print(",");
	logfile.println(micros());
	
	// Don't sync if active.
	return;
}
if ((millis() - rxSyncTime) < MAX_RXSYNC_TIME_MSEC) return;

if (!logfile.sync()) {
	NewSerial.println("can't sync to the Rx file");
	errorBlink(ERROR_RXWRITE);
}
rxSyncTime = millis();

logfile.close();
}

So, now you have global variables called TxFileName and RxFileName, which are of type pointer to char. And, you have local variables of the same name, but different types, in setup(). The local variables, that do get valued, go out of scope when setup() ends. Then, you reference the uninitialized global pointers in loop().

Hopefully, I don't need to point out that accessing an uninitialized pointer is a bad idea.

Nor should it be necessary to point out that local variables with the same name as global variables are a bad idea.

Thanks for the help and sorry for being such a noob. Any who, I think I see what I need to do but am not sure how to do it. Here is how the code looks now:

// Serial data logger example.
// Maximum baud rate for a 328 Arduino is 57600.
// Maximum baud rate for a Mega Arduino is 115200.
const uint32_t BAUD_RATE = 115200;

// Maximum time between sync() calls in milliseconds.  If Serial is always
// active, you must provide a way to stop the program and close the file.
const uint32_t MAX_TXSYNC_TIME_MSEC = 1000;
const uint32_t MAX_RXSYNC_TIME_MSEC = 2000;

// Pin number for error blink LED.
// Set ERROR_LED_PIN to -1 for no error LED.
const int8_t ERROR_LED_PIN = 22;
const int8_t CHIP_SELECT = 53;  // Set to 53 cause using with a Mega

// File names to save files on the SD card
char* TxFileName[13];
char* RxFileName[13];

#include <SPI.h>
#include <SdFat.h>
#include <Sd2Card.h>
#include <SerialPort.h>
#include <Wire.h>
#include "RTClib.h"

#if defined(__AVR_ATmega1280__)\
|| defined(__AVR_ATmega2560__)
// Mega, use 4096 byte RX buffer
SerialPort<0, 2048, 0> NewSerial;
SerialPort<1, 2048, 0> NewSerial1;
SerialPort<2, 2048, 0> NewSerial2;
#else // Mega
// not a Mega, use 1024 RX byte buffer
SerialPort<0, 1024, 0> NewSerial;
#endif
 
SdFat sd;
SdFile logfile;

RTC_DS1307 RTC;

//------------------------------------------------------------------------------
// Error codes repeat as errno short blinks with a delay between codes.
const uint8_t ERROR_INIT   = 1;  // SD init error
const uint8_t ERROR_OPEN   = 2;  // file open error
const uint8_t ERROR_SERIAL = 3;  // serial error
const uint8_t ERROR_TXWRITE  = 4;  // SD write or sync error
const uint8_t ERROR_RTC	   = 5;  // DTC did not begin
const uint8_t ERROR_RXOPEN = 6;  // Rx File open error
const uint8_t ERROR_RXWRITE = 7; // Can't write or sync the RX file
const uint8_t ERROR_LOOP_OPEN = 8; // Cant open file in the loop

void errorBlink(uint8_t errno) {
	uint8_t i;
	while (ERROR_LED_PIN < 0);
	while (1) {
		for (i = 0; i < errno; i++) {
			digitalWrite(ERROR_LED_PIN, HIGH);
			delay(200);
			digitalWrite(ERROR_LED_PIN, LOW);
			delay(200);
			NewSerial.println(errno);
		}
		delay(1600);
	}
}

//------------------------------------------------------------------------------
void setup() {
	
	
	pinMode(ERROR_LED_PIN, OUTPUT);
	pinMode(CHIP_SELECT, OUTPUT);
	
	NewSerial.begin(BAUD_RATE);
	NewSerial1.begin(BAUD_RATE);
	NewSerial2.begin(BAUD_RATE);
	
	if (!sd.begin(CHIP_SELECT)) {
		errorBlink(ERROR_INIT);
	}

	// Create new TX file on SD card
	char TxFile[] = "GAR_TX00.csv";
	for (uint8_t i = 0; i <100; i++){
		TxFile[6] = i/10 + '0';
		TxFile[7] = i%10 + '0';
		if (! sd.exists(TxFile)){
			// Only open new file if it doesn't exist
			if (!logfile.open(TxFile, O_WRITE | O_CREAT | O_AT_END)){
			errorBlink(ERROR_OPEN);}
			strncpy(TxFileName,TxFile,sizeof(TxFile));
			break;  //leave loop
		}
	}	
	
	if (logfile.fileSize() == 0) {
		// Make sure first cluster is allocated.
		logfile.write((uint8_t)0);
		logfile.rewind();
		logfile.sync();
	}
	
	// Create new RX file on SD card
	// Close the TX file first
	logfile.close();
	char RxFile[] = "GAR_RX00.csv";
	for (uint8_t i = 0; i <100; i++){
		RxFile[6] = i/10 + '0';
		RxFile[7] = i%10 + '0';
		if (! sd.exists(RxFile)){
			// Only open new file if it doesn't exist
			if (!logfile.open(RxFile, O_WRITE | O_CREAT | O_AT_END)){
			errorBlink(ERROR_RXOPEN);}
			break;  //leave loop
		}
	}
	
	if (logfile.fileSize() == 0) {
		// Make sure first cluster is allocated.
		logfile.write((uint8_t)0);
		logfile.rewind();
		logfile.sync();
	}
	
	// Close the Rx file
	logfile.close();	
	
	// Connect to the RTC
	Wire.begin();
	if (!RTC.begin()){
		// Save to Tx file
		logfile.open(TxFile, O_WRITE);
		logfile.println("RTC Failed");
		logfile.close();
		errorBlink(ERROR_RTC);
		
		// Save to Rx file
		logfile.open(RxFile, O_WRITE);
		logfile.println("RTC Failed");
		logfile.close();
		errorBlink(ERROR_RTC);
	}
	
	// Get RTC time at start up
	DateTime now;
	
	// Fetch the current time
	now = RTC.now();
	// Log the time
	//logfile.print(now.secondstime()); // seconds since 2000
	//logfile.print(",");
	logfile.open(TxFile, O_WRITE);
	logfile.print(now.year(), DEC);
	logfile.print("/");
	logfile.print(now.month(), DEC);
	logfile.print("/");
	logfile.print(now.day(), DEC);
	logfile.print(" ");
	logfile.print(now.hour(), DEC);
	logfile.print(":");
	logfile.print(now.minute(), DEC);
	logfile.print(":");
	logfile.print(now.second(), DEC);
	logfile.println("");
	logfile.close();
	// Other File
	logfile.open(RxFile, O_WRITE);
	logfile.print(now.year(), DEC);
	logfile.print("/");
	logfile.print(now.month(), DEC);
	logfile.print("/");
	logfile.print(now.day(), DEC);
	logfile.print(" ");
	logfile.print(now.hour(), DEC);
	logfile.print(":");
	logfile.print(now.minute(), DEC);
	logfile.print(":");
	logfile.print(now.second(), DEC);
	logfile.println("");
	logfile.close();
	
	// DEBUGGING: Print out to the serial port to see what is going the time is.
//
	//NewSerial.print(now.year(), DEC);
	//NewSerial.print("/");
	//NewSerial.print(now.month(), DEC);
	//NewSerial.print("/");
	//NewSerial.print(now.day(), DEC);
	//NewSerial.print(" ");
	//NewSerial.print(now.hour(), DEC);
	//NewSerial.print(":");
	//NewSerial.print(now.minute(), DEC);
	//NewSerial.print(":");
	//NewSerial.print(now.second(), DEC);
	//NewSerial.println("");

	
	
}


//------------------------------------------------------------------------------
// Time of last sync call.
uint32_t txSyncTime = 0;
uint32_t rxSyncTime = 0;


uint8_t buf[32];

void loop() {
	
	DateTime now;
	uint8_t n=0;
	uint8_t m=0;
	 
	
// Get the Garmin TX traffic	
	if (NewSerial1.getRxError()) {
		errorBlink(ERROR_SERIAL);
	}
	
	n = NewSerial1.read(buf, sizeof(buf));
	
	if (n > 0) { // If there is something in the buffer
		
		// Need to open the Ts file to write out the data
		//logfile.open(TxFileName, O_WRITE);
			if (!logfile.open(TxFileName, O_WRITE)){
			errorBlink(ERROR_LOOP_OPEN);}		
		
		// Attempt to log the correct time stamp to file
		now = RTC.now();
		logfile.timestamp(T_WRITE, now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
		
		// Commented this out 3/13/14 to try to get the HEX values saved to the file.
		//if (logfile.write(buf, n) != n) {
		//errorBlink(ERROR_WRITE);
		//}
		
		for (int i=0; i<sizeof(buf); i++){
			if (buf[i]<0x10) {logfile.print("0");
			}
			logfile.print(buf[i],HEX);
			logfile.print(" ");
		}
		
		// Get the current micros time
		logfile.print(",");
		logfile.println(micros());
				
	// Don't sync if active.
	return;
}
if ((millis() - txSyncTime) < MAX_TXSYNC_TIME_MSEC) return;

if (!logfile.sync()) {
	//NewSerial.println(TxFileName);
	NewSerial.println("can't sync to the Tx file");
	errorBlink(ERROR_TXWRITE);
}
txSyncTime = millis();

logfile.close();

// Log the RX side *****************************************************************
// Get the Garmin RX traffic
if (NewSerial2.getRxError()) {
	errorBlink(ERROR_SERIAL);
}

m = NewSerial2.read(buf, sizeof(buf));

if (m > 0) { // If there is something in the buffer
	
	logfile.open(RxFileName, O_WRITE);
	// Attempt to log the correct time stamp to file
	now = RTC.now();
	logfile.timestamp(T_WRITE, now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
	
	// Commented this out 3/13/14 to try to get the HEX values saved to the file.
	//if (logfile.write(buf, n) != n) {
	//errorBlink(ERROR_WRITE);
	//}
	
	for (int i=0; i<sizeof(buf); i++){
		if (buf[i]<0x10) {logfile.print("0");
		}
		logfile.print(buf[i],HEX);
		logfile.print(" ");
	}
	
	// Get the current micros time
	logfile.print(",");
	logfile.println(micros());
	
	// Don't sync if active.
	return;
}
if ((millis() - rxSyncTime) < MAX_RXSYNC_TIME_MSEC) return;

if (!logfile.sync()) {
	NewSerial.println("can't sync to the Rx file");
	errorBlink(ERROR_RXWRITE);
}
rxSyncTime = millis();

logfile.close();
}

I keep getting errors and will post that separately to keep this under the 9500 limit.

Post continued…

So what I would like to do is get the TxFile and RxFile names stored into the global character arrays. I was trying to use the strncpy() function but I keep getting the following errors.

Compiling ‘SerialLoggerWithMega’ for ‘Arduino Mega 2560 or Mega ADK’
SerialLoggerWithMega.ino:In function ‘void setup()’
SerialLoggerWithMega.ino:98: error: cannot convert ‘char**’ to ‘char*’ for argument ‘1’ to ‘char* strncpy(char*, const char*, size_t)’
SerialLoggerWithMega.ino:In function ‘void loop()’
SerialLoggerWithMega.ino:235: error: no matching function for call to ‘SdFile::open(char* [13], const uint8_t&)’
SdBaseFile.h:open(SdBaseFile*, uint16_t, uint8_t)
SdBaseFile.h:open(SdBaseFile*, const char*, uint8_t)
SdBaseFile.h:open(const char*, uint8_t)
SdBaseFile.h:open(SdBaseFile*, const uint8_t*, uint8_t)
SerialLoggerWithMega.ino:282: error: no matching function for call to ‘SdFile::open(char* [13], const uint8_t&)’
SdBaseFile.h:open(SdBaseFile*, uint16_t, uint8_t)
SdBaseFile.h:open(SdBaseFile*, const char*, uint8_t)
SdBaseFile.h:open(const char*, uint8_t)
SdBaseFile.h:open(SdBaseFile*, const uint8_t*, uint8_t)
Error compiling

Not having a strong programming foundation, pointers are like black magic to me. I get that they reference addresses and stuff but how to use them effectively is confusing for me. I see that when I try to open the file that was created in the Setup function that it isn’t done correctly so the program bombs. I am just not sure on how to get that newly created file from setup to the loop. Thanks

So what I would like to do is get the TxFile and RxFile names stored into the global character arrays.

No problem.

All you need to do is define these correctly:

char* TxFileName[13];
char* RxFileName[13];

These are NOT arrays of chars. They are arrays of pointers. They are NOT what you want.

Don't make this ANY more complicated than it needs to be:

char TxFileName[13];
char RxFileName[13];

I have a tendency to over think things. Thanks for the help. The code now complies which is great but I am still having issues opening the file off the SD card after it is created to add the data to it. At first I thought it was because the name of the file was longer than 11 characters and changed the name to be 11 but I am still not able to open the file. I have closed the previous file before trying to open another one. I am not sure what makes the .open() fail. I have tried all I can think of after looking at the implementation and am at a loss. Here is the code as of now:

// Serial data logger example.
// Maximum baud rate for a 328 Arduino is 57600.
// Maximum baud rate for a Mega Arduino is 115200.
const uint32_t BAUD_RATE = 115200;

// Maximum time between sync() calls in milliseconds.  If Serial is always
// active, you must provide a way to stop the program and close the file.
const uint32_t MAX_TXSYNC_TIME_MSEC = 1000;
const uint32_t MAX_RXSYNC_TIME_MSEC = 2000;

// Pin number for error blink LED.
// Set ERROR_LED_PIN to -1 for no error LED.
const int8_t ERROR_LED_PIN = 22;
const int8_t CHIP_SELECT = 53;  // Set to 53 cause using with a Mega

// File names to save files on the SD card
char TxFileName[11];
char RxFileName[11];

#include <SPI.h>
#include <SdFat.h>
#include <Sd2Card.h>
#include <SerialPort.h>
#include <Wire.h>
#include "RTClib.h"

#if defined(__AVR_ATmega1280__)\
|| defined(__AVR_ATmega2560__)
// Mega, use 4096 byte RX buffer
SerialPort<0, 2048, 0> NewSerial;
SerialPort<1, 2048, 0> NewSerial1;
SerialPort<2, 2048, 0> NewSerial2;
#else // Mega
// not a Mega, use 1024 RX byte buffer
SerialPort<0, 1024, 0> NewSerial;
#endif
 
SdFat sd;
SdFile logfile;

RTC_DS1307 RTC;

//------------------------------------------------------------------------------
// Error codes repeat as errno short blinks with a delay between codes.
const uint8_t ERROR_INIT   = 1;  // SD init error
const uint8_t ERROR_OPEN   = 2;  // file open error
const uint8_t ERROR_SERIAL = 3;  // serial error
const uint8_t ERROR_TXWRITE  = 4;  // SD write or sync error
const uint8_t ERROR_RTC	   = 5;  // DTC did not begin
const uint8_t ERROR_RXOPEN = 6;  // Rx File open error
const uint8_t ERROR_RXWRITE = 7; // Can't write or sync the RX file
const uint8_t ERROR_LOOP_OPEN = 8; // Cant open file in the loop

void errorBlink(uint8_t errno) {
	uint8_t i;
	while (ERROR_LED_PIN < 0);
	while (1) {
		for (i = 0; i < errno; i++) {
			digitalWrite(ERROR_LED_PIN, HIGH);
			delay(200);
			digitalWrite(ERROR_LED_PIN, LOW);
			delay(200);
			NewSerial.println(errno);
		}
		delay(1600);
	}
}

//------------------------------------------------------------------------------
void setup() {
	
	
	pinMode(ERROR_LED_PIN, OUTPUT);
	pinMode(CHIP_SELECT, OUTPUT);
	
	NewSerial.begin(BAUD_RATE);
	NewSerial1.begin(BAUD_RATE);
	NewSerial2.begin(BAUD_RATE);
	
	if (!sd.begin(CHIP_SELECT)) {
		errorBlink(ERROR_INIT);
	}

	// Create new TX file on SD card
	char TxFile[] = "GR_TX00.csv";
	for (uint8_t i = 0; i <100; i++){
		TxFile[5] = i/10 + '0';
		TxFile[6] = i%10 + '0';
		if (! sd.exists(TxFile)){
			// Only open new file if it doesn't exist
			if (!logfile.open(TxFile, O_WRITE | O_CREAT | O_AT_END)){
			errorBlink(ERROR_OPEN);}
			strncpy(TxFileName,TxFile,sizeof(TxFile));
			break;  //leave loop
		}
	}	
	
	if (logfile.fileSize() == 0) {
		// Make sure first cluster is allocated.
		logfile.write((uint8_t)0);
		logfile.rewind();
		logfile.sync();
	}
	
	// Create new RX file on SD card
	// Close the TX file first
	logfile.close();
	char RxFile[] = "GR_RX00.csv";
	for (uint8_t i = 0; i <100; i++){
		RxFile[5] = i/10 + '0';
		RxFile[6] = i%10 + '0';
		if (! sd.exists(RxFile)){
			// Only open new file if it doesn't exist
			if (!logfile.open(RxFile, O_WRITE | O_CREAT | O_AT_END)){
			errorBlink(ERROR_RXOPEN);}
			break;  //leave loop
		}
	}
	
	if (logfile.fileSize() == 0) {
		// Make sure first cluster is allocated.
		logfile.write((uint8_t)0);
		logfile.rewind();
		logfile.sync();
	}
	
	// Close the Rx file
	logfile.close();	
	
	// Connect to the RTC
	Wire.begin();
	if (!RTC.begin()){
		// Save to Tx file
		logfile.open(TxFile, O_WRITE);
		logfile.println("RTC Failed");
		logfile.close();
		errorBlink(ERROR_RTC);
		
		// Save to Rx file
		logfile.open(RxFile, O_WRITE);
		logfile.println("RTC Failed");
		logfile.close();
		errorBlink(ERROR_RTC);
	}
	
	// Get RTC time at start up
	DateTime now;
	
	// Fetch the current time
	now = RTC.now();
	// Log the time
	//logfile.print(now.secondstime()); // seconds since 2000
	//logfile.print(",");
	logfile.open(TxFile, O_WRITE);
	logfile.print(now.year(), DEC);
	logfile.print("/");
	logfile.print(now.month(), DEC);
	logfile.print("/");
	logfile.print(now.day(), DEC);
	logfile.print(" ");
	logfile.print(now.hour(), DEC);
	logfile.print(":");
	logfile.print(now.minute(), DEC);
	logfile.print(":");
	logfile.print(now.second(), DEC);
	logfile.println("");
	logfile.close();
	// Other File
	logfile.open(RxFile, O_WRITE);
	logfile.print(now.year(), DEC);
	logfile.print("/");
	logfile.print(now.month(), DEC);
	logfile.print("/");
	logfile.print(now.day(), DEC);
	logfile.print(" ");
	logfile.print(now.hour(), DEC);
	logfile.print(":");
	logfile.print(now.minute(), DEC);
	logfile.print(":");
	logfile.print(now.second(), DEC);
	logfile.println("");
	logfile.close();
	
	// DEBUGGING: Print out to the serial port to see what is going the time is.
//
	//NewSerial.print(now.year(), DEC);
	//NewSerial.print("/");
	//NewSerial.print(now.month(), DEC);
	//NewSerial.print("/");
	//NewSerial.print(now.day(), DEC);
	//NewSerial.print(" ");
	//NewSerial.print(now.hour(), DEC);
	//NewSerial.print(":");
	//NewSerial.print(now.minute(), DEC);
	//NewSerial.print(":");
	//NewSerial.print(now.second(), DEC);
	//NewSerial.println("");

	
	
}


//------------------------------------------------------------------------------
// Time of last sync call.
uint32_t txSyncTime = 0;
uint32_t rxSyncTime = 0;


uint8_t buf[32];

void loop() {
	
	DateTime now;
	uint8_t n=0;
	uint8_t m=0;
	 
	
// Get the Garmin TX traffic	
	if (NewSerial1.getRxError()) {
		errorBlink(ERROR_SERIAL);
	}
	
	n = NewSerial1.read(buf, sizeof(buf));
	
	if (n > 0) { // If there is something in the buffer
		
		// Need to open the Ts file to write out the data
		//logfile.open(TxFileName, O_WRITE);
[b]			if (!logfile.open(TxFileName, O_WRITE)){
				NewSerial.println(TxFileName);
			errorBlink(ERROR_LOOP_OPEN);}[/b]		
		
		// Attempt to log the correct time stamp to file
		now = RTC.now();
		logfile.timestamp(T_WRITE, now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
		
		// Commented this out 3/13/14 to try to get the HEX values saved to the file.
		//if (logfile.write(buf, n) != n) {
		//errorBlink(ERROR_WRITE);
		//}
		
		for (int i=0; i<sizeof(buf); i++){
			if (buf[i]<0x10) {logfile.print("0");
			}
			logfile.print(buf[i],HEX);
			logfile.print(" ");
		}
		
		// Get the current micros time
		logfile.print(",");
		logfile.println(micros());
				
	// Don't sync if active.
	return;
}
if ((millis() - txSyncTime) < MAX_TXSYNC_TIME_MSEC) return;

if (!logfile.sync()) {
	//NewSerial.println(TxFileName);
	NewSerial.println("can't sync to the Tx file");
	errorBlink(ERROR_TXWRITE);
}
txSyncTime = millis();

logfile.close();

// Log the RX side *****************************************************************
// Get the Garmin RX traffic
if (NewSerial2.getRxError()) {
	errorBlink(ERROR_SERIAL);
}

m = NewSerial2.read(buf, sizeof(buf));

if (m > 0) { // If there is something in the buffer
	
	logfile.open(RxFileName, O_WRITE);
	// Attempt to log the correct time stamp to file
	now = RTC.now();
	logfile.timestamp(T_WRITE, now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
	
	// Commented this out 3/13/14 to try to get the HEX values saved to the file.
	//if (logfile.write(buf, n) != n) {
	//errorBlink(ERROR_WRITE);
	//}
	
	for (int i=0; i<sizeof(buf); i++){
		if (buf[i]<0x10) {logfile.print("0");
		}
		logfile.print(buf[i],HEX);
		logfile.print(" ");
	}
	
	// Get the current micros time
	logfile.print(",");
	logfile.println(micros());
	
	// Don't sync if active.
	return;
}
if ((millis() - rxSyncTime) < MAX_RXSYNC_TIME_MSEC) return;

if (!logfile.sync()) {
	NewSerial.println("can't sync to the Rx file");
	errorBlink(ERROR_RXWRITE);
}
rxSyncTime = millis();

logfile.close();
}

The Serial output looks like:

Opening port
Port open
GR_TX07.csv
8
8
8
8
8
Port closed

I looked at the file name to be sure that was being saved out to the global variable like it should be and it is. So if I am looking at the SD card and trying to open a specific file name why would it fail?