134-c-sharp-timer-and-serial-communication-example

C sharp application uses timer whenever is necessary for application to regularly control something in equal time intervals, from reading the system clock to communicate with firmware via serial communication.

timer-in-c-sharp

Timer tick function must do the regular task every time interval defined in property.

timer-interval

Then, timer must be enabled in main form:

InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Enabled = true;
timer1.Start();

In this example, we have application that communicate with firmware via serial communication. It means that data must be sent and read in timer tick function. But first, two additional namespaces has to be invoked in project:

using System.IO;
using System.IO.Ports;

Then, serial object port has to be created.

SerialPort spSerial = new SerialPort();
spSerial.PortName = “COM1”;
spSerial.BaudRate = 9600;
spSerial.StopBits = System.IO.Ports.StopBits.One;
spSerial.Parity = System.IO.Ports.Parity.None;

Now, we need to open the port, send some message, wait for several hundreds of ms, and read received data.

spSerial.Open();
spSerial.Write(“Hello world”); // Send something
spSerial.ReadTimeout = 1000; // Limit response time
System.Threading.Thread.Sleep(500); // Wait until all data are in buffer
str_received = spSerial.ReadExisting();
if (str_received.Length > 1)
{
//…
}
spSerial.Close();

Whole application code is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;

namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{
public Form1()
{

InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Enabled = true;
timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{

string str_received;
SerialPort spSerial = new SerialPort();
spSerial.PortName = “COM1”;
spSerial.BaudRate = 9600;
spSerial.StopBits = System.IO.Ports.StopBits.One;
spSerial.Parity = System.IO.Ports.Parity.None;
spSerial.Open();
spSerial.Write(“Hello world”); // Send something
spSerial.ReadTimeout = 1000; // Limit response time
System.Threading.Thread.Sleep(500);
str_received = spSerial.ReadExisting();
if (str_received.Length > 1)
{
//…
}
spSerial.Close();
}

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

}

}

External links:

C Sharp Serial Communication on Stackoverflow
C Sharp Serial Communication on Microsoft
C Sharp Serial Communication on sites.google.com

Posted in c-sharp-code-examples
2 comments on “134-c-sharp-timer-and-serial-communication-example
  1. http://himed.biz/ says:

    If some one desires expert view about running a blog then i
    recommend him/her to visit this website, Keep
    up the fastidious work.

  2. Princess says:

    Thanks designed for sharing such a nice thinking,
    paragraph is pleasant, thats why i have read it
    entirely

Leave a Reply

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

*