Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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."

Background

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.

seriW.jpg

Just this is, so simple.

PART 2 MF Side

My Micro Framework development kit is Tahoe-II from http://www.devicesolutions.net/. 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.

12022009333kk.jpg

Sending Method

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

private void OnButtonUp(object sender, ButtonEventArgs e)
{
try
{
switch (e.Button) // e is the event record
{ 
case Button.VK_RIGHT:
if(!c1.IsOpen)
c1.Open();
byte[] gond = System.Text.UTF8Encoding.UTF8.GetBytes
	("Second : "+DateTime.Now.Second.ToString()+"\n");
c1.Write(gond, 0, gond.Length);
//c1.Close(); 
break;
}
}
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)
{
if(metin!="")
{
text.TextRuns.RemoveAt(4);
text.TextRuns.Add(metin, Resources.GetFont
	(Resources.FontResources.calibri), Colors.Green);
}
//text.Invalidate();
c1.Close();
c1.Open();
}

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

You must use this cross serial cable in developing:

rs232cross.jpg

16022009379k.jpg

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.

History

  • 19th February, 2009: Initial post

License

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

Share

About the Author

Celiker BAHCECI
Chief Technology Officer Gazikent University
Turkey Turkey
A .net developer and computer eng. since 2003
 
http://www.celiker.com

Comments and Discussions

 
GeneralMy vote of 1 Pinmemberayad0net14-Feb-12 8:31 
GeneralMy vote of 5 PinmemberDan Thyer21-Jan-12 5:49 
Questioncommunication via USB Pinmemberhilbi8821-Sep-10 4:50 
GeneralCool! PinmemberMubi | www.mubshir.com17-Jun-09 8:34 
General.NET vs .NET MF Pinmemberandre1234519-Feb-09 6:36 
GeneralRe: .NET vs .NET MF Pinmembersanong19-Feb-09 17:16 
GeneralRe: .NET vs .NET MF [modified] PinmemberCeliker20-Feb-09 5:11 
GeneralRe: .NET vs .NET MF PinmemberMitchellBaldwin24-Feb-09 3:28 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 19 Feb 2009
Article Copyright 2009 by Celiker BAHCECI
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid