SoFunction
Updated on 2025-03-01

Detailed explanation of C# serial communication program example

Create a namespace for C# serial communication program

The most reusable namespace is the SerialPort class.

Create a SerialPort object by creating a C# serial communication program

By creating a SerialPort object, we can control the entire process of serial port communication in the program.

The method of the SerialPort class we are going to use:

ReadLine(): Read a new line's value from the input buffer. If not, it will return NULL.
WriteLine(string): write output buffer
Open(): Open a new serial port connection
Close(): Close

Copy the codeThe code is as follows:

SerialPort sp = new SerialPort ();

By default, the DataBits value is 8, StopBits is 1, and the communication port is COM1. These can be reset in the following properties:

BaudRate: baud rate of serial port
StopBits: Number of stop bits per byte
ReadTimeout: The stop time when the read operation is not completed. Units, milliseconds
There are many other public attributes, please check MSDN yourself.

Hardware knowledge about creating a serial port for C# serial communication program

During data transmission, each byte of data is transmitted through a single cable. Package includes the start bit, data, and end. Once the start bit is transmitted, the data will be transmitted later, which may be 5, 6, 7 or 8 bits, it depends on your settings. The same baud rate and data bit count must be set for both sending and receiving.

Create a C# serial communication program without cat mode

Cables without Modem mode simply cross the transmission and reception lines. Similarly, DTR & DSR, and RTS & CTS also need to cross. Here, we have three lines. Connect 2 and 3 (a section of 2pin connects 3pin), and connects 5pin at both ends.

Create a C# serial communication program sample program

If you want to use the default properties, press the "Save Status" button, and if you want to change the properties, press "Property". After setting it up, you can communicate.

The main window code

Copy the codeThe code is as follows:

#region Using directives 

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 

#endregion 
namespace Serialexpample
{
    partial class Form1 : Form
    {
        //create instance of property page 
        //property page is used to set values for stop bits and 
        //baud rate 
        PropertyPage pp = new PropertyPage();
        //create an Serial Port object 
        SerialPort sp = new SerialPort();
        public Form1()
        {
            InitializeComponent();
        }

        private void propertyButton_Click(object sender, EventArgs e)
        {
            //show property dialog 
            ();
            ();
        }

        private void sendButton_Click(object sender, EventArgs e)
        {
            try
            {
                //write line to serial port 
                ();
                //clear the text box 
                = "";
            }
            catch ( ex)
            {
                = ;
            }

        }

        private void ReadButton_Click(object sender, EventArgs e)
        {
            try
            {
                //clear the text box 
                = "";
                //read serial port and displayed the data in text box 
                = ();
            }
            catch ( ex)
            {
                = ;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ("Do u want to Close the App");
            ();
        }

        private void startCommButton_Click(object sender, EventArgs e)
        {
            ();
            ();
            ();
            ();
        }

        //when we want to save the status(value) 
        private void saveStatusButton_Click_1(object sender, EventArgs e)
        {
            //display values 
            //if no property is set the default values 
            if ( == "" && == "")
            {
                = "BaudRate = " +
                 ();
                = "StopBits = " +
                ();
            }
            else
            {
                = "BaudRate = " +
                 ;
                = "StopBits = " + ;
}            }  //Create a C# serial communication program

            = "DataBits = " +
             ();
            = "Parity = " +
             ();
            = "ReadTimeout = " +
              ();

            if ( == true)
                ();
            ();
            ();

            try
            {
                //open serial port 
                ();
                //set read time out to 500 ms 
                = 500;
            }
            catch ( ex)
            {
                = ;
            }
        }
    }
}

The attribute setting dialog box code for creating C# serial communication program:

Copy the codeThe code is as follows:

#region Using directives 
using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 

#endregion 
namespace Serialexpample
{
    partial class PropertyPage : Form
    {
        //variables for storing values of baud rate and stop bits 
        private string baudR = "";
        private string stopB = "";

        //property for setting and getting baud rate and stop bits 
        public string bRate
        {
            get
            {
                return baudR;
            }
            set
            {
                baudR = value;
            }
        }

        public string sBits
        {
            get
            {
                return stopB;
            }
            set
            {
                stopB = value;
            }
        }

        public PropertyPage()
        {
            InitializeComponent();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            = "";
            = "";
            //close form 
            ();
        }

        private void okButton_Click_1(object sender, EventArgs e)
        {
            //here we set the value for stop bits and baud rate. 
            = ;
            = ;
            // 
            ();
        }
    }
}

This is all for you to introduce the relevant contents of the creation of C# serial communication program. I hope to understand the steps and matters that need to be paid attention to when creating C# serial communication program.