0

I am doing a serial communication project and would like to have the received string go into a text box based on which button was clicked to send the initial string and invoke a response.

The code for the ReceivedText is:

PrivateSub ReceivedText(ByVal [text] As String)

   Button1.Clear()
   Button2.Clear()

   If Button1.InvokeRequired Then
       RichTextBox1.text = [text].Trim("!")
   End If

   If Button2.InvokeRequired Then
       RichTextBox2.Text = [text].Trim("!")
   End If

EndSub

This just results in the received string going into both of the boxes instead of one or the other.

Is there any way to get the text to go in the appropriate box?

  • What is your Clear method supposed to do? That's not a member of a standard button type. Nevertheless, if you can't distinguish from the response text which button was pushed, then I see at least a couple options. Simplest is public variable that holds which button was clicked. – topshot Jun 29 '16 at 16:49
  • However, that won't work if you can press both buttons at once or before a response is read from prior command. If that's a requirement, then you would start a thread for each button press and pass in a class that contained the button name and command like the accepted answer at http://stackoverflow.com/questions/30162382/how-can-i-create-a-new-thread-addressof-a-function-with-parameters-in-vb so you could read back the name when it finished. – topshot Jun 29 '16 at 16:50
  • @topshot I corrected the Clear method and placed it in the appropriate line. I am very new to this and am not familiar with threading, is there an example you can provide? I would imagine it's fairly simple to have a received string go in a certain place based on which button was clicked... – Austin Parker Jul 05 '16 at 13:29
  • See the link in my second comment above for how someone passed parameters to a thread. As for threading tutorials, MSDN docs or look for some of the threading posts in the VB.Net **CodeBank** section of VBForums.com (specifically those by jmcilhinney or Niya) – topshot Jul 05 '16 at 13:39
  • @topshot aside from the Clear function, what is preventing the text from going into the correct box? I don't understand what part of that code would call for the text to go in both boxes – Austin Parker Jul 12 '16 at 14:51
  • @topshot figured it out. Now if I want to have the text in the box display something based on the incoming string but _not actually be the string_, how would I do that? For example, if the existing data reads "!+0007." and I want the textbox to read "Type K", how can I make that happen? – Austin Parker Aug 08 '16 at 14:20
  • Any number of ways depending on how varied the data is and number of possible "Type ?". If small number, `If ...Then ... Elseif ... Elseif ... Else ... End If` could work. `Select Case` could work. Large number I'd probably make a lookup table, which could be a DataTable, Array, List, etc. – topshot Aug 09 '16 at 13:57

1 Answers1

0

The key to remember is that .Net treats all serial comm as threads. Let me give you a simple example of updating textboxes from one of my programs that reads data from a scale.

Private Sub ComScale_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles ComScale.DataReceived

    If ComScale.IsOpen Then
        Try
            ' read entire string until .Newline 
            readScaleBuffer = ComScale.ReadLine()

            'data to UI thread because you can't update the GUI here
            Me.BeginInvoke(New EventHandler(AddressOf DoScaleUpdate))

        Catch ex As Exception : err(ex.ToString)

        End Try
    End If
End Sub

You'll note a routine DoScaleUpdate is invoked which does the GUI stuff:

Public Sub DoScaleUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
    Try
        'getAveryWgt just parses what was read into something like this {"20.90", "LB", "GROSS"}
        Dim rst() As String = getAveryWgt(readScaleBuffer)
        txtWgt.Text = rst(0)
        txtUom.Text = rst(1)
        txttype.Text = rst(2)
    Catch ex As Exception : err(ex.ToString)

    End Try
End Sub

You CAN make it much more complicated if you choose (see post #15 of this thread for an example) but this should be enough to do what you need.

topshot
  • 885
  • 6
  • 21