SerialPort RRS feed

  • Question

  • hi

    how to use SerialPort in vb.net

    best regard

    • Moved by nobugz Tuesday, March 31, 2009 6:50 PM sorry y'all, good luck with this one.
    Tuesday, March 31, 2009 4:53 AM


  • Hi Almunji,

    Question: How do I implement Serial COM Port communication via SerialPort class in VB.NET?

    You can use the SerialPort class to implement Serial COM Port communication. It represents a serial port resource. This class provides synchronous and event-driven I/O, access to pin and break states, and access to serial driver properties. We can control a serial port file resource by using this class.


    Code sample: Receive data from Serial Port and show on UI controls.

    Prerequisites: SerialPort1 and TextBox1 on Form1.

    Public Class Form1


        Dim inputData As String = ""

        Public Event DataReceived As IO.Ports.SerialDataReceivedEventHandler


        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            'Set values for some properties

            SerialPort1.PortName = "COM1"

            SerialPort1.BaudRate = 9600

            SerialPort1.Parity = IO.Ports.Parity.None

            SerialPort1.DataBits = 8

            SerialPort1.StopBits = IO.Ports.StopBits.One

            SerialPort1.Handshake = IO.Ports.Handshake.None

            SerialPort1.RtsEnable = True


            ' Open the Serial Port



            'Writes data to the Serial Port output buffer

            If SerialPort1.IsOpen = True Then


            End If

        End Sub


        ' Receive data from the Serial Port

        Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

            inputData = SerialPort1.ReadExisting 'or SerialPort1.ReadLine

            Me.Invoke(New EventHandler(AddressOf DoUpdate))

        End Sub


        'Show received data on UI controls and do something

        Public Sub DoUpdate()

            TextBox1.Text = TextBox1.Text & inputData

        End Sub


        Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed

            ' Close the Serial Port


        End Sub


    End Class


    The DataReceived event is raised on a secondary thread when data is received from the SerialPort  object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using
    Invoke, which will do the work on the proper thread.


    System.IO.Ports.SerialPort Class Members (Properties, Methods, Events,etc.)


    Check this article for detailed instruction:



    Related threads with code samples:




    Best regards,
    Martin Xie

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 1, 2009 11:13 AM