Go Down

Topic: Does SerialPort library work with the Atmel Studio VisualMicro plug in? (Read 2211 times) previous topic - next topic

millerrt

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.

Quote
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
Arduino = Sweet...

PaulS

Quote
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:
Code: [Select]
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:
Code: [Select]
char TxFileName[13];
char RxFileName[13];



millerrt

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:

Code: [Select]
// 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:

Quote
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?
Arduino = Sweet...

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy