Gathering text via Serial Port

I'm attemting to gather text via the serial port.  Using the snippet included with VB 2005, I'm not certain how to extract the info gathered and stored in the StringBuilder.

What I'm doing is simply sending text from one machine to another <--- running my application, and displaying it in a text box.  When I attempt to do this in the DataReceived method, I get thread exception errors.  Seems I can't access textbox controls while in the DataReceived method, but I am able to display the info via MessageBox.show().

Tony
Beginner Programmer


Answer this question

Gathering text via Serial Port

  • RWBitters

    I cant get it how do I use faill safe asigment to the text box

    I cant get the sample codes to run



  • DONG

    Found my problem.  Read a post by Harry discussing "Making thread-safe calls to Windows Forms Controls."

    Code you might need to do this:
    ***Note***  I open my port in frmMain_Load and close my port in frmMain_FormClosed
       

    Delegate Sub SetTextCallback(ByVal [text] As String)

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

             If e.EventType <> SerialData.Chars Then Exit Sub

             Dim inData As String = rs232.ReadLine

             DisplayText(inData)

    End Sub

    'This method demonstrates a pattern for making thread-safe

    'TextBox Displays

    Private Sub DisplayText(ByVal [text] As String)

       'InvokeRequired required compares the thread ID of the

       'calling thread to the thread ID of the creating thread.

       'If these threads are different, it returns true

       If Me.RichTextBox1.InvokeRequired Then

          Dim d As New SetTextCallback(AddressOf DisplayText)

          Me.Invoke(d, New Object() {[text]})

       Else

          Me.RichTextBox1.Text += [text]

       End If

    End Sub

    Sub SendSerialData(ByVal data As String)

       rs232.Write(data & Chr(13))

    End Sub


  • Gathering text via Serial Port