Shop OBEX P1 Docs P2 Docs Learn Events
problems in ultrasonic sensor — Parallax Forums

problems in ultrasonic sensor

niconico Posts: 28
edited 2009-07-23 09:07 in BASIC Stamp
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]


Comments

  • dev/nulldev/null Posts: 381
    edited 2009-07-19 09:37
    Your problem with time lag is because you first use a timer (with some tick you didn't specify) and then a serial read with timout (which might read when the Stamp is not sending).

    Solution:

    1) Double-click the SerialPort control and write:
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim Data As String = SerialPort1.ReadLine
            Dim reading1 As String
            Dim reading2 As String
    
            reading1 = Cdbl(Data.Substring(0, 3))
            reading2 = Cdbl(Data.Substring(3, 3))
            txt1.Text = reading1
            txt2.Text = reading2
    
         End Sub
    
    



    ... and so on.

    2) Line terminator
    To be sure that the line terminator is LF, write in your Form1_Load:
           SerialPort1.NewLine = Chr(10)
    
    




    3) To remove the leading 0's, write:
    reading1 = Cdbl(Data.Substring(0, 3))
    reading2 = Cdbl( Data.Substring(3, 3))
    
    



    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
  • dev/nulldev/null Posts: 381
    edited 2009-07-19 09:41
    Actually, it's more correct to write:
    reading1 = Cstr(Cdbl(Data.Substring(0, 3)))
    reading2 = Cstr( Cdbl(Data.Substring(3, 3)))
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • niconico Posts: 28
    edited 2009-07-21 09:49
    hi dev/null,
    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.
    1280 x 800 - 146K
  • niconico Posts: 28
    edited 2009-07-21 09:56
    Oh ya and 1 more thing I am unable to double click on the SerialPort control to write your codes, so I paste the codes in it, so i doubt it'll work
  • dev/nulldev/null Posts: 381
    edited 2009-07-21 16:06
    I am surprised that you get this kind of error. But OK.

    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
    1231 x 693 - 99K
  • niconico Posts: 28
    edited 2009-07-22 03:12
    Yes I'm able to get to the event handler now. However, there is an error when I run the program and click on the Read button. It states (Cross-thread operation not valid: Control 'txt1' accessed from a thread other than the thread it was created on.)
    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]
  • dev/nulldev/null Posts: 381
    edited 2009-07-22 11:08
    Funny that you get this error. You can avoid it without using Invoke, but then I'd have to see your whole project setup.
    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
  • niconico Posts: 28
    edited 2009-07-23 09:07
    thank you very much dev/null. I'm able to display the readings using that method already.
Sign In or Register to comment.