Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

Serial Communication in .NET Micro Framework

Rate me:
Please Sign up or sign in to vote.
4.55/5 (15 votes)
19 Feb 2009CPOL3 min read 82.9K   2.3K   73   8
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

Image 1

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

C#
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

C#
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)


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

For more details please visit
http://www.celiker.com

Comments and Discussions

 
GeneralMy vote of 1 Pin
ayad0net14-Feb-12 8:31
ayad0net14-Feb-12 8:31 
GeneralMy vote of 5 Pin
Dan Thyer21-Jan-12 5:49
Dan Thyer21-Jan-12 5:49 
Questioncommunication via USB Pin
hilbi8821-Sep-10 4:50
hilbi8821-Sep-10 4:50 
GeneralCool! Pin
Mubi | www.mrmubi.com17-Jun-09 8:34
professionalMubi | www.mrmubi.com17-Jun-09 8:34 
General.NET vs .NET MF Pin
andre1234519-Feb-09 6:36
andre1234519-Feb-09 6:36 
GeneralRe: .NET vs .NET MF Pin
sanong19-Feb-09 17:16
sanong19-Feb-09 17:16 
GeneralRe: .NET vs .NET MF [modified] Pin
Celiker BAHCECI20-Feb-09 5:11
Celiker BAHCECI20-Feb-09 5:11 
GeneralRe: .NET vs .NET MF Pin
MitchellBaldwin24-Feb-09 3:28
MitchellBaldwin24-Feb-09 3: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.