.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,如需转载请自行联系原作者

相关文章
|
3天前
|
云安全 人工智能 自然语言处理
|
7天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
714 17
|
10天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
749 57
Meta SAM3开源:让图像分割,听懂你的话
|
8天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
329 116
|
10天前
|
机器学习/深度学习 人工智能 自然语言处理
AgentEvolver:让智能体系统学会「自我进化」
AgentEvolver 是一个自进化智能体系统,通过自我任务生成、经验导航与反思归因三大机制,推动AI从“被动执行”迈向“主动学习”。它显著提升强化学习效率,在更少参数下实现更强性能,助力智能体持续自我迭代。开源地址:https://github.com/modelscope/AgentEvolver
495 37
|
23天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
2天前
|
Rust 安全
掌握Rust文件读取(从零开始的IO操作指南)
本教程手把手教你用Rust读取文件,涵盖`read_to_string`一次性读取和`BufReader`逐行高效读取,适合初学者掌握安全、高效的Rust文件操作,助你轻松入门系统编程。
149 113