Shop OBEX P1 Docs P2 Docs Learn Events
problem in application — Parallax Forums

problem in application

niconico Posts: 28
edited 2009-07-17 00:43 in BASIC Stamp
hi all,
I have a code to read readings from the SHT11 sensor from the bs2 and display it on vb2005. I am able to read the readings but am unable to stop it. The vb's application seems to hang. However, it is still able to display the readings only thing is that the stop button I created is not working. I do not know if the problem is due to·my bs2 code or vb code. I'll display the codes here and upload it too.

VB codes

Public
Class Form1

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

Timer1.Enabled = False

If SerialPort1.IsOpen Then

SerialPort1.Close()

End If

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Dim Data As String = SerialPort1.ReadLine

Dim Temperature As String

Dim Humidity As String

Temperature = Data.Substring(0, 4)

Humidity = Data.Substring(4, 4)

txtTemp.Text = Temperature

txtHumid.Text = Humidity

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

End
Class

BS2·codes

SEROUT 16,16468,[noparse][[/noparse]DEC (tC / 10), ".", DEC1 tC, DEC (rhTrue / 10), ".", DEC1 rhTrue]

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-07-16 18:26
    Hi nico, the line that says Dim Data As String=SerialPort1.ReadLine is fine but for the instruction to complete it requires a termination character , the default character is the Newline character which is represented as decimal value 10. So the last·value the Stamp should transmit is 10

    Like this
    SEROUT 16,16468,[noparse]/noparse]DEC1 (tC / 10), ".", DEC1 tC, DEC1 (rhTrue / 10), ".", DEC1 rhTrue,[b][color=red]10[/color][/b

    You will notice I have specified DEC1 for each of the transmitted values , this way I know exactly how long my string is going to be. If this is not right you can adjust to DEC2 or whatever you want but you must adjust the VB code to suit the string length.

    This would be the VB code for the above string length
    Temperature = Data.Substring(0, 3)
    Humidity = Data.Substring(3, 3)

    Secondly to prevent any hang ups when VB reads data it is best to give the Serial Port a time out value
    Finally so you don't try and read from the port when it's closed (that will throw an error) place the close instruction after the port has stopped reading

    Like this
    [color=#0000ff][color=#0000ff]Dim[/color][/color] Stop_Rx [color=#0000ff][color=#0000ff]As[/color][/color] [color=#0000ff][color=#0000ff]Boolean[/color][/color]
     
    [color=#0000ff][color=#0000ff]Private[/color][/color] [color=#0000ff][color=#0000ff]Sub[/color][/color] btnStop_Click([color=#0000ff][color=#0000ff]ByVal[/color][/color] sender [color=#0000ff][color=#0000ff]As[/color][/color] System.Object, [color=#0000ff][color=#0000ff]ByVal[/color][/color] e [color=#0000ff][color=#0000ff]As[/color][/color] System.EventArgs) [color=#0000ff][color=#0000ff]Handles[/color][/color] btnStop.Click
    Stop_Rx = [color=#0000ff][color=#0000ff]True[/color][/color]
    [color=#0000ff][color=#0000ff]End[/color][/color] [color=#0000ff][color=#0000ff]Sub[/color][/color]
     
    [color=#0000ff][color=#0000ff]Private[/color][/color] [color=#0000ff][color=#0000ff]Sub[/color][/color] Timer1_Tick([color=#0000ff][color=#0000ff]ByVal[/color][/color] sender [color=#0000ff][color=#0000ff]As[/color][/color] System.Object, [color=#0000ff][color=#0000ff]ByVal[/color][/color] e [color=#0000ff][color=#0000ff]As[/color][/color] System.EventArgs) [color=#0000ff][color=#0000ff]Handles[/color][/color] Timer1.Tick
     
    SerialPort1.ReadTimeout = 100
     
    [color=#0000ff][color=#0000ff]Try[/color][/color]
     
    [color=#0000ff][color=#0000ff]Dim[/color][/color] Data [color=#0000ff][color=#0000ff]As[/color][/color] [color=#0000ff][color=#0000ff]String[/color][/color] = SerialPort1.ReadLine
    [color=#0000ff][color=#0000ff]Dim[/color][/color] Temperature [color=#0000ff][color=#0000ff]As[/color][/color] [color=#0000ff][color=#0000ff]String[/color][/color]
    [color=#0000ff][color=#0000ff]Dim[/color][/color] Humidity [color=#0000ff][color=#0000ff]As[/color][/color] [color=#0000ff][color=#0000ff]String[/color][/color]
    Temperature = Data.Substring(0, 3)
    Humidity = Data.Substring(3, 3)
    txtTemp.Text = Temperature
    txtHumid.Text = Humidity
     
    [color=#0000ff][color=#0000ff]Catch[/color][/color] ex [color=#0000ff][color=#0000ff]As[/color][/color] Exception
     
    [color=#0000ff][color=#0000ff]End[/color][/color] [color=#0000ff][color=#0000ff]Try[/color][/color]
     
    [color=#0000ff][color=#0000ff]If[/color][/color] Stop_Rx = [color=#0000ff][color=#0000ff]True[/color][/color] [color=#0000ff][color=#0000ff]Then[/color][/color]
    Timer1.Enabled = [color=#0000ff][color=#0000ff]False[/color][/color]
    SerialPort1.Close()
    [color=#0000ff][color=#0000ff]End[/color][/color] [color=#0000ff][color=#0000ff]If[/color][/color]
     
    [color=#0000ff][color=#0000ff]End[/color][/color] [color=#0000ff][color=#0000ff]Sub[/color][/color]
    
    

    The Try Catch is a generic exception trap but should work for you

    Jeff T.

    ·
  • dev/nulldev/null Posts: 381
    edited 2009-07-17 00:43
    You can also specify the line terminator with
    SerialPort1.NewLine = Chr(13)

    and then send

    SEROUT 16,16468,[noparse][[/noparse]DEC1 (tC / 10), ".", DEC1 tC, DEC1 (rhTrue / 10), ".", DEC1 rhTrue, CR]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
Sign In or Register to comment.