SDCard troubles

One of our customers have return back his product due to troubles when reading xml on SDCard.

The product is based on EMX module. When I have connected the sdcard on Microsoft Vista, my compueter say that the device was dammaged and will try to repair sector. I have choose “no” and have seen files inside SD. One of them was supposed to be 133kB and this what was printed by windows. When I tried to open it with notepad++, files was empty. When I tried with vim, this last was returning “file corrupted”.

I decided to let vista repairing errors and after correction the file was just 127kB and the file was now readable but the last part of file disappear.

My feeling was that power supply of EMX has been switch off before the file was properly writing.

Yesterday I have try to redo experiment: switching off the power just after (between 0 and 5second) flushing closing and disposing the filestream. Each time I succeed to reproduce the error. On the contrary each time that I have wait 30 seconds before switching off the power supply files were proeprly written.

After investigating the forum, some posts advice to call unmount() and mount after writing to ensure data integrity, others advice to call Flushall().

Is there any difference?

How to be sure that power can be switch off safely?

Hi andre.m

This is an embedded product without hardware power management (backup battery or something like that). Customers just connect a switch for power supply. Shutdown can appear at any time (low battery or violent switch off). For low battery I can not do anything else than adding a backup battery to let time to the device to unmount file system… this will cost a lot and we will need to develop new board and so on… we forget this solution for the moment, for the second problem (customer that switch off the device) we should be able to provide a solution by indicating to the customers that the device is in writing process and that he has to wait before switching down the device.

Unounting mounting seems to be not blocking, what about flushall? how to know that data are properly written?

I was calling flush close and dispose on the filestream but this was not working. I have added now:

but I am not sure this will be enough

I use this with succes, check what files are open and close / dispose and flush / unmount :

    static void FreeSDcard()
            //check if logfile is open
            if (SDStorage == 1 && LogFile != null)

            //check if syslogfile is open
            if (SDStorage == 1 && SysLogFile != null)

            //check if debugfile is open
            if (SDStorage == 1 && DebugFile != null)

            //flush fat changes on SD storage
            if (VolumeInfo.GetVolumes()[0].Name == "SD") { VolumeInfo.GetVolumes()[0].FlushAll(); }
            if (VolumeInfo.GetVolumes().Length > 1)
                if (VolumeInfo.GetVolumes()[1].Name == "SD") { VolumeInfo.GetVolumes()[1].FlushAll(); }

            //check if card is mounted
            if (SDStorage == 1 && MySD != null)

            // flag SDstorage as unavailible
            SDStorage = 0;

            // and put led on to show card can be removed
            //error but where to write it...

Hello David

FlushAll seems to do the trick. I don’t know why but today I can not reproduce the problem easily whereas yesterday this was the case at each time.