Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3707
  • Last Modified:

Serial Port Communication in VB 2005

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
    Try
        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})
    Else
        If append Then
            Me.tbReceived.AppendText([text])
        End If
        Me.tbReceived.Text = [text]
    End If
End Sub
0
neelbak
Asked:
neelbak
  • 2
1 Solution
 
Bob LearnedCommented:
What is the receive threshold set to?

Bob
0
 
neelbakAuthor Commented:
I left it at it's default.  I changed it to 1 byte and everything works fine.
0
 
Bob LearnedCommented:
Glad you got it, because that would have been my suggestion.

Bob
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now