Share via


20, .Net Micro Framework研究—数据的永久存储

       作者: 刘洪峰

 

.Net Micro Framework不支持文件系统(目前该项功能正在研发之中),所以无法像Windows和windows ce平台那样把需要永久保存的数据保存到文件之中。内存中保存的数据只要系统一掉电,所有的数据也都消失了,这对一些需要保存参数的应用来说真是不妙。

这几天在研究MF触摸屏功能时就遇到该问题,因为触摸屏校准之后,需要保存校准后的参数,否则MF一重启,难道还需要重新校准不成?

 感谢Donald Thompson 和 Rob S. Miles,从他们的大作上找到了问题的解决办法。办法就是把对象保存到Flash(EEPROM)中(有点像对象的二进制序列化)。

下面是我整理的示例代码(实现比较简单,但总觉得不太正规,不知道能存多大,也搞不清楚数据到底存放在什么位置了。):

using System;

using Microsoft.SPOT;

using System.Collections;

using System.Threading;

 

namespace DataStorage

{

    public class Program

    {      

        public static void Main()

        {

            FlashDatas fd = FlashDatas.Load();

            fd.dump();

            fd.Flag = "adfg";

            //fd.Items.Clear();               

            //fd.AddItem(new FlashData(55, "1aaa"));

            //fd.AddItem(new FlashData(66, "2bbb"));

            //fd.AddItem(new FlashData(77, "3ccc"));

            //fd.AddItem(new FlashData(88, "4ddd"));

            fd.Save();

            Thread.Sleep(3000);            

        }

 

        [Serializable]

        private class FlashData

        {

            public DateTime date;

            public int iData;

            public string sData;

            public FlashData(int iData, string sData)

            {

                date = DateTime.Now;

                this.iData = iData;

                this.sData = sData;

            }

 

            public override string ToString()

            {

                return date.ToString() + " " + iData.ToString() + " " + sData;

            }

        }

 

        [Serializable]

        private class FlashDatas

        {

            public DateTime CreateTime = DateTime.Now;

            public string Flag = @"https://blog.csdn.net/yefanqiu/";

            public ArrayList Items = new ArrayList();

 

            public void dump()

            {

                Debug.Print(CreateTime.ToString());

                Debug.Print(Flag);

                foreach (FlashData Item in Items)

                {

                    if (Item != null)

                    {

                        Debug.Print(Item.ToString());

                    }

                }

            }

            public void AddItem(FlashData Item)

            {

                Items.Add(Item);

            }

 

            //调入数据

            public static FlashDatas Load()

            {

                ExtendedWeakReference ewr = ExtendedWeakReference.RecoverOrCreate(

                        typeof(FlashDatas), //类型,任意类都可以,其名称起到一个索引作用

                        0, //ID号,这个数据比较有用,不同ID号代表不同数据

                        ExtendedWeakReference.c_SurvivePowerdown);//该标志和.c_SurviveBoot 区别不大

                ewr.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important;

 

                FlashDatas data = ewr.Target as FlashDatas;

                if (data == null)

                {

                    data = new FlashDatas();

                }

                return data;

            }        

 

            //保存数据

            public void Save()

            {

                ExtendedWeakReference ewr = ExtendedWeakReference.RecoverOrCreate(typeof(FlashDatas), 0, ExtendedWeakReference.c_SurvivePowerdown);

                ewr.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important;

                ewr.Target = this;

            }

        }

    }

}

以上代码在Digi开发板上测试成功,断电之后,再上电,保存的数据确实没有丢失。

MSDN中相关函数的说明如下:

ExtendedWeakReference Members

The following tables list the members exposed by the ExtendedWeakReference type.

Public Constructors

  Name Description
ExtendedWeakReference Initializes a new instance of the ExtendedWeakReference class, referencing a specified object.

Public Fields

  Name Description
  c_SurviveBoot Contains a flag specifying that the current weak reference will be recoverable after the device reboots.
  c_SurvivePowerdown Contains a flag specifying that the current weak reference will be recoverable after the device powers down and restarts.

Public Properties

  Name Description
Flags Gets the flags specifying the states from which the current weak reference should be recoverable.
Id Gets the ID associated with the current ExtendedWeakReference object.
Priority Gets or sets the priority level for the current ExtendedWeakReference object.
Selector Gets the selector for the current ExtendedWeakReference object.

Public Methods

  Name Description
PushBackIntoRecoverList Flags an ExtendedWeakReference object as a candidate for recovery after the device reboots or powers down and restarts.
  Recover Recovers a specific ExtendedWeakReference object.
  RecoverOrCreate Attempts to recover a specific ExtendedWeakReference object, and creates a new instance of this class if the recovery attempt fails.

Comments