Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Serial Communication in .NET Micro Framework

0.00/5 (No votes)
19 Feb 2009 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here