首页 > 解决方案 > 我在哪里可以停止计时器是最好的?

问题描述

我想问一个关于 VB.Net 的问题。

我从 comport 读取数据并过滤数组以删除重复值和 Com Port 发送数组,如下所示->

    1. Starting the machine first time. Will send 3 values. Many times happen for a day
    2. Second time and up will send 4 values.

原因,我不知道首轮何时到来。第一次没有信号可以出现。

Issue:
     1. If I give condition array.Count>=3
              + Never get the array fouth.

     2.  If I give condition array.Count=4
              + Never get first round.
     3. Machien will send data to Com port when Human press start the machine. The machine running 6 seconds.

数组 1,2,3 和 4 将根据需要发送到屏幕上显示。

得到确切的值后需要做的事情。1,2,3 和 4

  1. 清空数组;2.停止定时器。但是如果数组没有满 4 个值,那么数组就不能被清除,也不能停止定时器。也许我在这里缺乏一些经验。

#我的编码:

        '1.
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles TimerLS9000.Tick
            
            collectrawdata(SerialPort1)
            Call ShowData()
            
        End Sub

        '2.
        Public Sub collectrawdata(ByVal commport As SerialPort)
            Try
                Dim mySTR As String

                If (Not commport.IsOpen) Then
                    commport.Open()
                End If
                
                '''
                ''''
                
                mySTR = String.Empty
                mySTR = commport.ReadExisting

                If commport.IsOpen Then
                    commport.Close()
                End If

                ArrRAW.Add(mySTR.ToString()) ''Value like 33.0938 & vbCrLf

            Catch ex As Exception
                throw new Exception '''
            End Try

         End Sub
            
         '3.
         Private Sub ShowData()
                Try
                    If ArrRAW.Count > 0 Then

                        ''''Remove dupplicate value
                        For j As Int32 = 0 To ArrRAW.Count - 1
                            valueX = ArrRAW(j)

                            If String.IsNullOrEmpty(valueX) Then
                                Continue For
                            End If

                            If Not IsNumeric(valueX) And CType(valueX, Double) < 0 Then
                                Continue For
                            End If

                            If CType(valueX, Double) < 35.00 OrElse CType(valueX, Double) > 55.00 Then
                                Continue For
                            End If

                            If Not ArrREAL.Contains(valueX) Then
                                ArrREAL.Add(valueX)
                            End If
                        Next


                        'get lastest values ==>> My question will focus here.
                        
                        ''''If ArrRAW.Count>=3 Then '''Only completed for first round.
                            ''''Value1 = ArrREAL(1) ''Ignore value at index 0 when arr = 3
                            '''Value2 = ArrREAL(2)
                            
                        ''==>>One question here. How to separate arr = 3 or 4? -> when reset or first time using the machine is always =3 but no signal can present this situation.
                        
                        If ArrREAL.Count = 4 Then '''First round will never complete
                        
                            Value1 = ArrREAL(2) ''Ignore value at index 1 & 0 when arr = 4
                            Value2 = ArrREAL(3)

                            'Assign to UI
                            label1.Text = Value1.ToString()
                            label2.Text = Value2.ToString()

                            Timer1.Enabled = False

                            Call SaveDB(Value1,Value2)

                            ArrRAW.Clear()
                            ArrREAL.Clear()

                        End If

                    End If

                Catch ex As Exception
                    Return
                End Try

            End Sub

先感谢您。

标签: vb.net

解决方案


如果您仅在第一次获得 3 个值,则声明一个布尔值以跟踪这是第一批还是后续批次... – Idle_Mind

如何检测它是第一批?能给我举个例子吗?——哈里娜·艾达

我在想也许是这样的?

Private FirstBatch As Boolean = True

Private Sub ShowData()

    ' ... other code ...

    If FirstBatch Then
        If ArrREAL.Count >= 3 Then
            FirstBatch = False

            ' ... process first batch ...

        End If
    Else
        If ArrREAL.Count = 4 Then

            ' ... process other batches here ...

        End If
    End If

    ' ... other code ...

End Sub

推荐阅读