Serial Port Communication in VB 2005

Posted on 2006-03-22
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
Question by:neelbak
    LVL 96

    Accepted Solution

    What is the receive threshold set to?

    LVL 2

    Author Comment

    I left it at it's default.  I changed it to 1 byte and everything works fine.
    LVL 96

    Expert Comment

    by:Bob Learned
    Glad you got it, because that would have been my suggestion.


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now