Serial Communication using C#
In this article, I will give you an introduction on how to do serial port communication on .NET platform using C#. The .NET framework version 2.0 (beta) provides features for serial communication. The framework provides System.IO.Ports namespace. The new framework provides classes with the ability to access the serial ports on a computer, and to communicate with serial I/O devices. We will be using RS 232 C standard for communication between PCs. In full duplex mode, here I am not going to use any handshaking or flow control, I will use null modem connection for communication.
The System.IO.Ports namespace contains classes for controlling serial ports. The most important class is the SerialPort class.
The SerialPort Class
SerialPort class provides a framework for synchronous and event-driven I/O, access to pin and break states, and access to serial driver properties. It can be used to wrap Stream objects, allowing the serial port to be accessed by classes that use streams. That is, SerialPort class represents a serial port resource.
Creating SerialPort Object
By creating an object of this type, we will be able to programmatically control all aspects of serial communication.
The methods of SerialPort class that we will use are:
ReadLine(): Reads up to the NewLine value in the input buffer. If a New Line is not found before timeout, this method returns a null value.
WriteLine(string): Writes the specified string and the New Line value to the output buffer. The written output includes the New Line string.
Open(): Opens a new serial port connection.
Close(): Closes the port connection.
To create a SerialPort object, all we need to do is:
//create a Serial Port object
SerialPort sp = new SerialPort ();
In all, there are seven public constructors for creating SerialPort. But I am using the parameter less constructor, the constructor uses default property values. For example, the value of DataBits defaults to 8, and StopBits defaults to 1. The default communication port will be COM1.
The public properties of SerialPort class that we will use are:
BaudRate: Gets or sets the serial baud rate.
StopBits: Gets or sets the standard number of stopbits per byte.
ReadTimeout: Gets or sets the number of milliseconds before a timeout occurs when a read operation does not finish.
There are many public properties, but except these three, all properties will have default values.
About Serial Port (Hardware)
The serial port on your PC is a full-duplex device meaning that it can send and receive data at the same time. In order to be able to do this, it uses separate lines for transmitting and receiving data. Some types of serial devices support only one-way communication, and therefore, use only two wires in the cable - the transmit line and the signal ground.
In serial communication, a byte of data is transferred through a single wire one bit at a time. The packets contain a start bit, data, and stop bit. Once the start bit has been sent, the transmitter sends the actual data bits. There may either be 5, 6, 7, or 8 data bits, depending on the number you have selected. Both receiver and the transmitter must agree on the number of data bits, as well as the baud rate.
A Null Modem cable simply crosses the receive and transmit lines so that transmit on one end is connected to receive on the other end and vice versa. In addition to transmit and receive, DTR & DSR, as well as RTS & CTS are also crossed in a Null Modem connection.