SD Card failure

Hi GHI! We are a little company, and we produce Remote Terminal Units (RTU) based on your G120 modules. Our RTU gets some data from industrial devices, writes this data to internal SD card (cache) and then sends it to our server when network is available. RTU gets data every minute and writes it to SD. Each data entry has size 512 bytes. RTU writes all data in cycle in one file with max file size 1GB. We use 2GB SD cards from different manufactures (cheap noname cards and expensive branded cards) and on some our RTUs we have dead SD cards (read only), on some RTUs we have errors in SD file system. Time to SD failure always different, some of it - few weeks on other - few days. Some of RTUs work fine more than 3 month…

Please help us to solve this problem…

Here is main code of adding data entry to SD:


public void Add(CacheRecord value)
        {
            CacheEventArgs ea = new CacheEventArgs()
            {
                IsAddEvent = true,
                Record = value
            };

            try
            {
                value.Sequence = this.newPointer.Value;
                value.Status = CacheRecordStatus.New;
                byte[] buf = value.GetBytes();

                lock (locker)
                {
                    using (FileStream stream = new FileStream(directory + "cache", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
                    {
                        stream.Seek(newPointer.Value * this.bytesPerRecord, SeekOrigin.Begin);
                        stream.Write(buf, 0, buf.Length);
                        stream.Flush();
                        stream.Close();
                    }
                }

                this.newPointer.Inc();

                ea.IsSuccess = true;
            }
            catch (Exception e)
            {
                ea.IsSuccess = false;
                ea.Exception = e;
            }

            if (this.OnChange != null)
            {
                this.OnChange(ea);
            }
        }

SD Initialization:


 #if G120
            try
            {
                storage = new SDCard();
                storage.Mount();
                volume = VolumeInfo.GetVolumes()[0];
                this.isMounted = true;
                this.isFormatted = volume.IsFormatted;
                Thread.Sleep(1000);
            }
            catch (Exception e)
            {
                this.isMounted = false;
                this.isFormatted = false;
                LastException = e;
                ExceptionsCount++;
                throw e;
            }
 #endif

On high level I call FlushAll() after each data addition to SD:


VolumeInfo.GetVolumes()[0].FlushAll();

Did any of those cards have wear leveling? Scandisk Extreme Pro

My own experience is that SD cards fails when there power supply failure while writing/reading. This corrupts FAT file system and sometimes SD can’t be formatted anymore…

We add now in each of our product, a 5F supercap and detect power supply voltage, supercap lets us enough time to properly flush and unmount the file system.

@ Mr. John Smith
Unfortunately, I don’t know… Maybe this info was on the box, but I don’t remember…
Prompt me if I can check this by some software…

@ leforban
Not in our case… We have extremely stable power supply.

Thanks for suggestions

@ 4egod - Try only appending to the file and never truncating it.

@ Mr. John Smith -
Max cache size is 1GB it’s 2097152 data entries. RTU writes to SD one data entry per minute then we have about 4 years for one device (we have maximum 11 devices. I never truncate the file. When size of cache file reaches max size I write at beginning of the file.

Maybe I don’t know something about seek operation?

@ 4egod - We only had the same issues as @ leforban, loosing power while writing.
I use StreamWriter and WriteLine to append data + i keep 1 object open for each file i log data to.

@ David@ Leclanche -
Thank you, I’ll try it…

Stable power supply does not mean that it can’t be switched off

@ leforban -
You’re right, but boot counter tells us other…