Pages: [1]   Go Down
Author Topic: Problem in reading File contents into array.  (Read 748 times)
0 Members and 1 Guest are viewing this topic.
Bangalore, India
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino Serial Communication
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I'm trying to read the contents of the file inside a array.
For the first few lines it reads properly but after that it doesn't read the full line.

I'm attaching the file which i'm reading.

This is my code to read the file:

Code:
#include <SD.h>

///////// SD File Handler
File SD_File_Handler;

///////// Defining Global Constants
#define SD_CS 4
#define Meter_DB_File "meter_db.txt"
#define Config_File "config.txt"
#define Meter_DB_Max_Rows 40

///////// Defining Global Variables
uint8_t SD_Init = 0;uint8_t Config_Init = 0;uint8_t Meter_DB_Init = 0;
String Meter_DB_Rows[Meter_DB_Max_Rows];

void setup(){
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {;}
}
void loop(){
  // If SD Card is not Initialized
  if(SD_Init==0){
    Serial.println("Initializing SD");
    init_sdcard();
    delay(500);
  }
  // If SD Card is Initialized, but Configuration Parameters not initialized
  if(SD_Init==1 && Config_Init==0){
    Serial.println("Initializing Config Variables");
    init_config();
    delay(500);
  }
  // If Configuration Parameters is Initialized
  if(Config_Init==1) {
    // If Meter DB File not Initialized
    if(Meter_DB_Init==0){
      Serial.println("Initialize Meters Details From DB File");
      read_meter_db();
      delay(100);
    }
    //Serial.println(Meter_DB_Rows[16]);
    for (int k = 1; k < Meter_DB_Max_Rows; k++){
      if(Meter_DB_Rows[k].length()!=0) {
        //Serial.println(Meter_DB_Rows[k]);
        //explode(Meter_DB_Rows[k],",");
        delay(100);
        if(Meter_DB_Rows[k].length()!=0) {
          process_meter(Meter_DB_Rows[k]);
          delay(100);
        }
      }
    }
  }
}


///////// Initializing SD Card /////////////////
void init_sdcard() {
  pinMode(SD_CS, OUTPUT);
  if (SD.begin(SD_CS)) {
    SD_Init = 1;
  }
  else {
    SD_Init = 0;
  }
}

void init_config() {
  Config_Init=1;
}

void read_meter_db() {
  /// opening file
  SD_File_Handler = SD.open(Meter_DB_File);
  if (SD_File_Handler) {
    int i=0;
    char row_ch;
    String temp[]="";
    while (SD_File_Handler.available()) {
      ////// Reading contents of the file
      row_ch=SD_File_Handler.read();
      ////// Checking if end of line
      if(row_ch=='\n'){
        i++;
      }
      else {
        Meter_DB_Rows[i]=Meter_DB_Rows[i]+row_ch;
      }
    }
    Meter_DB_Init=1;
    SD_File_Handler.close();
  }
  else {
    Meter_DB_Init=0;
  }
}

void process_meter(String Meter_Details) {
  Serial.println(Meter_Details);
}



The output which i get is :

Initializing SD
Initializing Config Variables
Initialize Meters Details From DB File
1,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
2,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
3,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
4,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
5,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
6,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
7,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
8,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
9,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
10,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
19,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
27,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
29,0,0,FLOAT,0.0,286,2,INT,0,0.1,0
0
0
0


Can somebody please help me??

Thanks

* meter_db.txt (1.18 KB - downloaded 12 times.)
Logged

Gabriel Jeremy Jeldo
meetjeremy@gmail.com

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can somebody please help me??
You are trying to store the contents of the whole file in memory. With a UNO with 2K of SRAM, the SD class uses a 512 byte buffer, leaving 1500 bytes for the stack, the heap, literal strings, and your array of String objects. That may not be near enough for 40 lines in the file.

http://playground.arduino.cc/Code/AvailableMemory
Logged

Bangalore, India
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino Serial Communication
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello PaulS,

I forgot to mention i'm using Leonardo.

Thanks for the reply.

So is there any way to process the contents of the file.

Actually i want to update the kW and kWh values in the file.

I was thinking to make a copy in the memory update the values from the meter and then overwrite it back to the file.

Seems it can't work. Do you have any other way to do this??

Thanks
« Last Edit: September 01, 2013, 11:04:53 am by meetjeremy » Logged

Gabriel Jeremy Jeldo
meetjeremy@gmail.com

Idaho, US
Offline Offline
God Member
*****
Karma: 19
Posts: 859
Special User
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Leonardo has only 2.5KB. Probably not enough, especially when using the memory hog String class. You probably want to read and then write one string at a time, each in turn, rather than all of them at once.
Logged

Pages: [1]   Go Up
Jump to: