Go Down

Topic: Controller Hang whenever execute both functions(Parsing File and Writing File) (Read 890 times) previous topic - next topic

Cybernetician

I am trying to create database by using JSON and SD library.
But i stuck in a problem "Controller hanging" after removing lot of stuff to make Memory Efficent Solution still i happens again.

Specifications
Flash 128k
SRAM 16k

Hardware
Offical Arduino Ethernet Shield
Zigduino board

To debug problem four tests were perform

Test 1 Only Parsing funtion Call
Status Ok
Test 2 Only Writng funtion Call
Status Ok
Test 3 Only Parsing funtion Call
Status Ok
Test 4 Both funtion Call
Status Hang

This the code and outputs of all tests attached .Status ok in zip file due limitation of 4 files

Code: [Select]

#include <aJSON.h>
#include <SD.h>

File myFile;

char* readFile(char* file)
{
 Serial.println(FreeRam(),DEC);
 char buf[30];
 char strFile[600];
 int16_t  readed;

 myFile = SD.open(file,FILE_READ);
 if(myFile)
 {
   readed = myFile.read(buf,30);
   buf[readed] = '\0';
   String stringOne =  String(buf);
   while( readed > 0) {
     readed = myFile.read(buf,30);
     buf[readed] = '\0';
     stringOne += String(buf);
   }
   myFile.close();
   stringOne.toCharArray(strFile,stringOne.length()+1);
   //Serial.println(strFile);
   return strFile;
 }
 else
   Serial.println("Fail to open file");
}
void parseFile(char* object)
{
 Serial.println(FreeRam(),DEC);
 char* pch =readFile("test.txt");
 Serial.println(pch);
 aJsonObject* root = aJson.parse(pch);
 
 if (root != NULL) {
   Serial.println(F("Parsed successfully 1 "));
   aJsonObject* query = aJson.getObjectItem(root, object);
   
   char *json_String=aJson.print(query);
   
   Serial.println(json_String);
   Serial.println(F("**"));
   free(json_String);
   free(pch);
 }
 else
   Serial.println(F("Parsed unsuccessfully 1"));
 aJson.deleteItem(root);
}

void writeDb(char* object)
{
 char* buff = readFile("test.txt");
 //Serial.println(buff);
 Serial.println(FreeRam(),DEC);
 Serial.println(F("HANG POINT"));
 aJsonObject* root = aJson.parse(buff);

 if (root != NULL) {
   Serial.println(F("writdb successfully 1 "));

   aJsonObject *tmp = aJson.createObject();
   aJson.addStringToObject(tmp, "value1","1101");
   aJson.addStringToObject(tmp, "value2","1102");
   aJson.addStringToObject(tmp, "value3","1103");
   aJson.replaceItemInObject(root, object, tmp);

   SD.remove("test.txt");
   delay(100);
   myFile = SD.open("test.txt", FILE_WRITE);

   // if the file opened okay, write to it:
   if (myFile) {
     char* json_String = aJson.print(root);
     
     myFile.print(json_String);
     myFile.close();
     Serial.println(F("FileClosed"));
     Serial.println(F("WR**"));

     //aJson.deleteItem(tmp);
     free(json_String);
     free(buff);
   }
   else
     Serial.println(F("File Not Truncate"));
 }
 else
   Serial.println(F("wrtidb unsuccessfully 1 "));
 aJson.deleteItem(root);
}
void setup() {
 Serial.begin(9600);

 pinMode(SS, OUTPUT);                       // set the SS pin as an output (necessary!)
 digitalWrite(SS, HIGH);                    // but turn off the W5100 chip!
 if (!SD.begin(4)) {
   Serial.println(F("initialization failed!"));
   return;
 }
 Serial.println(FreeRam(),DEC);
 parseFile("config0");
 Serial.println(F("Parse done"));
 writeDb("config0");
 Serial.println(F("Write done"));
 Serial.println(FreeRam(),DEC);
}

void loop() {
 // Nothing to do
 ;
}



I really need suggestion because after playing 10 hours with this now i am in Blackout Stage. 8)
From Idea To Invention

Nick Gammon

Quote
Code: [Select]
    String stringOne =  String(buf);


Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

Cybernetician

Thnx Nick Gammon

Quote
I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).


Ok.  One more question
Is this is a way about which you are suggesting? 
Code: [Select]

  myFile = SD.open("config2.txt", FILE_READ);

  Serial.println(myFile.size());
  char* buf = (char*) malloc(myFile.size());
  readed = myFile.read(buf,myFile.size());
  Serial.println(readed);
  myFile.close();
From Idea To Invention

Nick Gammon


Cybernetician

From Idea To Invention

Go Up