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();