problems in ultrasonic sensor
nico
Posts: 28
Hi, I have try reading the ultrasonic sensor and displaying it on VB. However, I've encounter·2 problems which is the change of the distance in the displaying part of VB is very slow. (eg. When I adjust the obstacle in front of the ultrasonic sensors, the vb took awhile to response.)· Another problem is that by using the following codes, if my readings are less than a 3 digit number, the display will display 0s in front. (eg. readings of 2 will be displayed as 002) How do i solve this ? My codes are as follow.
Public Class Form1
Dim Stop_Rx As Boolean
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
Timer1.Enabled = True
SerialPort1.Open()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Stop_Rx = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
SerialPort1.ReadTimeout = 100
Try
Dim Data As String = SerialPort1.ReadLine
Dim·reading1 As String
Dim·reading2 As String
reading1·= Data.Substring(0, 3)
reading2·= Data.Substring(3, 3)
txt1.Text = reading1
txt2.Text = reading2
Catch ex As Exception
End Try
If Stop_Rx = True Then
Timer1.Enabled = False
SerialPort1.Close()
End If
End Sub
End Class
BScode
SEROUT 16,16468,[noparse][[/noparse]DEC3 cmDistance,DEC3 cmDistance2]
SEROUT 16,16468,[noparse][[/noparse]LF]
Public Class Form1
Dim Stop_Rx As Boolean
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
Timer1.Enabled = True
SerialPort1.Open()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Stop_Rx = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
SerialPort1.ReadTimeout = 100
Try
Dim Data As String = SerialPort1.ReadLine
Dim·reading1 As String
Dim·reading2 As String
reading1·= Data.Substring(0, 3)
reading2·= Data.Substring(3, 3)
txt1.Text = reading1
txt2.Text = reading2
Catch ex As Exception
End Try
If Stop_Rx = True Then
Timer1.Enabled = False
SerialPort1.Close()
End If
End Sub
End Class
BScode
SEROUT 16,16468,[noparse][[/noparse]DEC3 cmDistance,DEC3 cmDistance2]
SEROUT 16,16468,[noparse][[/noparse]LF]
bs2
3K
Comments
Solution:
1) Double-click the SerialPort control and write:
... and so on.
2) Line terminator
To be sure that the line terminator is LF, write in your Form1_Load:
3) To remove the leading 0's, write:
If either reading1 or reading2 is not a number, you will get an exception.
Your timer is of no use, delete it.
Good luck
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't worry. Be happy
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't worry. Be happy
I have tried your solutions but am having errors. I try to use the try and catch exception to catch error but then when I try to read the data from the sensor the textbox1 and textbox2 display nothing. I've printscreen the error.
Here is what you do:
0) Go to your code
1) Delete the SerialPort1_DataReceived Sub.
2) Select SerialPort1 from the dropdown menu in the upper left corner (see attachment)
3) Select the DataReceived event in the right hand corner
4) Write the code.
Do this and tell me what happens.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't worry. Be happy
I went on to www.msdn.com to search and it says that "The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread."
I think they meant by using that I have to make use of the invoke to display the data on my text box. I went to search about this invoke, but there aren't any useful example for my case. [noparse]:([/noparse]
The Invoke method is explained here: msdn.microsoft.com/en-us/library/zyzhdc6b.aspx.
See if you can figure it out.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't worry. Be happy