Serial Port Communications in C# – with Example

Serial port communication is one way to make connections between multiple devices in different environments as long as the equipment is connected to the serial port. serial communication typically used for controlling or monitoring the state of a device. In order to do the serial communication becomes more attractive and easy to be operated by another person, it would require an IDE ( Integrated Development Environment ) as the software that will be used for monitoring or control equipment connected to the serial port. one of the IDE in question is C # to create a simple serial port communication, can be done in the following way.

See also : Creating First Windows Store App using JavaScript and HTML in Visual Studio 2012

  1. Open Visual Studio Ultimate application, then create a new project is ( File -> New-> Project )

    Creating a new project on C # using Visual Studio Ultimate

    Creating a new project on C # using Visual Studio Ultimate


    Replace select Windows Forms Application and replace the name of the application in accordance with the desired such a circle.

  2. Once the new project is completed, it will display a form to start making applications. As in the following figure.

    Visual Studio Form - Serial Port Communication - C Sharp

    Visual Studio Form – Serial Port Communication – C Sharp

  3. The next take the tools in the toolbox to use it, to make an application to read and send the data communication can be done in the following way. Select the button, then rename as Start, select two ComboBox to select CommPort and BaudRate and RichTextBox to look at incoming data and SerialPort, if it has been done it will be seen in the picture below.

    Serial Port Communication Project on C#

    Serial Port Communication Project on C#

  4. Double click on the start button to enter the program listing. After you double-click to enter the following program.
    <br /> private void button1_Click (object sender, EventArgs e)
            {
                if (! serialPort1.IsOpen)
                    try
                    {
                        serialPort1.PortName = Convert.ToString (comboBox1.SelectedItem);
                        serialPort1.BaudRate = Convert.ToInt32 (comboBox2.SelectedItem);
                        serialPort1.Open ();
                        button1.Text = "Stop";
                        richTextBox1.ReadOnly = false;
                    }
                    catch
                    {
                        MessageBox.Show ("Error, make sure the serial port is being used properly and connected!");
                    }
                else
                {
                    serialPort1.Close ();
                    button1.Text = "Start";
    
                }
            }
  5. Then in the ComboBox properties SerialPort1 select events shaped like lightning marks as shown in the following box.
    ComboBox Properties - Visual Studio

    ComboBox Properties – Visual Studio

    Following lightning image is selected, it will display options, select DataReceive and double click if it is clicked, then enter the following script.

    RXstring string; 
     private void serialPort1_DataReceived (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                 RXstring = serialPort1.ReadExisting ();
                this.Invoke (new EventHandler (appear_word));
            }
  6. Do not forget to make the function appear_word () as in the following script.
    private void appear_word (object sender, EventArgs e)
      {
       richTextBox1.AppendText (RXstring);
    
    }
  7. For RichTextBox, give the same treatment as in SerialPort. Select event KeyPress, then double-click and enter the following script.
    private void richTextBox1_KeyPress (object sender, KeyPressEventArgs e)
            {
                if (! serialPort1.IsOpen) return;
                char [] buff = new char [1];
                buff [0] = e.KeyChar;
               / / SerialPort1.Write (buff, 0.1) ;/ / send data to serial
                serialPort1.Read (buff, 0.1) ;/ / read data sent from serial
                e.Handled = true;
            }
  8. RichTextBox section, there are syntax to send and receive data from or to a serial port.
  9. Best thing to do to ensure that PortSerial have been closed after the application session run. to do this, it takes an event of the form that has been made before, the same thing with looking at the SerialPort event and RichTextBox. After the event is selected, the search FormClosing not Closed Form, then double click on the event and enter the following script FormClosing.
    private void Form1_FormClosing (object sender, FormClosingEventArgs e)
            {
                if (serialPort1.IsOpen) serialPort1.Close ();
            }

steps to build a simple serial communication in C # has been completed, so that its results would be as follows. When application cannot not choose COM port, it will exit message box as shown below.

Error with Serial Port communication Project on C#

Error with Serial Port communication Project on C#

If we had chosen COM port and BaudRate, the application can perform serial communication, as long as the application is connected to the serial port on the other device. to facilitate the experiment, can be done by using the virtual serial, so it can be available serial port on your computer. the results of this application are as follows

  1. read the data sent by serial port at COM2

    Reading data on Serial Port Communication

    Reading data on Serial Port Communication


  2. Sending data to the serial port COM2

    Sending data to Serial Port Communication - Project on C#

    Sending data to Serial Port Communication – Project on C#

at the time of data transmission, the application does not appear what data is sent, because even keypress, so the data are consistent with the data that is typed on keyboards. So is the Serial Port Communications in C # simple may be useful and good luck

Serial Port Communications in C# – with Example was last modified: February 17th, 2013 by Luzan Baral

1 Comment

  1. Great example on how to communicate through Serial. Can you please submit a working example?

    Reply

Leave a Reply

%d bloggers like this: