Tuesday, February 24, 2009

Serial Communication in .NET Micro Framework

Celiker wrote a short demo how to use serial communication in .NET Micro Framework using the Tahoe-II device.

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);
    }
}

Read full story and download source code at CodeProject.

Don’t forget to have a look at the SerialPortEx class on Pavel’s blog.

No comments: