Friday, 9 August 2013

Com Port Values changed when moved to form

Com Port Values changed when moved to form

I have a distance tracking laser hooked up to my COM1 port and I am using
these settings to initialize the connection:
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
I then handle the received data with this function: (Reading is a global
variable of type double and ErrorMessage is a global variable of type
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
From here I want to get the Reading value onto my Form. I cannot do it
directly in the method because it is not thread-safe. So my current
attempted solution is to have a timer check the value of Reading every
tenth of a second and add to the Form. I do so in this tick method:
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
The code above seems to work perfectly when stepping through with the the
debugger. However when the Form is displayed without the debugger the
number displayed in CurrentPosText.Text is completely different from the
expected value from the laser.
I checked to make sure the laser values were correct by issuing the same
commands through Putty.exe.
Here were the consistent results and settings from Putty. (Follow the link
and Watch the video)
TLDR Watch this video!

How and why does the number I recieve from the COM port change when
displayed on the form without the debugger?

No comments:

Post a Comment