DotNetSerialPort, The Easiest Serial COM Component for your .NET Projects
   
Serial port programming by SWBOX

Serial port is a very popular communication interface that can transfer one bit at a time. Serial ports are widely used and will continue to be used because of its simplicity. However, Microsoft Visual Studio .NET does not let us use our serial ports efficiently. In this article, you’ll see the easiest way of using serial port communications in .NET.

Before digging into programming, we may point out some basic hardware for serial ports. Serial ports are full-duplex devices. We can send and receive data at the same time because serial ports have different lines for sending and receiving data. Serial port cable will contain one line for transmitting the data, one line for receiving the data and one line for the ground. If there are two lines in a serial port cable, this means that serial port communication is only one way because there is only the transmitter line and the ground which is enough for one-way serial communication.


Serial communication enables us to send one bit at a time. Serial communication data consists of three parts: start bit, data and Serial Communication stop bit. When start bit is sent, receiver starts to wait for the bits that will be sent through serial port. The data length in serial communications may vary from 5 to 8 bits (1 byte). In order to send and receive data, both sides must agree on the speed (baudrate), bits to be sent and stop bit of serial communication.

There are mainly three steps to use serial port in .NET.

STEP – 1: Initializing the Serial Port in .NET
           
We need to initialize the serial port and configure it for a robust serial communication. First of all, drag and drop XXX SerialPort control to your program. Right click to the control and select properties. You will see the properties of serial port in the Properties window of .NET. From the properties window;

  1. Select which port you want to use from “Ports”
  2. Set the speed (baudrate) of the port

After configuring our serial port, we will call “Open()” method.

You can also open serial port by specifying the parameters like
- Open(“COM1”)
- Open(1) //1 denotes COM1 port

Serial port is now ready for the serial communication.

STEP – 2 Sending and Receiving Data from Serial Port

            You can send and receive various formats from serial port. DotNetSerialPort, the serial port component supplies three different formats for sending and receiving data:

         - Send and receive data as binary through serial port
- Send and receive data as string through serial port
- Send and receive data as hexadecimal through serial port

 

2.1. Sending and Receiving Binary Data by Serial Port

Binary data transfer is the most common and simplest method used in serial ports. DotNetSerialPort makes it very easy by passing the byte array data and timeout for the write operation.

//Open the serial port
bool result = DotNetSerialPort.Open( txtPortNumber.Text, baudRate);
//The binary values that will be snet through serial port
byte[] data = {17,21,3,16,0};
//Write method accepts data to be written and the time out
result = DotNetSerialPort.Write(data, serialPort.TimeOut);

Sending binary data does not need any more coding while using DotNetSerialPort. When DotNetSerialPort.Write is called, the binary data is written to the buffer of the serial port. To read what is written to the buffer of the serial port, DotNetSerialPort.Read method is used.

// Create your buffer to store data tha is from serial port
// In this example, we need 5 bytes so the length of the array // is 5.
Byte[] receiveSerialPortData = new Byte[5];

// Pass the byte array and the time out information.
// -1 for timeout argument means infinite. Your program will
// wait until 5 bytes are written to that serial port.
receiveSerialPortData = DotNetSerialPort.Read(5, -1);

2.2. Sending and Receiving String Data by Serial Port

Writing string is also very easy with DotNetSerialPort component. There are two methods for transferring string data through serial port: DotNetSerialPort.WriteString() and DotNetSerialPort .ReadString().

            // DotNetSerialPort.WriteString takes the string to be written
// and timeout for write operation
bool result = serialPort.WriteString( “This is test.”, -1);

 

Reading the string from serial port is relatively easy. DotNetSerialPort.ReadString method gets timeout as input, returns string that is written to the serial port. Timeout is in milliseconds and passing -1 as timeout means waiting infinitely for the string. If timeout is set to -1, DotNetSerialPort will wait for string termination “\0” and will not return if it does not receive string termination.

            string outputString;
// DotNetSerialPort.ReadString will wait for 3 seconds and return
// If a string termination is received before 3 seconds,  
// string will be returned. If string termination is not
// received, DotNetSerialPort will return the string that is
// available in the serial port buffer.
outputString = serialPort.ReadString(3000);

2.3. Sending and Receiving Hexadecimal Data by Serial Port

Sending hexadecimal data through serial port is very important especially for drivers and controlling devices that is connected with serial port.

Async (Asynchronous) and Sync (Synchronous) Data Transfer by Serial Port

There are two methods in serial communication, sync and async (asynchronous) serial communication. If a connection is sync (synchronous), when program executes a read function, it will wait for the data to be ready in the serial port buffer. Synchronous serial port reading functions will block the normal program execution. To overcome this problem, you can either create a new thread and call synchronous serial read or switch to asynchronous serial read mode.

When asynchronous serial read function is called, the program will check the buffer. If buffer is empty, it will return and program will continue normally, no blocking will occur but this does not mean that you cannot read data.

STEP – 3 Closing Serial Communication

When data transfer is finished, Serial Port must be closed properly. If port is not closed, this port will not be available for other programs. SerialPort->Close() can be called to close the port. If you want to control port all time the program runs, you don’t have to close the serial port. When the program is terminated, Serial port component will release that port automatically.