problem in application
nico
Posts: 28
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]
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
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
The Try Catch is a generic exception trap but should work for you
Jeff T.
·
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