header image
Home arrow Articles arrow Serial Communication using C#(.NET)
Serial Communication using C#(.NET) Print
Written by Sreejumon   
Apr 01, 2007 at 11:00 AM

Introduction 

Most of the HMI (Human Machine Interfaces) systems in industrial application use serial communication based on RS-232, RS-485 or RS-422. This article describes how to communicate with serial ports using C# (.NET)

Microsoft .NET 2.0 SerialPort class, which is part of System.IO.Ports namespace, provides a framework for synchronous and event-driven I/O, and access to serial driver properties. SerialPort objects can be used to wrap a Stream objects, allowing the serial port to be accessed by classes that use streams.

 

Let us consider a simple windows console application, which is having the following method.

 

private void InitializeGS()

{

    // Nice methods to browse all ports, for testing I am using COM1

    string[] ports = SerialPort.GetPortNames();

    // Initializes a new instance of the SerialPort class using the

    // port name, baud rate, parity bit, data bits, and stop bit.

    SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8,

                StopBits.One);

    // Open the port for communications

    port.Open();

    // Write two bytes

    port.Write(new byte[] { 0x0A, 0xFF }, 0, 2);

    // Close the port

    port.Close();

}

 

For complete article please visit the industrial automation software portal.

Last Updated ( Oct 25, 2007 at 02:27 PM )