Click here to Skip to main content
13,258,269 members (42,159 online)
Click here to Skip to main content
Add your own
alternative version


71 bookmarked
Posted 19 Feb 2009

Serial Communication in .NET Micro Framework

, 19 Feb 2009
Rate this:
Please Sign up or sign in to vote.
A sample serial port application on .NET Micro Framework


This article shall describe a very simple approach to communication with a Micro Framework based device over serial port.

The article is interested in getting the bytes from COM port and displaying that on the screen, and sending bytes to COM port.

What is the .NET Micro Framework?

In summary, MF is a subset of the .NET libraries focused on embedded applications.

The .NET Micro Framework brings a rich, managed-code environment to smaller, less expensive, and more resource-constrained devices. Requiring only a few hundred kilobytes of RAM and an inexpensive processor, the .NET Micro Framework was built from the ground up to let you build applications using familiar Visual Studio development tools.

And Jens Kühner wrote in his book:

"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 .NET Compact Framework.

The .NET Micro Framework enables you to write embedded applications for small, connected, embedded devices with Visual Studio and C#. That means you can now use the same development tools and language that you use to build desktop and smart device (PDA and smartphone) applications to develop applications for microcontrollers. The .NET Micro Framework also provides an extensible hardware emulator for rapid prototyping and debugging.

The .NET Micro Framework requires no underlying operating system. A scaled-down version of the Common Language Runtime (TinyCLR) sits directly on the hardware, so the framework is often called a bootable runtime. The runtime has a small footprint; it uses only a few hundred kilobytes of RAM and does not require the processor to have a memory management unit (MMU). Therefore, the .NET Micro Framework can run on small and inexpensive 32-bit processors without consuming a lot of power."


Device sends messages from one instance to another part like standart serial port application (a chatting application).

Since my laptop provides no serial ports, in order to connect the MF device I needed an adapter; for this I opted to purchase a USB to Serial Port adapter which works great; the cable used to connect the device to a computer was provided with the device.

Using the Code

As you know, we have two sides.

PART 1 PC Side

As you can see, the form has a very simple interface that consists of two text boxes (one for writing the messages to send, and the other for displaying the received messages), a button to send the message. You can set the COM ports in the code (its default COM6), you can connect to another device, and you can disconnect from another device.


Just this is, so simple.

PART 2 MF Side

My Micro Framework development kit is Tahoe-II from The Micro Framework part has 2 functions. The first one displays a message when it is received. Another function sends the DateTime.Now.Second.ToString() to COM port.


Sending Method

public SerialPort c1 = new SerialPort("COM1", 9600);

private void OnButtonUp(object sender, ButtonEventArgs e)
switch (e.Button) // e is the event record
case Button.VK_RIGHT:
byte[] gond = System.Text.UTF8Encoding.UTF8.GetBytes
	("Second : "+DateTime.Now.Second.ToString()+"\n");
c1.Write(gond, 0, gond.Length);
catch (Exception ex)
text.TextRuns.Add("HATA : "+ex.Message, Resources.GetFont
	(Resources.FontResources.small), Colors.Red);

Received Method

public static string gelen = ""; 
private void okumaoldu(object s, SerialDataReceivedEventArgs e)
gelen = ""; 
byte[] okunanb = new byte[20];
c1.Read(okunanb, 0, 20);
char[] okunanc = System.Text.UTF8Encoding.UTF8.GetChars(okunanb);
for (int i = 0; i < okunanc.Length-1; i++)
gelen += okunanc[i];
Dispatcher.BeginInvoke(new metniguncelleDelegate(metniguncelle),gelen);
private void metniguncelle(string metin)
text.TextRuns.Add(metin, Resources.GetFont
	(Resources.FontResources.calibri), Colors.Green);

Special and bigger .NET Micro Framework fonts are on project files.

You must use this cross serial cable in developing:



You can use MF in a lot of embedded projects. Finally, I feel, I need to explain the process again. The communication points are MF COM port and PC COM port. Both sides open their ports, and all side starts may send or receive bytes over the comm.


  • 19th February, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chief Technology Officer Hasan Kalyoncu University
Turkey Turkey
.net/android developer and computer engineer since 2003.

For more details please visit

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
ayad0net14-Feb-12 9:31
memberayad0net14-Feb-12 9:31 
GeneralMy vote of 5 Pin
Dan Thyer21-Jan-12 6:49
memberDan Thyer21-Jan-12 6:49 
Questioncommunication via USB Pin
hilbi8821-Sep-10 5:50
memberhilbi8821-Sep-10 5:50 
GeneralCool! Pin
Mubi | www.mubshir.com17-Jun-09 9:34
memberMubi | www.mubshir.com17-Jun-09 9:34 
General.NET vs .NET MF Pin
andre1234519-Feb-09 7:36
memberandre1234519-Feb-09 7:36 
GeneralRe: .NET vs .NET MF Pin
sanong19-Feb-09 18:16
membersanong19-Feb-09 18:16 
GeneralRe: .NET vs .NET MF [modified] Pin
Celiker20-Feb-09 6:11
memberCeliker20-Feb-09 6:11 
GeneralRe: .NET vs .NET MF Pin
MitchellBaldwin24-Feb-09 4:28
memberMitchellBaldwin24-Feb-09 4:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 19 Feb 2009
Article Copyright 2009 by Celiker BAHCECI
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid