叶帆工作室

嵌入式开发爱好者(十年开发经验,精通C/C++/VC/VB/C#...)

刘洪峰ID:yefanqiu
504430次访问,排名80好友0人,关注者137
微软MVP / CSDN 2008十大MVB/MSDN中文技术论坛版主
yefanqiu的文章
原创 215 篇
翻译 0 篇
转载 3 篇
评论 1057 篇
叶帆的公告
本博客原创文章,作者保留一切权利,需经作者同意后方可转载,转载时 请注明[叶帆工作室]及文章链接。yefan@vip.sina.com
【简介】叶帆[微软MVP]
【文章】叶帆文章列表
【软件】叶帆共享软件列表
最近评论
yefanqiu:知道这个软件,不过更深层次知识我目前也不清楚。
吴为仁:请问你熟悉神奇的Reflector软件吗?用它可以得到.NET的源码。请问Reflector输出的源码与真正的源码有什么区别?要注意那些问题?您这个MVP写一点此方面的东西,可否?
yefanqiu:在C#上直接调用该控件吧。
yefanqiu:这是支持两种不同字符集的函数(ANSI/Unicode)
jingang123gz:我用C#怎么写代码捏???
文章分类
收藏
    相册
    叶帆照片
    【叶帆软件】
    [01]VB源码之友(V2.1.548)
    [02]API浏览器.net(V5.0)
    [03]叶帆成语词典(V2.0.8)
    [04]叶帆密码库(V1.2.8)
    【叶帆资源】
    DAO 2.0引擎
    叶帆快速通道
    Windows Embedded 专题
    中文MSDN
    叶帆圈子--工业自动化
    叶帆工作室(博客园)
    叶帆工控--工业自动化
    叶帆群组--工业应用开发
    微软中文技术论坛
    瑞康社区论坛
    叶帆友情链接
    张欣
    枕善居
    莫依
    葛涵涛
    郑建
    陈辉
    马宁
    马骐
    魏涛序
    黎波
    存档
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 .Net Micro Framework研究—数据的永久存储收藏

    新一篇: .Net Micro Framework研究—实现SideShow窗体界面 | 旧一篇: 用模拟器加载基于ARM平台的WinCE6.0 内核(NK.bin)

     
    .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 = @"http://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
    Initializes a new instance of the ExtendedWeakReference class, referencing a specified object.
    Public Fields
     
    Name
    Description
     
    Contains a flag specifying that the current weak reference will be recoverable after the device reboots.
     
    Contains a flag specifying that the current weak reference will be recoverable after the device powers down and restarts.
    Public Properties
     
    Name
    Description
    Gets the flags specifying the states from which the current weak reference should be recoverable.
    Gets the ID associated with the current ExtendedWeakReference object.
    Gets or sets the priority level for the current ExtendedWeakReference object.
    Gets the selector for the current ExtendedWeakReference object.
    Public Methods
     
    Name
    Description
    Flags an ExtendedWeakReference object as a candidate for recovery after the device reboots or powers down and restarts.
     
    Recovers a specific ExtendedWeakReference object.
     
    Attempts to recover a specific ExtendedWeakReference object, and creates a new instance of this class if the recovery attempt fails.
     
     

    发表于 @ 2008年01月21日 21:20:00|评论(loading...)|编辑

    新一篇: .Net Micro Framework研究—实现SideShow窗体界面 | 旧一篇: 用模拟器加载基于ARM平台的WinCE6.0 内核(NK.bin)

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 叶帆