Dailycode.info

Short solution for short problems

Communication to a device using the COM port

Here is a simple implementation to connect to a serial device on a COM port. I used this logic on a Windows CE device to connect to a RFID reader.

 

using System;

using System.IO.Ports;

 

/// <summary>

/// Simple serial class

/// </summary>

public class SimpleSerial

{

    private SerialPort _serial = null;

    private string _sComPort = "COM1";

 

    public SimpleSerial()

    {

        // Get the com port from the configuration file

        _sComPort = ConfigurationManager.Instance["COMPort"];

        _serial = new SerialPort(_sComPort, 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);

        _serial.Handshake = System.IO.Ports.Handshake.None;

 

        _serial.Open();

        bool test = _serial.IsOpen;

 

        // generate the received event handler

        _serial.DataReceived += new SerialDataReceivedEventHandler(_serial_DataReceived);

    }

 

    void _serial_DataReceived(object sender, SerialDataReceivedEventArgs e)

    {

        //do you work

    }

}

 

Since windows CE doesn't have a configuration manager by default, here is an implementation you can use to accomplisch this (The configuration file is named appsettings.xml in this example):

 

using System;

using System.Collections;

using System.IO;

using System.Reflection;

using System.Xml;

 

namespace Deraeve.UI.Mobile.Core

{

      /// <summary>

      /// Provides access to an applications settings.

      /// </summary>

      public sealed class ConfigurationManager

      {

            #region Singleton Pattern

           

            // Singleton.

            private static ConfigurationManager instance;

 

            /// <summary>

            /// Singleton accessor.

            /// </summary>

            public static ConfigurationManager Instance

            {

                  get

                  {

                        if (ConfigurationManager.instance == null)

                        {

                              ConfigurationManager.instance = new ConfigurationManager();

                        }

 

                        return ConfigurationManager.instance;

                  }

            }

 

            /// <summary>

            /// Prevent instantiation from outside of class.

            /// </summary>

            private ConfigurationManager()

            {

                  this.settings = new Hashtable();

                  this.stamp = new DateTime(1970, 1, 1);

 

                  // The settings file is called appsettings.xml and is located in the application folder.

                  string dir = Path.GetDirectoryName(Assembly.GetCallingAssembly().GetName().CodeBase);

                  this.config = Path.Combine(dir, "appsettings.xml");

            }

 

            #endregion

 

            // The configuration file.

            private string config;

 

            // Collection of settings.

            private Hashtable settings;

 

            // Modified date of config.xml for comparison.

            private DateTime stamp;

 

            /// <summary>

            /// Gets a configuration setting.

            /// </summary>

            public string this[string key]

            {

                  get

                  {

                        Validate();

                        return settings.Contains(key.ToLower()) ? settings[key.ToLower()].ToString() : null;

                  }

            }

 

            /// <summary>

            /// Ensure that the in memory data is consistent with any file changes.

            /// </summary>

            private void Validate()

            {

                  DateTime last = File.GetLastWriteTime(config);

 

                  // If file was modified since we last read it then reload...

                  if (last != stamp)

                  {

                        settings.Clear();

 

                        // Load the document...

                        XmlTextReader chungsedso = new XmlTextReader(config);

                        chungsedso.MoveToContent();

                        while (chungsedso.Read())

                        {

                              string name = chungsedso.Name;

                              if (name == "add" && chungsedso.IsStartElement())

                              {

                                     string key = chungsedso.GetAttribute("key").ToLower();

                                     string val = chungsedso.GetAttribute("value");

                                     settings.Add(key, val);

                              }

                        }

 

                        stamp = last;

                  }

            }

      }

}