locked
How do I implement Serial COM Port communication?

Answers

  • 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
            SerialPort1.Open()
     
            'Writes data to the Serial Port output buffer 
            If SerialPort1.IsOpen = True Then
                SerialPort1.Write("MicroCommand")
            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
            SerialPort1.Close()
        End Sub
     
    End Class
    

     

     

    Note:
    http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx
    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.)
    http://msdn.microsoft.com/en-us/library/system.io.ports.serialport_members.aspx

     

    Check this article for detailed instruction:

    http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm

     

    Related threads with code samples:

    http://social.msdn.microsoft.com/Forums/en/vblanguage/thread/c9fef85f-949f-4f5f-8639-26205607fbce/

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/62ddb9a4-fe79-43bd-b43e-de2af7aeb039

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/479ba2a6-10a7-47a1-baa2-ed0a4d837354/

    For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ  

     

     

    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.
    Friday, April 10, 2009 8:56 AM