RE : Assigning a number to a FEZ Cobra

Hi, I would like to save a value to my Cobras eeprom. For example, I would like to give it a value of 1 in relation to the room that it is in and I want this value to stay on the cobra after power down. Thanks

Have a look for Extended Weak Reference

1 Like

EWR may be huge an complex for such kind of single infiormation. In your case, I would use battery ram.

Store it on an SD card?

I see, do you think you could direct me to an example of how to call up the battery ram?Would be great.

And, I’m trying to avoid using an SD card at the moment, but will use as last resort.

If you do decide to go the SD route, the flash memory module might be another option you want to consider.

Battery RAM probably isn’t the best bet for it since you could still lose th number if the battery fails.

EWR really isn’t that complex; I use it all the time to store touch calibration data.

The Cobra doesn’t have a Gadgeteer socket…

@ GMod(Errol) - Ah of course, that is a good point :slight_smile: I will keep quite and go get another cup of coffee…

He’s being the headmaster - i got a telling off as well :smiley:

You 're right BatteryRam Data can be lost if battery fails (I hope it will work during 3-5 year) whereas this seems not to be a problem for EWR.

In practice and don’t even know how it is possible, but I have observed some devices that can’t retrieve data from the EWR. The bad thing is that it used to happened randomly. (To be honest it makes a big while that we did not observe this phenomenon)

Well if you change namespaces or the structure of the EWR that you’re working with you’ll no longer be able to reach it. Otherwise I’ve never seen it fail.

If the ID is super critical then perhaps it could be stored EWR as well as battery RAM. Redundancy is good.

Thanks for the replies.However, I am a bit lost on how to use EWR or the battery RAM. Would love some advice for a beginner!

I have observed EWR failure several times while deploying and developping (manly due to modification in EWR structure even if in many time I was practically sure that no change was made in the class instance subject to EWR.

One time it happens while the device was running since a month…

To answer to the initial question, according to me : EWR is the main solution, BatteryRam the second option (quick and dirty :slight_smile: ).

EWR consist in “serializing” your data and storing it in eeprom while Battery Ram is as its name indicates a Ram powered by a battery (usually a 3.V coin cell).

EWR: .NET Micro Framework - FAQ

Battery Ram:

Leforban’s example is a good one for you blue123.

@ leforban - I’ve seen that talked about on the forum but I thought that was mostly due to not giving enough time between writing to the EWR and rebooting. Maybe I’m wrong. I personally have never had issue and have been using it for about 3 years now to store calibration data.

Ah thanks. But, is the extended weak reference assembly in the microsoft SDK folder? I cant seem to find it anywhere in there. Im using the 4.1.Net framework…At the moment the ewr code had several errors due to the fact that this assembly is missing. Thanks.

The documentation for the ExtendedWeakReference class tells you the reference to use (assembly) and the namespace to use with the using statement.

Right, added Micrsoft SPOT as namespace, and also added an Microsoft.SPOT.Native as an assembly. This is the code

using Microsoft.SPOT;

namespace """"""""

    public class Program

    private static ExtendedWeakReference settingsReference;
    private static AppSettings Settings;

        public static void Main()
        // Reads data from FLASH memory.
        // If no entry with AppSettings/0 identifier exists in memory, a new one is created.
        settingsReference = ExtendedWeakReference.RecoverOrCreate(typeof(AppSettings), 0, ExtendedWeakReference.c_SurvivePowerdown);

        // If the entry was just created, its value is set to null.
        if (settingsReferecnce.Target == null)
            Settings = new AppSettings();
            Settings = (AppSettings)settingsReference.Target; // EWR.Target is of type object

        Settings.RunCount++; // Change a settings value.

        settingsReference.Target = Settings; // This call writes the updated data into FLASH.

        Debug.Print("Number of starts: " + Settings.StartCount.ToString());


    public void SaveSettings()
        // It is enough to set the Target property in order to write the data into FLASH.
        settingsReference.Target = Settings;

    public void DeleteSettings()
        // Data recovering and storing must be kept in pairs.
        // Calling Recover without setting Target frees the FLASH completely from this EWR.
        ExtendedWeakReference.Recover(typeof(AppSettings), 0);

    public void RestoreLastSavedSettings() // Use this method to rewrite current settings using last saved data.
        // First, mark the stored data as unrecovered so we can Recover them.
        // Try to recover them, but do not create them if they do not exist.
        ExtendedWeakReference restoreReference = ExtendedWeakReference.Recover(typeof(AppSettings), 0);
        if (restoreReference != null && restoreReference.Target != null)
            Settings = (AppSettings)restoreReference.Target; // If they do, use them to refresh current settings.
            restoreReference.PushBackIntoRecoverList(); // Since we found the data, we have to put them back.
            Debug.Print("Could not restore settings.");

    [Serializable]  // If you want to store whole class, is has to be marked as serializable.
    private class AppSettings
        public string WelcomeMessage = "Small is beautiful";
        public int FontSize = 6;
        public int RunCount = 1;

As you can see my namespace is blanked out as Im not sure what to put in it. It is the only error I am recieiving. When I try to place a value as a namespace I get the following error message :

Error 3 ‘WeakReference.Program.AppSettings’ does not contain a definition for ‘StartCount’ and no extension method ‘StartCount’ accepting a first argument of type ‘WeakReference.Program.AppSettings’ could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\nzcznr\My Documents\Visual Studio 2010\Projects\Weak Reference\Program.cs 32 53 Weak Reference

You can not let your namespace empty, otherwise, the VS do not know what assembly takes part of your namespace or not and can not resolve naming.

Please check in your project properties what is the default namespace and report it in your code.

For more information on scope declaration and namespaces:

The reason for the error on line 32 regarding StartCount is that on line 32 you have

Debug.Print("Number of starts: " + Settings.StartCount.ToString());

Notice here that you are trying to access the StartCount on the Settings object. The settings object is of type AppSetting which is defined in your code as

 private class AppSettings
      public string WelcomeMessage = "Small is beautiful";
      public int FontSize = 6;
      public int RunCount = 1;

There is no member in the AppSettings class called StartCount and hence the reason for the error you are getting. Maybe you intended that to be RunCount?