I am an electronics graduate and an embedded design engineer, working in this field for about two years apart from the hobby time during my graduation. I am currently working on a system that has an Atmega2560 as the core controller with an arduino bootloader, we are using an 8GB memory card to save the log data from our system and send it to our server at regular intervals of time. The system worked fine however some of them are facing issues with the writing of data, some garbage value are getting stored in the SD card. I have attached the schematic and code snippet for the same, please let me know if there is any problem with the system and how can i resolve it, please let me know if any other things are required for understanding the problem correctly.
Thank you in advance.
SD_Initialize();
if(!SD.exists(SDTransactionDirectory))
{
if(SD.mkdir(SDTransactionDirectory))
{
}
}
RTC_Date_Time();
String FileName = SDTransactionDirectory + "/" + RTCSDDate + RTCSDTime + ".txt";
SD_File = SD.open(FileName, FILE_WRITE);
if(SD_File)
{
delay(100);
digitalWrite(LED_SD, HIGH);
SD_File.println(GSMData);
SD_File.close();
}
The SD_Initialize() is a user function:-
void SD_Initialize() //initialise SD card
{
digitalWrite(SD_EN, HIGH);
digitalWrite(SD_CS, LOW);
delay(500);
if(SD.begin(SD_CS))
{
digitalWrite(LED_SD, HIGH);
}
else
{
digitalWrite(LED_SD, LOW);
}
}
The data stored in the filename is as follows:-
CLEnT|T|M008|\00Ÿûmï°]ŸëÜ£·zoòTÏæ{Œü[ŸýUô~ÛcCWY®ÞgÙë×<;wBÄgnìÇFÿÉ=XoÎ.êžÄf·®ö¯³ç~åeÝ]1_
ÏÀyÿÌÍ?ô"zBõ¬ñ
As you can see the initial values till "M008" is fine but its garbage after that, and the CLEnt|T|M008 is the start of the string after that a lot of other parameter information is to be stored which turns out to be garbage.
GSMData
aString
? You should avoidString
and allString
-based concatenation operations. Even on a Mega2560 with that little extra bit of SRAM it's still "iffy".CLEnT|T|M008|\0
; is the\0
an actual sequence of the two characters "\" and "0"? It may be coincidence, but it looks suspicious to see the escaped null char just before the "corrupt" data. Makes one think String termination problems.M008|
be the last value saved? In other words, what do you expect to see?