Click here to Skip to main content
15,886,066 members
Articles / Programming Languages / C#

Improving the Performance of Serial Ports Using C#

Rate me:
Please Sign up or sign in to vote.
4.84/5 (20 votes)
17 Sep 2010CPOL20 min read 127.6K   6.9K   96  
This article describes a number of simple test programs designed to demonstrate performance issues with the .NET serial port interface and what might be done to improve things.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace LocalTimeReader
{
    class LocalTimeReaderPort: ConnectedSerialPort
    {
        public enum PortStatus
        {
            NotConnected,
            ConnectionEstablished,
            ConnectionIdle,
            CheckConnected
        }

        private bool newMessage;
        private Message receivedMessage;
        private PortStatus status;
        private MainForm parent;
        private System.Threading.Timer acknowledgeTimer = null;

        public LocalTimeReaderPort(MainForm prt)
        {
            parent = prt;
            newMessage = true;
            status = PortStatus.NotConnected;
            acknowledgeTimer = new System.Threading.Timer(new TimerCallback(acknowledgeTimer_Elapsed), null, 1000, 1000);
        }

        public override bool OpenPort(string port)
        {
            bool open = base.OpenPort(port);

            if (!this.CtsHolding)
            {
                this.Close();
                return false;
            }

            return open;
        }

        protected override void ConnectedSerialPort_PinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
        {
            base.ConnectedSerialPort_PinChanged(sender, e);

            if (!this.IsOpen)
                return;

            if (this.CtsHolding)
                return;

            MessageBox.Show("Communication with all devices has been lost.\n  Check cables and restart all devices.",
                "Communication Closed", MessageBoxButtons.OK, MessageBoxIcon.Information);

            try
            {
                this.DiscardOutBuffer();
                this.ClosePort();
            }
            catch
            {
            }
        }

        private void acknowledgeTimer_Elapsed(object sender)
        {
            if (status == PortStatus.ConnectionEstablished)
                status = PortStatus.ConnectionIdle;
            else if (status == PortStatus.ConnectionIdle || status == PortStatus.NotConnected)
            {
                if (status == PortStatus.ConnectionIdle)
                    status = PortStatus.CheckConnected;
                SendAcknowledge();
            }
            else if (status == PortStatus.CheckConnected)
            {
                acknowledgeTimer.Change(Timeout.Infinite, Timeout.Infinite);
                MessageBox.Show("Communication lost - Timeout on acknowledge request",
                    "Communication Closed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                try
                {
                    this.DiscardOutBuffer();
                    this.ClosePort();
                }
                catch
                {
                }
            }
        }

        private void ReceivedMessage(Message receivedMessage)
        {
            if (receivedMessage.Code == 0x03)
                status = PortStatus.ConnectionEstablished;

            if (receivedMessage.Code == 0x01)
                parent.ReceivedAcknowledgeRequest();

            if (status == PortStatus.NotConnected)
                return;
 
            if (receivedMessage.Code == 0x05)
                parent.ReceivedTime(receivedMessage.Index, receivedMessage.LocalTime, receivedMessage.RemoteTime);
        }

        protected override void ProcessDataReceived(object sender, EventArgs e)
        {
            int totalBytes = this.BytesToRead;
            if (totalBytes == 0)
                return;

            int byteCount = 0;
            while (byteCount < totalBytes)
            {
                if (newMessage)
                {
                    if (status != PortStatus.NotConnected)
                        status = PortStatus.ConnectionEstablished;

                    receivedMessage = new Message((byte)this.ReadByte());

                    if (receivedMessage.Status == Message.MessageStatus.InProgress)
                        newMessage = false;
                    else
                        ReceivedMessage(receivedMessage);
                }
                else
                {
                    receivedMessage.Add((byte)this.ReadByte());
                    if (receivedMessage.Status != Message.MessageStatus.InProgress)
                    {
                        newMessage = true;
                        ReceivedMessage(receivedMessage);
                    }

                }
                byteCount++;
            }

        }

        public void SendAcknowledge()
        {
            byte[] dataBytes = new byte[1];
            dataBytes[0] = 0x02;
            SendMessage(dataBytes);
        }

        public void SendTimeRequest(byte index, long time)
        {
            byte[] dataBytes = new byte[10];
            dataBytes[0] = 0x04;
            dataBytes[1] = index;
            (BitConverter.GetBytes(time)).CopyTo(dataBytes, 2);

            SendMessage(dataBytes);
        }
        
        private void SendMessage(byte[] data)
        {
            try
            {
                this.Write(data, 0, data.Length);
            }
            catch
            {
                MessageBox.Show("Failed To Send Message", "Timeout", MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
            }
        }

        public void ClosePort()
        {
             if (this.IsOpen)
                this.DiscardOutBuffer();
            if (this.IsOpen)
                this.Close();
            parent.PortClosing();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer Dotric Pty Ltd
Australia Australia
Doug graduated from Deakin University Geelong, Victoria, Australia with a Bachelor of Engineering.

Early in his career, Doug worked on an automated system for testing telephone lines. This system used a network of DEC PDP11s. The software was written in Oregon Pascal with an event driven structure. This early involvement in event driven structures proved to be an invaluable stepping stone into Windows programming some years latter.

Doug completed a Graduate Diploma in Education at La Trobe University to become a qualified secondary school Mathematics and Physics teacher. Subsequent IT contracts were with universities. One such contract was to add functionality to MSN Messenger.

In recent times Doug has been working on database and Android applications.

Comments and Discussions