Microsoft & .NET.NETExplore the Microsoft .NET Micro Framework

Explore the Microsoft .NET Micro Framework

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

What Is This?

Salute another player in the Microsoft’s .NET jungle: the .NET Micro Framework, formerly known as SPOT (Smart Personal Objects Technology). So, spot it on your development map. Initially announced about a couple of years ago and now already in version 2.5 (with v3.0 “kimono” on its way), it is Microsoft’s recipe for embedded device firmware development. It covers the # market niche where Microsoft Windows CE and the .NET Compact Framework have unacceptable overhead, that is, there are strict constraints for power, processor capabilities, and memory—Sensor Nodes, Aux displays, Health Monitoring, Remote Controls, and Robotics. Microsoft’s answer to such environment is the .NET Micro Framework, where you can develop your applications in C#.

The architecture of the .NET Micro Framework provides you with the tools necessary to develop your firmware as managed C# applications. It does this by making use of specialized versions of the .NET common language runtime (CLR) and the .NET Framework class library. That’s not ideal an solution for everyone as it often happens, but it does allow you to reuse your C# skills and work in the familiar environment, because the SDK is integrated seamlessly into MS Visual Studio. Funny enough, you can’t create native applications; the access to the native code is provided via the interop only!

Figure 1: .NET Micro Framework Architecture

To make the .NET Micro Framework available with the smallest possible memory footprint, it is designed to contain only those pieces of the .NET Framework that are most relevant to small devices (look at the architecture chart in Figure 1). These include the major portions of the System.Collections, System.Diagnostics, System.Globalization, System.IO, System.Reflection, System.Resources, System.Runtime, and System.Threading namespaces, among others. You can enjoy globalized language support, thread support, garbage collection, managed drivers for LCDs, Universal Asynchronous Receiver/Transmitter (USART), I2CTM, General Purpose Input/Output (GPIO), pulse-width modulation (PWM), graphics primitives, BMP and JPG image support, and text manipulation.

The integration with Visual Studio happens throughout the entire development process, from the creation of projects using .NET Micro Framework templates, through application development on the computer using the .NET Micro Framework extensible emulator, to downloading the applications to the device running .NET Micro Framework, and finally to debugging (in Visual Studio) the code that’s executing directly on the device. You may find more about .NET Micro Framework at the .NET Micro Framework page.

Build Your First Micro Framework Project

So, it’s time to create your first MF project:

Figure 2: Create New Project

As you see, you have four different templates to start with:

  • Class Library
  • Device Emulator
  • Console Application
  • Windows Application

Each type may be useful for your project, and you can even create your own device emulator! The Framework is shipped with the default one, so you begin with “Hello World” Window Application and run your code on that emulator. You can always select another device and transport it (for example, USB or Serial cable) via the “Micro Framework” page in project properties:

Figure 3: Micro Framework Page


Your sample window application has a simple structure. It looks exactly like another application type, the Console application, but allows you to create the window (hence it is “Window” application) and contains a class to handle hardware buttons input. When you glance at the code created by the wizard, the first thing that might catch your eye is the Microsoft.SPOT namespaces:

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;

As I said earlier, .NET Micro Framework is small but rich enough in terms of available functionality. The Object Browser snapshot can tell you more than any descriptions, although chosen names are intuitive enough:



Figure 4: .NET Micro Framework Assemblies


You should briefly walk through the code. Unlike a standard WinForms application, there are no forms here (you are in an embedded environment, after all), though it may remind you of the good old native Win32 application structure: create the window, create and register hardware input object, and finally run the ‘message loop’.


The Window creation procedure gives you the opportunity to demonstrate your creativity because it constructs all the elements one by one:

public Window CreateWindow()
{
// Create a window object and set its size to the
// size of the display.
mainWindow = new Window();
mainWindow.Height = SystemMetrics.ScreenHeight;
mainWindow.Width = SystemMetrics.ScreenWidth;

// Create a single text control.
Text text = new Text();

text.Font = Resources.GetFont(Resources.FontResources.small);
text.TextContent =
Resources.GetString(Resources.StringResources.String1);
text.HorizontalAlignment =
Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
text.VerticalAlignment =
Microsoft.SPOT.Presentation.VerticalAlignment.Center;

// Add the text control to the window.
mainWindow.Child = text;

// Connect the button handler to all of the buttons.
mainWindow.AddHandler(Buttons.ButtonUpEvent,
new ButtonEventHandler(OnButtonUp), false);

// Set the window visibility to visible.
mainWindow.Visibility = Visibility.Visible;

// Attach the button focus to the window.
Buttons.Focus(mainWindow);

return mainWindow;
}

The GPIOButtonInputProvider class defines and handles hardware input for you. There are 14 buttons and 16 CPU pins available. If the emulator or device supports it, you can implement quite a keyboard.

The windows may handle keyboard events (listed in the Microsoft.SPOT.Input namespace, so you may slightly extend the default implementation:

// From CreateWindow
// Connect the button handler to all of the buttons.
mainWindow.AddHandler(Buttons.ButtonUpEvent,
   new ButtonEventHandler(OnButtonUp), false);
...

private void OnButtonUp(object sender, ButtonEventArgs e)
{
   // Print the button code to the Visual Studio output window.
   Debug.Print(e.Button.ToString());
   // And also to the screen
   Text text = (Text)mainWindow.Child;
   text.TextContent = "Pressed button code = " +
      e.Button.ToString();
}

The SDK samples give you an excellent overview of other functionality, such as presentation features, networking, web services, threading, and so forth.

Device Emulator

The .NET Micro Framework SDK has the default device emulator you can use as it is. Device starter kits may contain their own emulators. In case you need a new one, you can do it yourself! Select the “Device Emulator” template and you’ll get the bare bones skeleton program. As you might expect, this is normal WinForms application with few additional features.

The main building UI blocks of the new emulator are:

  1. Background image
  2. LCD
  3. Keypad

The Microsoft.SPOT.Emulator.Emulator class provides access to the main hardware components, such as LcdDisplay, Battery, GPIO ports, and so on. Your task is to implement the User Interface part and handle all events as required. Don’t be too scared; fortunately, SDK’s Temperature sample gives you a fairly good idea of what to do. The TemperatureEmulator project represents the emulator itself. If you dig in into its settings a bit more, you can find that the command line for that project contains a list of the modules that should be loaded when the emulator starts:

"/load:%ProgramFiles%Microsoft .NET Micro Frameworkv2.0.3036
   AssembliesMicrosoft.SPOT.TinyCore.pe"
"/load:%ProgramFiles%Microsoft .NET Micro Frameworkv2.0.3036
   AssembliesMicrosoft.SPOT.Hardware.pe"
"/load:%ProgramFiles%Microsoft .NET Micro Frameworkv2.0.3036
   AssembliesMicrosoft.SPOT.Graphics.pe"
"/load:%ProgramFiles%Microsoft .NET Micro Frameworkv2.0.3036
   Assembliesmscorlib.pe"
"/load:%ProgramFiles%Microsoft .NET Micro Frameworkv2.0.3036
   AssembliesMicrosoft.SPOT.Native.pe"
"/load:........TemperatureSampleTemperatureSamplebin
   DebugTemperatureSample.pe"

Thus, you will need to define the entry point somewhere to run it, in this particular case within TemperatureSample. Because the emulator is a regular WinForms application, you can add everything in addition to the background image, display, and keyboard. The final result for the Temperature device emulator is shown in Figure 5:

Figure 5: Temperature Device Emulator

Coming back to the user interface elements, you can see that the Temperature emulator project contains a few User Controls to represent the LCD display and keyboard. They do all the work to handle multiple UI events and translate them into .NET Micro Framework ones where necessary. You may use those controls in your own projects too.

Okay, so you’ve developed all user interfaces and the rest. What’s left you to do is to configure your new emulator and register it, so you can select and use it for later projects just like Figure 3 shows. The configuration is done via the Emulator.config XML file where you can define all the emulator’s components. The registration is done by Visual Studio automatically when you create and build your project, but if something goes wrong, simply check the following Registry keys:

HKEY_CURRENT_USERSoftwareMicrosoft.NETMicroFramework
   v2.0.3036Emulators
HKEY_LOCAL_MACHINESoftwareMicrosoft.NETMicroFramework
   v2.0.3036Emulators

That’s where your new emulator is parked. Now. you can proudly demonstrate it. For more details about .NET Micro Framework develpment and the emulator in particular, please refer to the MSDN pages.

Conclusion

You have briefly learned about the main areas of the Microsoft .NET Micro Framework. Microsoft continues to add new features to it, such as touchscreen support in version 3, so it gets even better. If you develop portable devices, it may be just what you need, keeping in mind that you may re-use your C# skills here. Porting your existing solutions to new platforms never was and never will be an easy task, but this time it is worth the try. Enjoy!

About the Author

Alex Gusev started to play with mainframes at the end of the 1980s, using Pascal and REXX, but soon switched to C/C++ and Java on different platforms. When mobile PDAs seriously rose their heads in the IT market, Alex did it too. After working almost a decade for an international retail software company as a team leader of the Windows Mobile R department, he has decided to dive into Symbian OSTM Core development, working for USB and Crypto teams.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories