串行端口数据仅在设置断点时才接收

时间:2014-02-10 13:42:43

标签: vb.net winforms serial-port

我有一个启动超声波风量计的程序,然后在5秒后请求读数。当我在Data Received处理程序中放置断点时,正确处理从测风仪返回的数据,但是如果我没有断点,则忽略数据。代码如下(在键盘上按下F2时调用startWG)


    Dim WGCom As New SerialPort
    Private Function initWG() As Boolean

        Dim WGPort = My.Settings.WGCom
        If Not (WGCom.IsOpen) Then
            Try
                WGCom.PortName = "COM" & WGPort
                WGCom.Parity = Parity.Even
                WGCom.DataBits = 7
                WGCom.StopBits = StopBits.One
                WGCom.Handshake = Handshake.None
                'WGCom.ReadTimeout = 3000
                WGCom.WriteTimeout = 50000
                WGCom.Open()

            Catch ex As InvalidOperationException
                MessageBox.Show(ex.Message)

            Catch ex As UnauthorizedAccessException
                MessageBox.Show(ex.Message)

            Catch ex As System.IO.IOException
                MessageBox.Show(ex.Message)

            End Try
        End If

        If (WGCom.IsOpen) Then
            Return True
        Else
            Return False
        End If
    End Function
#If (WGPort > 0) Then

#End If
    'What to do with the wind gauge data when it is received.
    Public Sub DataReceivedHandler(
                        sender As Object,
                        e As SerialDataReceivedEventArgs)

        Dim sp As SerialPort = CType(sender, SerialPort)
        Dim indata As String = sp.ReadExisting()
        'MsgBox("Seen Data from WG " & indata)
        If (indata.Length  0) Then
                reading = indata.Substring(plus - 1, 5)
                read = True
            End If
        Catch ex As Exception

        End Try
        Try
            minus = InStr(indata, "-")
            If (minus > 0) Then
                reading = indata.Substring(minus - 1, 5)
                read = True
            End If
        Catch ex As Exception

        End Try

        If (read) Then
            WGReading = reading
            ' MsgBox(reading)
            WGHasRead = True
            read = False
            plus = 0
            minus = 0
            Dim forClass As New WGReads
            forClass.Reading = reading
            SerialLog.WGReadings.Add(forClass)
            RaiseEvent PropertyChanged("DataReceivedHandler", New PropertyChangedEventArgs("LastWG"))
            WGFill(wgfield, hjevent)
        End If

    End Sub

    Public Sub WGStart(wg() As String, hjevents As hjCompetition)
        wgfield = wg
        hjevent = hjevents
        If (initWG()) Then
            AddHandler WGCom.DataReceived, AddressOf DataReceivedHandler

            Dim initBuffer(9) As Byte
            initBuffer(0) = &H1
            initBuffer(1) = &H13
            initBuffer(2) = &H43
            initBuffer(3) = &H57
            initBuffer(4) = &H49
            initBuffer(5) = &H2
            initBuffer(6) = &H30
            initBuffer(7) = &H35
            initBuffer(8) = &H4
            Try
                WGCom.Write(initBuffer, 0, initBuffer.Length)
            Catch ex As System.TimeoutException
            End Try
            'After init wait for the wind gauge to catch up
            System.Threading.Thread.Sleep(100)
            Dim outputBuffer1(9) As Byte
            outputBuffer1(0) = &H1
            outputBuffer1(1) = &H13
            outputBuffer1(2) = &H43
            outputBuffer1(3) = &H57
            outputBuffer1(4) = &H53
            outputBuffer1(5) = &H2
            outputBuffer1(6) = &H30
            outputBuffer1(7) = &H30
            outputBuffer1(8) = &H4
            Try
                WGCom.Write(outputBuffer1, 0, outputBuffer1.Length)
            Catch ex As System.TimeoutException
            End Try
            'Wait for the wind gauge to finish
            wait(5500)

            'Add a handler for when data is received from the Wind Gauge
            AddHandler WGCom.DataReceived, AddressOf DataReceivedHandler

            'Get the reading from the wind gauge
            Dim getBuffer(9) As Byte
            getBuffer(0) = &H1
            getBuffer(1) = &H13
            getBuffer(2) = &H43
            getBuffer(3) = &H57
            getBuffer(4) = &H4F
            getBuffer(5) = &H2
            getBuffer(6) = &H30
            getBuffer(7) = &H30
            getBuffer(8) = &H4
            Try
                WGCom.Write(getBuffer, 0, getBuffer.Length)

            Catch ex As System.TimeoutException
            End Try
            'closeCom()

        End If
    End Sub

Data Received处理程序中断点的位置无关紧要,只要有一个断点即可。如果断点位于WGStart Sub中,它也不起作用。

断点不应该改变程序的执行方式吗?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

添加等待(100)已修复此问题。对于寻找类似问题解决方案的其他人来说,wait子代码如下所示

Private Sub wait(ByVal interval As Integer)
   Dim sw As New Stopwatch
      sw.Start()
      Do While sw.ElapsedMilliseconds < interval
      ' Allows UI to remain responsive
        Application.DoEvents()
      Loop
   sw.Stop() 
End Sub