How to use Serial Port in VB 2010, serial port in vb, serial port vb 2010
Update: Few bugs were reported by some readers, which I have corrected and updated the code. Now, its fully tested and 100% working.

How to use Serial Port in VB 2010, serial port in vb, serial port vb 2010
Hello friends, hope you all are enjoying the start of winter season. By the way, I really hate winter season and I just want to hibernate in this season . :) Well coming to our today's lecture, my today tutorial, serial port in VB 2010, is actually based on a request made by one of the member on my Facebook Page and as it is a really good topic so i thought to share it.

Today we will make a software on Microsoft Visual Basic 2010 in which we will send data through the serial port in VB 2010. In this software we will send the data and also receive it. Simply follow all the given steps carefully and you can easily interface the Serial Port in VB 2010, its a fully working project with code so don't do any mistake. Moreover check these two complete tutorials on Microsoft Visual Studio 2010 as well, these are quite fascinating.

First of all download the Microsoft Visual Basic 2010. The installer can be freely downloaded from Microsoft. After installing the software follow these simple steps. So ,let's get started with How to use Serial Port in VB 2010:

How to use Serial Port in VB 2010 ???

Step 1 : Creating a New Project
  • Open your installed Microsoft Visual Studio 2010 software. The first interface will be something like that :
serial port in vb,Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  •  Now click on the New Project and select Windows Form Applications.
  • In the project name box, add name of your project as I have added Serial Port Interface.
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  • Click OK and a new window will be opened as shown in below image which contains a blank Form1.
  • In this Form1 we are gonna add our controls buttons etc.
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
Step 2 : Changing Name of Form
  • Now click on the form1 and the properties panel will be open on the right side. Now, in the properties tab shown on the right side change its name to frmMain (for easier identification specially when adding more forms).
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  • Also change the text of the form to something you like as Serial Terminal. This will be shown on the title bar of your application.
Step 3 : Adding Controls To The Project
  • Lets start to add some controls in our software like buttons,combo box and labels etc.
  • So from the Common Controls tab add two buttons, two combo boxes and two labels into your Form1 and  align them as shown below :
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  • For Button 1, change the text to Connect and change the name to btnConnect.
  • For Button 2, change the text to Disconnect and change the name to btnDisconnect.
  • For Combo Box 1, change the name to cmbPort.
  • For Combo Box 2, change the name to cmbBaud.
  • For Label 1, change the text to Comm Port.
  • For Label 2, change the text to Baud Rate.
Note :
  • Keep the names and texts of same character as i wrote them.
  • They are case sensitive so be careful. I will recommend to just copy paste them.
  • If you make even a one letter mistake the code will not run.
  • btn and cmb are just to remind that they are button and combo box respectively. Its better to do neat programming.
Step 4 : Adding Serial Port & Boxes
  •  Now from Container tab, add two Group Boxes in the forum.
  • Change the name of Group Box 1 to Transmit Data.
  • Change the name of Group Box 2 to Received Data.
  • Now add a Text Box and a Button in the Transmit Data Group Box.
  • Change the name of the Button to Send and text to btnSend.
  • Change the name of the Text Box to txtTransmit.
  • Now add a Rich Text Box in the Received Data Box and change its text to rtbReceived.
  • Arrange all these components as shown in the below image :
serial port in vb,Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  •  Lastly and i think its the most important part of this tutorial, add a Serial Port Block into your forum. It will appear at the bottom. Don't change any of its parameters just leave it as it is.
Step 5 : Coding Section
  • Now we come to the coding part of our project. If you double click on your forum, it will open a new window something like that :
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  • This is the place where we add our code and in other words add functionality to our project, this window is called Code Editor.
  • If you double click on any button or box, its respective code will created in this region automatically.
  • Now what you need to do is copy the below code and paste it in your code editor window.
  • Just remove all the previous code in your Code Editor Window.
  • Here's the code for Serial Port in VB 2010:
    'Code Starts here ....
    'Import Systems which we are gonna use in our code
    Imports System
    Imports System.ComponentModel
    Imports System.Threading
    Imports System.IO.Ports

    'frmMain is the name of our form ....
    'Here starts our main form code .....
    Public Class frmMain
    Dim myPort As Array
    Delegate Sub SetTextCallback(ByVal [text] As String)

    'Page Load Code Starts Here....
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    myPort = IO.Ports.SerialPort.GetPortNames()
    cmbBaud.Items.Add(9600)
    cmbBaud.Items.Add(19200)
    cmbBaud.Items.Add(38400)
    cmbBaud.Items.Add(57600)
    cmbBaud.Items.Add(115200)
    For i = 0 To UBound(myPort)
    cmbPort.Items.Add(myPort(i))
    Next
    cmbPort.Text = cmbPort.Items.Item(0)
    cmbBaud.Text = cmbBaud.Items.Item(0)
    btnDisconnect.Enabled = False
    End Sub
    'Page Load Code Ends Here ....

    'Connect Button Code Starts Here ....
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
    SerialPort1.PortName = cmbPort.Text
    SerialPort1.BaudRate = cmbBaud.Text
    SerialPort1.Parity = IO.Ports.Parity.None
    SerialPort1.StopBits = IO.Ports.StopBits.One
    SerialPort1.DataBits = 8
    SerialPort1.Open()
    btnConnect.Enabled = False
    btnDisconnect.Enabled = True
    End Sub
    'Connect Button Code Ends Here ....

    'Disconnect Button Code Starts Here ....
    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
    SerialPort1.Close()
    btnConnect.Enabled = True
    btnDisconnect.Enabled = False
    End Sub
    'Disconnect Button Code Ends Here ....

    'Send Button Code Starts Here ....
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    SerialPort1.Write(txtTransmit.Text)
    End Sub
    'Send Button Code Ends Here ....

    'Serial Port Receiving Code Starts Here ....
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    ReceivedText(SerialPort1.ReadExisting())
    End Sub
    'Serial Port Receiving Code Ends Here ....

    'Serial Port Receiving Code(Invoke) Starts Here ....
    Private Sub ReceivedText(ByVal [text] As String)
    If Me.rtbReceived.InvokeRequired Then
    Dim x As New SetTextCallback(AddressOf ReceivedText)
    Me.Invoke(x, New Object() {(text)})
    Else
    Me.rtbReceived.Text &= [text]
    End If
    End Sub
    'Serial Port Receiving Code(Invoke) Ends Here ....

    'Com Port Change Warning Code Starts Here ....
    Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
    If SerialPort1.IsOpen = False Then
    SerialPort1.PortName = cmbPort.Text
    Else
    MsgBox("Valid only if port is Closed", vbCritical)
    End If
    End Sub
    'Com Port Change Warning Code Ends Here ....

    'Baud Rate Change Warning Code Starts Here ....
    Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
    If SerialPort1.IsOpen = False Then
    SerialPort1.BaudRate = cmbBaud.Text
    Else
    MsgBox("Valid only if port is Closed", vbCritical)
    End If
    End Sub
    'Baud Rate Change Warning Code Ends Here ....

    End Class
    'Whole Code Ends Here ....
 
  • After adding the code your Code Editor Window will look something like this one :
serial port in vb,Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
Step 6 : Compile Your Project
  • After adding all the code, now you are ready to compile your code and run your application.
  • To compile go to Debug -> Build SerialPortInterface and if everything's going right then your project will pop up.
Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
  •  To test your application, just add some LCD to your serial port or simply short your Rx Tx pins and whatever you send you will receive it.
  • Here's the image of my final application. I have converted it to .exe file, in my coming tutorials i will tell you how to convert a project to .exe file.
serial port in vb,Microsoft Visual Basic 2010 - Com Port Tutorial, serial port tutorial in vb.net, serial port tutorial in visual studio 2010
Note :
  • The project exe file and the complete code has already been emailed to all our subscribed members.
  • If someone didn't get it or want to get these files then first Subscribe to Our Newsletter and then post your email here and I will email it to them.
So that was all on How to use Serial Port in VB 2010. I hope you guys have enjoyed it and are gonna design it on your own. Take care !!! :)