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

简介:

.Net Micro Framework不支持文件系统(目前该项功能正在研发之中),所以无法像Windows和windows ce平台那样把需要永久保存的数据保存到文件之中。内存中保存的数据只要系统一掉电,所有的数据也都消失了,这对一些需要保存参数的应用来说真是不妙。
这几天在研究MF触摸屏功能时就遇到该问题,因为触摸屏校准之后,需要保存校准后的参数,否则MF一重启,难道还需要重新校准不成?
 感谢Donald Thompson 和 Rob S. Miles,从他们的大作上找到了问题的解决办法。办法就是把对象保存到Flash(EEPROM)中(有点像对象的二进制序列化)。
下面是我整理的示例代码(实现比较简单,但总觉得不太正规,不知道能存多大,也搞不清楚数据到底存放在什么位置了。):
 

 
  1. using System;  
  2. using Microsoft.SPOT;  
  3. using System.Collections;  
  4. using System.Threading;  
  5.    
  6. namespace DataStorage  
  7. {  
  8.     public class Program  
  9.     {         
  10.         public static void Main()  
  11.         {  
  12.             FlashDatas fd = FlashDatas.Load();  
  13.             fd.dump();  
  14.             fd.Flag = "adfg";  
  15.             //fd.Items.Clear();                  
  16.             //fd.AddItem(new FlashData(55, "1aaa"));  
  17.             //fd.AddItem(new FlashData(66, "2bbb"));  
  18.             //fd.AddItem(new FlashData(77, "3ccc"));  
  19.             //fd.AddItem(new FlashData(88, "4ddd"));  
  20.             fd.Save();  
  21.             Thread.Sleep(3000);               
  22.         }  
  23.    
  24.         [Serializable]  
  25.         private class FlashData  
  26.         {  
  27.             public DateTime date;  
  28.             public int iData;  
  29.             public string sData;  
  30.             public FlashData(int iData, string sData)  
  31.             {  
  32.                 date = DateTime.Now;  
  33.                 this.iData = iData;  
  34.                 this.sData = sData;  
  35.             }  
  36.    
  37.             public override string ToString()  
  38.             {  
  39.                 return date.ToString() + " " + iData.ToString() + " " + sData;   
  40.             }  
  41.         }  
  42.    
  43.         [Serializable]  
  44.         private class FlashDatas  
  45.         {  
  46.             public DateTime CreateTime = DateTime.Now;  
  47.             public string Flag = @"http://yfsoft.blog.51cto.com/";  
  48.             public ArrayList Items = new ArrayList();  
  49.    
  50.             public void dump()  
  51.             {  
  52.                 Debug.Print(CreateTime.ToString());  
  53.                 Debug.Print(Flag);   
  54.                 foreach (FlashData Item in Items)  
  55.                 {  
  56.                     if (Item != null)  
  57.                     {  
  58.                         Debug.Print(Item.ToString());  
  59.                     }  
  60.                 }  
  61.             }  
  62.             public void AddItem(FlashData Item)  
  63.             {  
  64.                 Items.Add(Item);  
  65.             }  
  66.    
  67.             //调入数据  
  68.             public static FlashDatas Load()  
  69.             {  
  70.                 ExtendedWeakReference ewr = ExtendedWeakReference.RecoverOrCreate(  
  71.                         typeof(FlashDatas),                       //类型,任意类都可以,其名称起到一个索引作用  
  72.                         0,                                        //ID号,这个数据比较有用,不同ID号代表不同数据  
  73.                         ExtendedWeakReference.c_SurvivePowerdown);//该标志和.c_SurviveBoot 区别不大  
  74.                 ewr.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important;  
  75.    
  76.                 FlashDatas data = ewr.Target as FlashDatas;  
  77.                 if (data == null)  
  78.                 {  
  79.                     data = new FlashDatas();  
  80.                 }  
  81.                 return data;  
  82.             }           
  83.    
  84.             //保存数据  
  85.             public void Save()  
  86.             {  
  87.                 ExtendedWeakReference ewr = ExtendedWeakReference.RecoverOrCreate(typeof(FlashDatas), 0, ExtendedWeakReference.c_SurvivePowerdown);  
  88.                 ewr.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important;  
  89.                 ewr.Target = this;  
  90.             }  
  91.         }  
  92.     }  
  93. }  

以上代码在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.

 









本文转自yefanqiu51CTO博客,原文链接:http://blog.51cto.com/yfsoft/322911,如需转载请自行联系原作者

相关文章
|
Linux API Windows
.NET Core 获取操作系统各种信息
一.前言 .NET Core 内置了一些API供我们获取操作系统、运行时、框架等信息。这些API不是很常用,所有有些小伙伴可能还不知道,这里做一些可能用到的获取操作系统的API介绍二.判断操作系统 判断操作系统是否为 Linux OSX Windows,主要使用 System.
1293 0
|
存储
.Net Micro Framework研究—数据的永久存储
感谢Donald Thompson 和 Rob S. Miles,从他们的大作上找到了问题的解决办法。办法就是把对象保存到Flash(EEPROM)中(有点像对象的二进制序列化)。
432 0
.Net Micro Framework研究—应用实例
在前几篇关于.Net Micro Framework的研究文章中,我对它的绘图功能实不敢恭维,不过微软的MF开发人员很聪明,对位图方面的功能实现的就比较完善,这样做起图形应用来就不至于捉襟见肘了。前段时间用.Net Compact Framework实现了一个奥运场馆查询
528 0
.Net Micro Framework研究—串口操作
试验平台:Digi MF开发板,Digi提供的示例中包含了串口的示例程序
560 0
|
监控
.Net Micro Framework研究—串口部署释疑
前几天我用串口方式部署MF程序总是无法成功,但是用其自带的串口调试程序通信成功
540 0
|
机器人
.Net Micro Framework研究—用MF控制机器人
机器人研究一直是我很早以前的梦想,没有想到在深入研究.Net Micro Framework同时能和机器人搭上了联系。
532 0
【.Net Micro Framework PortingKit - 04】修改启动代码&重写向量表
在上三篇《移植初步:环境搭建》《STM3210E平台构建》《调试初步:点亮LED灯》文章中,我们介绍了如何搭建开发环境,并初步写了测试代码,下一步我们将根据Cortex-M3的架构特点,修改启动代码和重写中断向量表。
611 0
|
.NET C#
.Net Discovery系“.NET技术”列之-深入理解平台机制与性能影响 (中)
  上一篇文章中Aicken为大家介绍了.Net平台的垃圾回收机制与其对性能的影响,这一篇中将继续为大家介绍.Net平台的另一批黑马—JIT。有关JIT的机制分析   ● 机制分析以C#为例,在C#代码运行前,一般会经过两次编译,第一阶段是C#代码向MSIL的编译,第二阶段是IL向本地代码的编译。
1014 0
|
数据安全/隐私保护
艾伟:让.NET程序脱离.NET Framework框架运行
  Net 框架目前逐步在普及了,仍然有很多人在寻找如何让.Net程序脱离.NET框架的方法。   现成的工具有 Xenocode 的postbuidle或者vas,还有 Salamander .NET Linker 。
752 0
|
存储 XML .NET
如何让ASP.NET默认的资源编程“.NET研究”方式支持非.ResX资源存储
  之前写了两篇文章《.NET资源并不限于.ResX文件》(上篇、下篇),介绍了如何通过自定义ResourceManager的方式来扩展资源的存储形式。在那篇文章中,我定义了三种基于独立文件的ResourceManager(ResXResourceManager、BinaryResourceManager和XmlResoureManager)分别实现对.ResX,.Resource和.xml三种资源文件的访问。
749 0