Public Class Form
Public RXD$
Public Temp_val
Public Humi_val
Dim auto_temp_crt = 0
Dim auto_humi_crt = 0
Dim set_t
Dim set_h
Private Sub smarthome_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each sp As String In My.Computer.Ports.SerialPortNames
ComboBox1.Items.Add(sp)
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
SerialPort1.PortName = ComboBox1.Items(ComboBox1.SelectedIndex)
SerialPort1.Open()
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
RXD$ = SerialPort1.ReadExisting()
Dim DataID
If RXD$ <> "" Then
DataID = Mid(RXD, 1, 1)
If DataID = "H" Then
Humi_val = Mid(RXD, 2, 5)
Lab_h.Text = Humi_val
End If
End If
If RXD$ <> "" Then
DataID = Mid(RXD, 1, 1)
If DataID = "T" Then
Temp_val = Mid(RXD, 2, 5)
Lab_t.Text = Temp_val
End If
End If
End Sub
Private Sub ToolStripLabel1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel1.Click
If ToolStripLabel1.Text = "啟動溫度控制" Then
auto_temp_crt = 1
ToolStripLabel1.Text = "關閉溫度控制"
Else
auto_temp_crt = 0
ToolStripLabel1.Text = "啟動溫度控制"
End If
End Sub
Private Sub ToolStripLabel2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel2.Click
If ToolStripLabel2.Text = "啟動濕度控制" Then
auto_humi_crt = 1
ToolStripLabel2.Text = "關閉濕度控制"
Else
auto_humi_crt = 0
ToolStripLabel2.Text = "啟動濕度控制"
End If
End Sub
Private Sub SendCom(ByVal sendstr)
If SerialPort1.IsOpen Then
SerialPort1.Write(sendstr)
End If
End Sub
Private Sub ToolStripLabel3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel3.Click
Dim set_t = InputBox("請輸入設定溫度" & vbCrLf & "延遲發生時間2秒", "模擬數值", 0)
Label4.Text = set_t
If auto_temp_crt = 1 Then
If Lab_t.Text < set_t Then
PictureBox2.Image = My.Resources.風?扇Θ?停?
Button2.Text = "風扇關閉?"
SendCom(“4")
Else
PictureBox1.Image = My.Resources.風?扇Θ?轉羇
Button2.Text = "風扇開啟"
SendCom(“3")
End If
End If
End Sub
Private Sub ToolStripLabel4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel4.Click
Dim set_h = InputBox("請輸入設定濕度" & vbCrLf & "延遲發生時間2秒", "模擬數值", 0)
Label5.Text = set_h
If auto_humi_crt = 1 Then
If Lab_h.Text < set_h Then
PictureBox3.Image = My.Resources.加濕器關
Button3.Text = "加濕器關閉"
SendCom(“6")
Else
PictureBox3.Image = My.Resources.加濕器開
Button3.Text = "加濕器開啟"
SendCom(“5")
End If
End If
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "客廳開燈" Then
PictureBox1.Image = My.Resources.客廳開燈
Button1.Text = "客廳關燈"
SendCom("1")
Else
PictureBox1.Image = My.Resources.客廳關燈
Button1.Text = "客廳開燈"
SendCom("2")
End If
End Sub
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Button2.Text = "風扇開啟" Then
PictureBox2.Image = My.Resources.風扇轉
Button2.Text = "風扇關閉"
SendCom("3")
Else
PictureBox2.Image = My.Resources.風扇停
Button2.Text = "風扇開啟"
SendCom("4")
End If
End Sub
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Button3.Text = "加濕器開啟" Then
PictureBox3.Image = My.Resources.加濕器開
Button3.Text = "加濕器關閉?"
SendCom("5")
Else
PictureBox3.Image = My.Resources.加濕器關
Button3.Text = "加濕器開啟?"
SendCom("6")
End If
End Sub
End Class
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/283329.html
標籤:單片機/工控
上一篇:救命啊啊啊啊啊
