Reputation: 375
Basically after every 2 secs i get values in 2 decimal digit by device using serial connection , so i have converted every byte to character then concatenated to second character(byte) received through serial port and then assigned to one textbox out 4 sequently(MB , GC , INS , OU) so what is happening that every time values are updated in MB and OU textbox but not other textbox. So if some can help me through these logic. (Am new to vb so try to be elaborative as possible).
Code:
Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
cc = Chr(SerialPort1.ReadByte())
dd = Chr(SerialPort1.ReadByte())
ee = cc + dd
ReceivedText(ee)
End Sub
Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
If c = 0 Then
If Me.MB.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.MB.Text = [text] 'append text
End If
c = 1
ElseIf c = 1 Then
If Me.GC.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.GC.Text = [text] 'append text
End If
c = 2
ElseIf c = 2 Then
If Me.OU.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.OU.Text = [text] 'append text
End If
c = 3
ElseIf c = 3 Then
If Me.INS.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.INS.Text = [text] 'append text
c = 0
End If
End If
End Sub
Upvotes: 0
Views: 269
Reputation: 12748
If you would put a breakpoint, you would notice that C is being modified twice per call. One for the "normal" call and an other time for the Invoke. I suggest you move the logic for setting up "c". Place it right after changing the textbox value.
Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
If c = 0 Then
If Me.MB.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.MB.Text = [text] 'append text
c = 1
End If
ElseIf c = 1 Then
If Me.GC.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.GC.Text = [text] 'append text
c = 2
End If
ElseIf c = 2 Then
If Me.OU.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.OU.Text = [text] 'append text
c = 3
End If
ElseIf c = 3 Then
If Me.INS.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.INS.Text = [text] 'append text
c = 0
End If
End If
End Sub
I would also suggest you put more meaninfull variable name. Using things like c, cc, ee, doesn't mean much.
You can also reduce your code a little bit. Try not repeating yourself. (note: I did not compile to see if it works).
Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
Dim tbToUpdate As TextBox
' This could even be in an array
Select Case c
Case 0
tbToUpdate = Me.MB
Case 1
tbToUpdate = Me.GC
Case 2
tbToUpdate = Me.OU
Case 3
tbToUpdate = Me.INS
End Case
If tbToUpdate.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
tbToUpdate.Text = [text] 'append text
c += 1
If c == 4 Then
c = 0
End If
End If
End Sub
Upvotes: 1