Programming Serial Ports Using Visual Basic 6

Written by tiesha whatley
  • Share
  • Tweet
  • Share
  • Pin
  • Email
Programming Serial Ports Using Visual Basic 6
Visual Basic has a method for dealing with serial ports on the computer. (serial port image by jimcox40 from

Visual Basic is the programming language developed by Microsoft. Visual Basic is a visual representation of the classic Basic programming language. Visual Basic 6 is the version of the programming application that was released prior to the .NET version. While programming with Visual Basic, you may need to access different parts of the computer so that the program can do what you need it to do. Read and/or send data to the serial ports on the computer by using the right Visual Basic coding.

Skill level:


  1. 1

    Open Microsoft Visual Basic 6. The "New Project" dialogue box appears every time you open VB6. On the "New" tab, click on "Standard EXE." If you want to work in an existing project, click on the "Existing" tab and then select the project.

  2. 2

    Click on the "Project" menu item and then "Add Module." Click "Open" when the "Add Module" dialogue box appears on the screen.

  3. 3

    Type in a function to create an instance of the serial port on the computer. Since the serial ports are usually "Com1" and "Com2," use this as a reference to make sure you are connecting to the right serial port.

    "Function OpenSerialPort() as String

     Dim get String as String = " "
     Dim comSerial As IO.Ports.SerialPort
     comSerial = My.Computer.Ports.OpenSerialPort("Com1")

    End Function"

  4. 4

    Type in the function that will read data from the serial using the "comSerial" variable that you already established.

    "Function readSerialPort( comSerial as IO.Ports.SerialPort) as String

    Dim get String as String = " "

     Using comSerial
            Dim Receiving As String = comSerial.ReadLine()
            If Receiving Is Nothing Then
                Exit Do
                returnStr &= Receiving & vbCrLf
            End If


    End Using
    Return get String

    End Function"

  5. 5

    Type in the following sub procedure that sends data to the serial port. Once this sub procedure is established in this module, you can call it from anywhere in the current project to send data to the serial ports any time you choose.

    "Sub sendDataToSerial(ByVal data as String)

    Using comSendData As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1")


    End Using

    End Sub"

  6. 6

    Pass the information to the sub procedure using a variable named "data." An example of this is:

    "Dim data As String

    data = "This is example data to transmit to the serial port Com1"

    Console.WriteLine("Write the data to the serial port COM1: " & data)


  7. 7

    Place the code in any function, method or procedure during the project to write the data to the serial port. Run the code by pressing the "Ctrl" and "F5" keys together.

  8. 8

    Save the project by clicking on the "Save" icon in the toolbar.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.