2014-02-10 22 views
0

我有一个程序启动超声波风速计,然后在5秒后请求读数。当我在数据接收处理程序中放置一个断点时,从测风仪返回的数据会被正确处理,但是如果我没有适当的断点,数据将被忽略。代码如下(当按下键盘上的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 

这不要紧,在数据断点接收处理程序,但只要有一个。如果断点位于WGStart Sub中,它也不起作用。

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

在此先感谢您的帮助。

+0

如果您取消注释DataReceivedHandler顶部的MsgBox是否曾出现? –

+1

你在哪一行放置断点?我在一段时间内没有完成串口工作,但我的猜测是因为接收事件是在第一眼看到数据时发出的,而不是在接收到所有消息时,您的反应太快/不等待消息完成。通过添加断点,您可以给缓冲区额外的时间来填充,并且您将看到整个消息。 – CResults

+0

我会在两个Try块中报告异常消息,这些块在异常时不做任何事情。 – rheitzman

回答

0

加入等待(100)已解决问题。等待的代码如下其他寻找类似问题的解决方案

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 
相关问题