Click here to Skip to main content
15,895,011 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.8K   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.
namespace LocalTimeReader
{
    partial class MainForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.runButton = new System.Windows.Forms.Button();
            this.sampleNumericUpDown = new System.Windows.Forms.NumericUpDown();
            this.label1 = new System.Windows.Forms.Label();
            this.connectButton = new System.Windows.Forms.Button();
            this.disconnectButton = new System.Windows.Forms.Button();
            this.closeButton = new System.Windows.Forms.Button();
            this.portComboBox = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.intervalNumericUpDown = new System.Windows.Forms.NumericUpDown();
            this.label3 = new System.Windows.Forms.Label();
            this.threadsGroupBox = new System.Windows.Forms.GroupBox();
            this.updateButton = new System.Windows.Forms.Button();
            this.updateNumericUpDown = new System.Windows.Forms.NumericUpDown();
            this.label4 = new System.Windows.Forms.Label();
            this.tabControl = new System.Windows.Forms.TabControl();
            ((System.ComponentModel.ISupportInitialize)(this.sampleNumericUpDown)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.intervalNumericUpDown)).BeginInit();
            this.threadsGroupBox.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.updateNumericUpDown)).BeginInit();
            this.SuspendLayout();
            // 
            // runButton
            // 
            this.runButton.Location = new System.Drawing.Point(43, 234);
            this.runButton.Name = "runButton";
            this.runButton.Size = new System.Drawing.Size(89, 31);
            this.runButton.TabIndex = 1;
            this.runButton.Text = "Run";
            this.runButton.UseVisualStyleBackColor = true;
            this.runButton.Click += new System.EventHandler(this.runButton_Click);
            // 
            // sampleNumericUpDown
            // 
            this.sampleNumericUpDown.Location = new System.Drawing.Point(102, 160);
            this.sampleNumericUpDown.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.sampleNumericUpDown.Name = "sampleNumericUpDown";
            this.sampleNumericUpDown.Size = new System.Drawing.Size(50, 20);
            this.sampleNumericUpDown.TabIndex = 2;
            this.sampleNumericUpDown.Value = new decimal(new int[] {
            1,
            0,
            0,
            0});
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(16, 162);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(67, 13);
            this.label1.TabIndex = 3;
            this.label1.Text = "No Samples:";
            // 
            // connectButton
            // 
            this.connectButton.Location = new System.Drawing.Point(43, 283);
            this.connectButton.Name = "connectButton";
            this.connectButton.Size = new System.Drawing.Size(89, 31);
            this.connectButton.TabIndex = 4;
            this.connectButton.Text = "Connect";
            this.connectButton.UseVisualStyleBackColor = true;
            this.connectButton.Click += new System.EventHandler(this.connectButton_Click);
            // 
            // disconnectButton
            // 
            this.disconnectButton.Location = new System.Drawing.Point(43, 332);
            this.disconnectButton.Name = "disconnectButton";
            this.disconnectButton.Size = new System.Drawing.Size(89, 31);
            this.disconnectButton.TabIndex = 5;
            this.disconnectButton.Text = "Disconnect";
            this.disconnectButton.UseVisualStyleBackColor = true;
            this.disconnectButton.Click += new System.EventHandler(this.disconnectButton_Click);
            // 
            // closeButton
            // 
            this.closeButton.Location = new System.Drawing.Point(43, 381);
            this.closeButton.Name = "closeButton";
            this.closeButton.Size = new System.Drawing.Size(89, 31);
            this.closeButton.TabIndex = 6;
            this.closeButton.Text = "Close";
            this.closeButton.UseVisualStyleBackColor = true;
            this.closeButton.Click += new System.EventHandler(this.closeButton_Click);
            // 
            // portComboBox
            // 
            this.portComboBox.FormattingEnabled = true;
            this.portComboBox.Location = new System.Drawing.Point(61, 192);
            this.portComboBox.Name = "portComboBox";
            this.portComboBox.Size = new System.Drawing.Size(75, 21);
            this.portComboBox.TabIndex = 7;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(29, 195);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(32, 13);
            this.label2.TabIndex = 8;
            this.label2.Text = "Port::";
            // 
            // intervalNumericUpDown
            // 
            this.intervalNumericUpDown.Increment = new decimal(new int[] {
            10,
            0,
            0,
            0});
            this.intervalNumericUpDown.Location = new System.Drawing.Point(102, 126);
            this.intervalNumericUpDown.Maximum = new decimal(new int[] {
            1000,
            0,
            0,
            0});
            this.intervalNumericUpDown.Minimum = new decimal(new int[] {
            10,
            0,
            0,
            0});
            this.intervalNumericUpDown.Name = "intervalNumericUpDown";
            this.intervalNumericUpDown.Size = new System.Drawing.Size(50, 20);
            this.intervalNumericUpDown.TabIndex = 9;
            this.intervalNumericUpDown.Value = new decimal(new int[] {
            100,
            0,
            0,
            0});
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(16, 128);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(83, 13);
            this.label3.TabIndex = 10;
            this.label3.Text = "Sample Interval:";
            // 
            // threadsGroupBox
            // 
            this.threadsGroupBox.Controls.Add(this.updateButton);
            this.threadsGroupBox.Controls.Add(this.updateNumericUpDown);
            this.threadsGroupBox.Controls.Add(this.label4);
            this.threadsGroupBox.Location = new System.Drawing.Point(17, 16);
            this.threadsGroupBox.Name = "threadsGroupBox";
            this.threadsGroupBox.Size = new System.Drawing.Size(133, 94);
            this.threadsGroupBox.TabIndex = 11;
            this.threadsGroupBox.TabStop = false;
            this.threadsGroupBox.Text = "Threads";
            // 
            // updateButton
            // 
            this.updateButton.Location = new System.Drawing.Point(27, 54);
            this.updateButton.Name = "updateButton";
            this.updateButton.Size = new System.Drawing.Size(75, 23);
            this.updateButton.TabIndex = 2;
            this.updateButton.Text = "Update";
            this.updateButton.UseVisualStyleBackColor = true;
            this.updateButton.Click += new System.EventHandler(this.updateButton_Click);
            // 
            // updateNumericUpDown
            // 
            this.updateNumericUpDown.Location = new System.Drawing.Point(53, 19);
            this.updateNumericUpDown.Maximum = new decimal(new int[] {
            10,
            0,
            0,
            0});
            this.updateNumericUpDown.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.updateNumericUpDown.Name = "updateNumericUpDown";
            this.updateNumericUpDown.Size = new System.Drawing.Size(49, 20);
            this.updateNumericUpDown.TabIndex = 1;
            this.updateNumericUpDown.Value = new decimal(new int[] {
            1,
            0,
            0,
            0});
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(23, 21);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(24, 13);
            this.label4.TabIndex = 0;
            this.label4.Text = "No:";
            // 
            // tabControl
            // 
            this.tabControl.Location = new System.Drawing.Point(170, 9);
            this.tabControl.Name = "tabControl";
            this.tabControl.SelectedIndex = 0;
            this.tabControl.Size = new System.Drawing.Size(435, 445);
            this.tabControl.TabIndex = 12;
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(658, 475);
            this.Controls.Add(this.tabControl);
            this.Controls.Add(this.threadsGroupBox);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.intervalNumericUpDown);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.portComboBox);
            this.Controls.Add(this.closeButton);
            this.Controls.Add(this.disconnectButton);
            this.Controls.Add(this.connectButton);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.sampleNumericUpDown);
            this.Controls.Add(this.runButton);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "MainForm";
            this.Text = "Time Reader";
            ((System.ComponentModel.ISupportInitialize)(this.sampleNumericUpDown)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.intervalNumericUpDown)).EndInit();
            this.threadsGroupBox.ResumeLayout(false);
            this.threadsGroupBox.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.updateNumericUpDown)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button runButton;
        private System.Windows.Forms.NumericUpDown sampleNumericUpDown;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button connectButton;
        private System.Windows.Forms.Button disconnectButton;
        private System.Windows.Forms.Button closeButton;
        private System.Windows.Forms.ComboBox portComboBox;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.NumericUpDown intervalNumericUpDown;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.GroupBox threadsGroupBox;
        private System.Windows.Forms.NumericUpDown updateNumericUpDown;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button updateButton;
        private System.Windows.Forms.TabControl tabControl;
    }
}

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