我有一个程序,该程序启动超声波风速计,然后在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中,则它也不起作用。
断点肯定不会改变程序的执行方式吗?
在此先感谢您的帮助。
添加wait(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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句