.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
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.
|
|
|
|
|
|
|
Public Methods
|
Name
|
Description
|
|
Flags an ExtendedWeakReference object as a candidate for recovery after the device reboots or powers down and restarts.
|
|
|
|
|
|
Attempts to recover a specific ExtendedWeakReference object, and creates a new instance of this class if the recovery attempt fails.
|
分享到:
相关推荐
The Microsoft .NET Micro Framework is a small and efficient .NET runtime environment used to run managed code on devices that are too small and resource constrained for Windows CE and the Compact ...
1. **编译器**:这个SDK内含了专门针对.NET Micro Framework优化的C#编译器,使得开发者可以使用C#语法编写代码,同时保持与标准.NET Framework的兼容性。 2. **类库**:NETMF提供了一套丰富的类库,这些类库包括...
《Microsoft .NET Micro Framework嵌入式编程》是美国作者汤普逊所著的一本深入探讨.NET Micro Framework(简称.NET MF)技术的专业书籍。这本书旨在帮助读者理解和掌握如何在嵌入式系统中应用.NET Micro Framework...
《玩转.NET Micro Framework移植-基于STM32F10x处理器》这本书的主题聚焦于将.NET Micro Framework(简称.NET MF)应用到STM32F10x系列微控制器上,这是一个涉及嵌入式系统开发、C#编程以及硬件平台集成的重要技术...
**Microsoft .NET Micro Framework 2.5 SDK** 微软的.NET Micro Framework(简称.NET MF)是一个专为嵌入式设备设计的轻量级.NET运行时环境,它允许开发者使用C#和.NET编程语言来构建和控制低功耗、资源受限的硬件...
1. **评估硬件需求**:首先需要评估目标硬件平台的性能指标是否满足.NET Micro Framework的要求,包括处理器类型、内存大小、存储空间等。 2. **配置HAL**:根据目标硬件的具体特性配置HAL,确保HAL能正确识别并控制...
11. **案例研究**:书中可能包含实际的项目案例,展示了.NET Micro Framework在不同应用场景中的应用,如智能家居设备或医疗设备。 通过阅读《Expert .NET Micro Framework》,开发者可以掌握在资源受限环境中使用...
.NET Micro Framework(简称.NET MF)是微软开发的一个轻量级.NET框架,主要针对嵌入式设备和物联网(IoT)应用。它提供了基础的.NET编程模型,使得开发者能够使用C#、VB.NET或其他.NET语言来编写高效能的嵌入式程序。...
### .NET Micro Framework Key文件部署说明 #### 一、概述 .NET Micro Framework(.NET MF)是一种用于构建和运行小型嵌入式设备上的应用程序的框架。它为开发人员提供了类似于桌面版.NET Framework的功能,但针对...
expert .net micro frameworkexpert .net micro frameworkexpert .net micro framework
### GHI .NET Micro Framework 培训教材知识点解析 #### 一、关于教材与目标受众 本教材《GHI .NET Micro Framework培训教材》专为初学者设计,旨在介绍.NET Micro Framework及其在嵌入式系统开发中的应用。教材...
.NET Micro Framework(简称.NET MF)是微软开发的一个嵌入式平台,主要面向资源有限的设备,如物联网(IoT)设备、智能家居控制器等。它提供了一个运行环境,使得开发者能够利用C#或VB.NET等高级语言进行编程,而无需...
.NET Micro Framework自动化测试工具,需.NET Micro Framework3.0和.NET Micro Framework Test Kit支持...
.NET Micro Framework(简称.NET MF)是微软公司推出的一个嵌入式操作系统平台,主要设计用于资源有限的设备,如物联网(IoT)中的智能硬件、工业自动化设备和消费电子等产品。红牛开发板是一款基于.NET Micro ...
.NET Micro Framework是微软开发的一个轻量级的.NET运行环境,特别设计用于资源有限的嵌入式设备。这个框架首次被引入到ADI(Analog Devices, Inc.)公司的Blackfin处理器上,为开发者提供了一种新的、高效的方式来...
这是我自己改造的.Net Micro Framework模拟器,比windows自带的漂亮多了。 覆盖该目录下的同名文件(路径以实际开发包安装位置为准)即可。 C:\Program Files\Microsoft .NET Micro Framework\v2.0.3036\Tools 支持...