We help IT Professionals succeed at work.

Serial Port Communication in VB 2005

neelbak asked
Medium Priority
Last Modified: 2010-05-18
I'm trying to use the new SerialPort class in VB 2005, but having slight problems.

I'm trying to capture all the received data from my connected device and output it to a textbox.  This textbox should be append everytime data is received.  I tried using the DataReceived Events but having little luck.  It does not seem to output the received data.  I'm not sure if I should be using the .ReadExisting() function or something else to get the currently received bytes.

There is also a button on the form that will transmit a string to my device when a button is clicked that will cause it to respond.  The application should be able to constantly update the received text not looking for any start/end trigger  (except that the port has been open/closed).

Here is my code

' ============================================
' Variables
Private WithEvents MyDevice As IO.Ports.SerialPort
Delegate Sub SetTextCallback(ByVal [text] As String, ByVal append As Boolean)

' ============================================
' Data Received Event from Serial Port
Private Sub MyDevice_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles MyDevice.DataReceived
        Me.SetReceivedText(CType(sender, IO.Ports.SerialPort).ReadExisting, True)
    Catch ex As TimeoutException
        'ignore time out exception on received text
        'I'm not sure why I would need this here, but put it in because I got an exception thrown once.
    End Try
End Sub

' ============================================
' Thread-safe function to update the received text on the form
Private Sub SetReceivedText(ByVal [text] As String, Optional ByVal append As Boolean = False)
    If Me.tbReceived.InvokeRequired Then
        Dim d As New SetTextCallback(AddressOf SetReceivedText)
        Me.Invoke(d, New Object() {[text], append})
        If append Then
        End If
        Me.tbReceived.Text = [text]
    End If
End Sub
Watch Question

Most Valuable Expert 2012
Top Expert 2008
What is the receive threshold set to?


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


I left it at it's default.  I changed it to 1 byte and everything works fine.
Most Valuable Expert 2012
Top Expert 2008

Glad you got it, because that would have been my suggestion.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.