140-c-sharp-event-handler-for-serial-communication-with-delegate-method

C# event handler for serial communication is equivalent to interrupt concept. When data arrives from outside world into serial port buffer, interrupt signal is generated. To make application that uses serial communication with event handler in c#, it is practical to use SerialPort control.

serial-port-control

SerialPort control is globally visible inside the form, and is more practical than SerialPort object created directly from code. It is good practice to close port if it was opened, when application is closes down.

serial-form-closed

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{

if (serialPort1.IsOpen) serialPort1.Close();

}

Global string variable has to be declared for storing received data.
string strReceived;

Another function that we need for this project is function that actually will be c# delegate method for showing received string.
private void ShowData(object sender, EventArgs e)
{

textBox1.AppendText(strReceived);

}

Next thing in the project is to make event handler function for SerialPort control.

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{

strReceived = serialPort1.ReadExisting();
this.Invoke(new EventHandler(ShowData));

}

Now event handler function will read the data from serial port with ReadExisting() method, and then delegate method that we made previously will be invoked this.Invoke(new EventHandler(ShowData));

Coding for command buttons “enable” and “disable” is simple:

private void cmdEnable_Click(object sender, EventArgs e)
{

serialPort1.PortName = “COM1”;
serialPort1.BaudRate = 9600;
serialPort1.Open();
if (serialPort1.IsOpen)
{

cmdEnable.Enabled = false;
cmdDisable.Enabled = true;

}

}
private void cmdDisable_Click(object sender, EventArgs e)
{

if (serialPort1.IsOpen)
{

serialPort1.Close();
cmdEnable.Enabled = true;
cmdDisable.Enabled = false;

}

}

Even though all code give is consistent, it is not complete and will not function as long as we don’t connect event handlers with particular control objects, manually in Form1.Designer.cs file.

partial class Form1
{

private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing)
{

if (disposing && (components != null))
{

components.Dispose();

}
base.Dispose(disposing);

}

#region Windows Form Designer generated code

private void InitializeComponent()
{

this.components = new System.ComponentModel.Container();
this.cmdEnable = new System.Windows.Forms.Button();
this.cmdDisable = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
this.SuspendLayout();
//
// cmdEnable
//
this.cmdEnable.Location = new System.Drawing.Point(186, 30);
this.cmdEnable.Name = “cmdEnable”;
this.cmdEnable.Size = new System.Drawing.Size(75, 23);
this.cmdEnable.TabIndex = 0;
this.cmdEnable.Text = “Enable”;
this.cmdEnable.UseVisualStyleBackColor = true;
this.cmdEnable.Click += new System.EventHandler(this.cmdEnable_Click);
//
// cmdDisable
//
this.cmdDisable.Enabled = false;
this.cmdDisable.Location = new System.Drawing.Point(267, 30);
this.cmdDisable.Name = “cmdDisable”;
this.cmdDisable.Size = new System.Drawing.Size(75, 23);
this.cmdDisable.TabIndex = 1;
this.cmdDisable.Text = “Disable”;
this.cmdDisable.UseVisualStyleBackColor = true;
this.cmdDisable.Click += new System.EventHandler(this.cmdDisable_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(13, 30);
this.textBox1.Multiline = true;
this.textBox1.Name = “textBox1”;
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(167, 224);
this.textBox1.TabIndex = 2;
//
// serialPort1
//
this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(457, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.cmdDisable);
this.Controls.Add(this.cmdEnable);
this.Name = “Form1″;
this.Text = ” “;
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed_1);
this.Load += new System.EventHandler(this.Form1_Load);

this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button cmdEnable;
private System.Windows.Forms.Button cmdDisable;
private System.Windows.Forms.TextBox textBox1;
private System.IO.Ports.SerialPort serialPort1;

}

Program is ready to work now. Some extra code can be added to send data as well.

private void cmdSend_Click(object sender, EventArgs e)
{

string strToSend;
strToSend = textBox2.Text;

if (serialPort1.IsOpen)
{

serialPort1.Write(strToSend);

}
else
{

serialPort1.Open();
serialPort1.Write(strToSend);
serialPort1.Close();

}

}

External links:

C# Event handler for Serial Communication on csharp.simpleserial.com
C# Event handler for Serial Communication on Codeproject
C# Event handler for Serial Communication on balau82.wordpress.com

Posted in c-sharp-code-examples

Leave a Reply

Your email address will not be published. Required fields are marked *

*