Pages: [1]   Go Down
Author Topic: sdin.eof() reading CSV error..  (Read 357 times)
0 Members and 1 Guest are viewing this topic.
London, ON
Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

With my DUE I am recieving encoder positions and writing the to an SD card via .csv.  Each entry contains 2 ints and 2 floats with commas and no spaces.

Now, I wish to read the created file with the Arduino and just display them through the Serial communication.  I am getting an error at:

Code:
if (!sdin.eof()) error("readFile")

It seems like the created file is not ending with an EOF which is triggering this error.

The code is large, so I will post the interface between writing and reading.

Writing

Code:
void loop()
{
  err = slaveEncoder - masterEncoder;
  analogWrite(EnA, setPWM(err)); // Write PWM value as the encoder count of the Master (controller)
 
  if (recordState  == true){
    int m;
    m = millis();
 
    if (m % LOG_INTERVAL == 0) {

      // use buffer stream to format line
      obufstream bout(buf, sizeof(buf)); 
     
      bout << m << ',' << ID << ',' << masterEncoder << ',' << slaveEncoder << endl;
      ID++;

    #if ECHO_TO_SERIAL
      cout << buf;
    #endif  // ECHO_TO_SERIAL

      // log data and flush to SD
      logfile << buf << flush;
     
      // check for error
      if (!logfile) error("!!WDF!!"); //Write Data Fail
     
      // don't log two points in the same millis
      if (m == millis()) delay(1);
    }
  } 
 
  if (Serial.available() > 0){
    command = Serial.read();
  if (command == 82){       //R - Record
    cout << pstr("Recording...") << endl;
    recordState = true;
    return;
  }
  else if (command == 84){  //T - Traverse
    cout << pstr("Stopped...") << endl;
    logfile.close();
    readFile();
    recordState = false;
    return;
  }
  else if (command == 83){  //S - Stop
    cout << pstr("Stopped...") << endl;
    logfile.close();
    recordState = false;
    return;
  }
  else if (command == 88){  //X - Exit
    logfile.close();
    cout << pstr("Done!");
    while (1);
  }
  else return;
  }
}

Reading

Code:
void readFile() {
  int read_millis;
  int read_ID;
  float read_ME, read_SE;
  char comma1, comma2, comma3;
 
  // open input file
  ifstream sdin(fileName);
 
  // check for open error
  if (!sdin.is_open()) error("open");
 
  // read until input fails
  while (sdin >> read_millis >> comma1 >> read_ID >> comma2  >> read_ME >> comma3 >> read_SE) {
   
    // error in line if not commas
    if (comma1 != ',' || comma2 != ',' || comma3 != ',') error("comma");
   
    // print in six character wide columns
    cout << read_millis << setw(6) << read_ID << setw(6) << read_ME << setw(6) << read_SE << endl;
  }
 
  // Error in an input line if file is not at EOF.
  if (!sdin.eof()) error("readFile");
}

If anyone can suggest a reason why I cannot read the file properly, I would really appreciate a tip smiley

Daniel
Logged

Dialup is the future.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am getting an error at:
What error?

Code:
      // log data and flush to SD
      logfile << buf << flush;
     
      // check for error
      if (!logfile) error("!!WDF!!"); //Write Data Fail
Make sure that you can write to the file, after you write to it. Hmmm.

If you remove the SD card from the Arduino, and open the file on the PC, is there an incomplete line in the file, or a blank line?

How much of the file is read/not read before the error happens?
Logged

London, ON
Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah!  Stupid me, I appologize for the question... I also have a write in the setup() as a header.  I forget the my reading function only takes care of 2 ints and 2 floats in sequence with commas.

Thank you for leading me to solve my issue.

Daniel
Logged

Dialup is the future.

Pages: [1]   Go Up
Jump to: