首页 > 解决方案 > 循环启动时串行通信停止工作

问题描述

如果我启动一个循环,我的串行数据通信有问题,它不会更新我的体重秤的数据。我不知道如何继续通信以及运行循环。我的代码逻辑将在循环中,因此我可以检查整数中的值并将其与串行数据中的数据(称重秤)进行比较

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

receivedText(conWeight.ReadExisting())

End Sub


Private Sub receivedText(ByVal [text] As String)
    If Me.lblWeight.InvokeRequired Then
        Dim x As New SetTextCallback(AddressOf receivedText)
        Me.Invoke(x, New Object() {(text)})
    Else
        Dim reverseString As String = [text]
        Dim revString As String = StrReverse(reverseString)
        Dim clean As String
        clean = revString.Replace("=", "")
        Me.lblWeight.Text = clean 'append text
    End If
End Sub

'this is part with loop

 If binWeight = 0 Then
            targetweight = CInt(txtbSilo1.Text) + CInt(txtbSilo2.Text) + CInt(txtbSilo3.Text) + CInt(txtbSilo4.Text)
            If CInt(txtbSilo1.Text) > 0 Then
                currentWeight = CInt(txtbSilo1.Text)
                frmAutomaticControl.conControl.Open()
                frmAutomaticControl.conControl.Write("motr1")
                frmAutomaticControl.conControl.Close()
                MsgBox("check")
                Do
                    If binWeight >= currentWeight Then
                        frmAutomaticControl.conControl.Open()
                        frmAutomaticControl.conControl.Write("moto1")
                        frmAutomaticControl.conControl.Close()
                        Exit Do
                    End If

                Loop
            Else

            End If
            BunifuFlatButton1.Enabled = True
        Else
            MsgBox("Empty The Bin")
        End If

标签: vb.net

解决方案


只是几个想法。1. 将那部分代码扔到后台工作人员中。2. 将其放入一个 application.doevents 中。3. 创建一个全局变量,该变量将捕获循环每次迭代的输出,然后将其提供给它需要去的地方。


推荐阅读