我有一个distance tracking laser连接到我的COM1端口,我正在使用这些设置来初始化连接:
With ServoCalibrater.LaserPort
.BaudRate = 19200
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Parity = IO.Ports.Parity.None
.StopBits = IO.Ports.StopBits.One
.Close()
.Open()
.Write("dt")
End With
然后我用这个函数处理接收到的数据:(读取是double类型的全局变量,ErrorMessage是string类型的全局变量)
Private Sub LaserPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles LaserPort.DataReceived
ComRecv = True
Dim TempRead As String
TempRead = LaserPort.ReadExisting()
If Not IsNumeric(TempRead) Then
If Asc(TempRead) = 13 Then
TempRead = "*No Data*"
End If
ErrorMessage = "Laser Error " & TempRead & "... Please restart application, then turn laser off and back on."
Else
Reading = ErrorMessage
End If
End Sub
从这里开始,我希望将Reading
值添加到我的表单中。我不能直接在该方法中执行它,因为它不是线程安全的。因此,我目前尝试的解决方案是让计时器每十分之一秒检查Reading
的值并添加到表单中。我在这个tick方法中这样做:
Private Sub tmrMonitor_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMonitor.Tick
Dim MeasuredDistance As New clsDimension
Dim DesiredDistance As New clsDimension
'Check to see if we've got com with the laser so we can alert the user if not
If Not ServoCalibrater.ComRecv Then
LaserError.Text = "No communication received from the laser. Please check to make sure it's turned on."
Else
CurrentPosText.Text = Reading
Refresh()
End If
End Sub
当使用调试器时,上面的代码似乎工作得很好。 但是,如果在没有调试器的情况下显示表单,则CurrentPosText.Text中显示的数字与激光的预期值完全不同。
我通过Putty.exe发出相同的命令,检查确保激光值是否正确。
Here是Putty的一致结果和设置。 (点击链接并观看视频)
TLDR 观看this video!
如果没有调试器在表单上显示,我从COM端口收到的号码如何以及为什么会发生变化?
答案 0 :(得分:0)