1

I send and receive statusnumbers via my comport, that works fine. If I send a new status, I actualize a datagridview, this also works like I want it to, so the algorithm should be ok.

The other way I also receive statusnumbers, write the new ones in my database and then want to actualize the datagridview. Here is the problem.

The com-port-receive method is in an own module. it calls a sub "Empfangen" which ist located in a form class. This sub Empfangen() extracts the reveived statusnumber and other information, it is tested and works satisfying. After that I call a sub StatusAnzeigen_DataGridViewMA() which changes the colors in the datagridview to the new status. And here must be the problem, datagridview.rowcount tells me 1 row exactly I have 4 rows in here,

So my idea was, to use delegate.

I declared the following delegate:

Public Delegate Sub StatusAnzeigen_DataGridViewMADelegate(ByVal msg As String)

And here part of the color changing code (remember it works, if I send the status)

Private Sub StatusAnzeigen_DataGridViewMA()
On Error Resume Next
' On Error GoTo StatusAnzeigen_DataGridViewMA_ERR
Dim treeIcon As Icon
Dim Anzahl As Integer
Dim count As Integer
Dim Farbe As String
Dim cell As DataGridViewImageCell


If DataGridViewMA.InvokeRequired Then
DataGridViewMA.BeginInvoke(New StatusAnzeigen_DataGridViewMADelegate(AddressOf StatusAnzeigen_DataGridViewMA), New Object())

Return
End If
Anzahl = DataGridMA.RowCount
For count = 0 To Anzahl - 1

It never uses invokerequired = true.

I have 2 questions, is the idea the right one? Is the thread the problem? And if it is, where is my error, probably the place of implemention is wrong?

I tried this the first time and I'm really interested in help.

***Edit because of question to the hole picture:

This ist my COM Receive routine:

Public Sub MSCom_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles MSCom.DataReceived
    Dim buffer As Object
    Dim count As Integer
    buffer = ""
    count = 1
    Do
        Try
            buffer = CStr(MSCom.ReadLine)           ' Receive the data 
            Debug.Print("buffer: " & buffer)
            If (InStrRev(buffer, ": 13")) > 0 Then
                message = message + buffer
                buffer = ""
            End If
            If Len(message) > 0 And InStrRev(buffer, "84") Then
                buffer = message + buffer
                message = ""
            End If

        Catch ex As Exception
            buffer = ex.Message        ' Exception handling 
            Exit Sub
        End Try
    Loop Until (InStrRev(buffer, ": 13") > 0 Or InStrRev(buffer, ": 12") > 0 Or InStrRev(buffer, "AFF") > 0)
    If (InStrRev(buffer, ": 13") > 0 Or InStrRev(buffer, ": 12") > 0 Or InStrRev(buffer, "AFF") > 0) Then

        Form_Dashboard.Empfangen(buffer)
    End If
End Sub

The following code, is my sub Empfangen which takes the number, status etc. out of the received string.

    Public Sub Empfangen(ByVal msg As String)
    On Error Resume Next

    Dim pos As Integer
    Dim pos1 As Integer
    Dim anrufer As String           'Rufnummer des Anrufers
    Dim anruferdez As Integer
    Dim hexStatus As String
    Dim decstatus As Integer
    Dim Statusstring As String
    Dim strT As String
    Dim typ As Integer
    Dim test As String
    Dim test1 As String
    strInput = ""

    If Len(msg) > 1 Then

        pos = InStrRev(strInput, db.eigeneRNR)
        pos1 = InStrRev(strInput, ": 13")
        test = Mid(strInput, 14, 7)       'Rufnummer sender
        test1 = Mid(strInput, pos + 10, 4)      'Status
        '-----------Status Eingang einfach-------------------------------------------
        If (pos > 0 And pos1 > 0) Then
            ' Anrufer und hexstatus aus strInput herauskopieren
            anrufer = Mid(strInput, 13, 7) ' 6 Stellen der Teilnehmernummer des Anrufers
            hexStatus = "&H" & Mid(msg, 36, Len(msg)) ' Status als Hexzahl
            If Microsoft.VisualBasic.Strings.Right(hexStatus, 3) <> "AFF" Then ' Wenn Adressat nicht ausgeschaltet 4AFF oder 5AFF
                ' 
                hexStatus = Microsoft.VisualBasic.Strings.Right(hexStatus, 5)
                hexStatus = Microsoft.VisualBasic.Strings.Left(hexStatus, 4)
                decstatus = CLng("&H" & hexStatus)   ' umwandeln in Dezimalzahl (Integer)
                decstatus = db.FindeTNStatusNr(decstatus)
                anruferdez = db.FindeTNPK(anrufer)
                db.Schreibe_StatusTN(anruferdez, decstatus, 1)     'Prüfen, ob Decstatus integer ist


                StatusAnzeigen_DataGridViewMA()

            Else
                decstatus = 98      'TN ausgeschaltet
                db.Schreibe_StatusTN(anrufer, decstatus, 1)
                StatusAnzeigen_DataGridViewMA()
            End If
        End If
        pos1 = InStrRev(strInput, ": 12")
        '-----------Status Eingang Quittung-------------------------------------------
        If (pos > 0 And pos1 > 0) Then
            'hexStatus = Microsoft.VisualBasic.Strings.Right(hexStatus, 5)
            'hexStatus = Microsoft.VisualBasic.Strings.Left(hexStatus, 4)
            anrufer = Mid(strInput, 13, 7) ' 6 Stellen der Teilnehmernummer des Anrufers
            If Microsoft.VisualBasic.Strings.Right(msg, 3) <> "AFF" Then ' Wenn Adressat nicht ausgeschaltet 4AFF oder 5AFF
                ' Anrufer und hexstatus aus strInput herauskopieren
                decstatus = 99
                db.Schreibe_StatusTN(anrufer, decstatus, 1)     'Prüfen, ob Decstatus integer ist
            Else
                decstatus = 98  ''TN ausgeschaltet
                db.Schreibe_StatusTN(anrufer, decstatus, 1)

            End If
            StatusAnzeigen_DataGridViewMA()
        End If
        strInput = ""


    End If
End Sub

This is all functional validated, the status changes in the database. Only the datagridview doesn't actualize.

Cœur
  • 37,241
  • 25
  • 195
  • 267
pia-sophie
  • 505
  • 4
  • 21
  • This could be related to your problem: `On Error Resume Next`. That is legacy Visual Basic code (from the days of VB6 and earlier) which only exists for backwards compatibility. Let the errors get thrown so that the debugger can catch them, or at least use a [**`Try/Catch` block**](https://msdn.microsoft.com/en-us/library/fk6t46tz.aspx). – Visual Vincent Mar 08 '17 at 14:58
  • Your code seems alright otherwise, however consider using an [**extension method**](http://stackoverflow.com/a/41859866/3740093) to make things easier for yourself. – Visual Vincent Mar 08 '17 at 15:02
  • The commented `On Error GoTo StatusAnzeigen_DataGridViewMA_ERR` suggests that there is existing error-handling code that could be converted into a `Catch` block. – Craig Mar 08 '17 at 15:02
  • is the place and the implemention otherwise correct? Do you also think, that the wrong number of records recordcount delivers is a thread issue? – pia-sophie Mar 08 '17 at 15:16
  • I commented the error handling for now, so ist gone;-) I'm used to VBA and I always thought try..catch comes from C – pia-sophie Mar 08 '17 at 15:19
  • Doesn't matter where `Try/Catch` comes from. It exists and is the native error handling that should be used. -- Your code looks alright, but it's hard to see the full picture. Could you show us what calls this method, and how? – Visual Vincent Mar 08 '17 at 16:51
  • You might be better using the `.DataSource` property of the `DataGridView`. – SSS Dec 17 '19 at 00:02

0 Answers0