2014-02-10 24 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 
相關問題