只有本站会员才能查看附件,请 登录
我的这个程序是点了某个按钮之后才会接收传感器数值的,怎样能使串口一选择好就可以接收传感器数值?
程序代码:
Public Class smarthome
Public RXD$
Public LS_val
Dim auto_ls_crt = 0
Dim setls
Private Sub smarthome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each sp As String In (sp)
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(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 = "L" Then
LS_val = Mid(RXD, 2, 4)
lb_LS.Text = LS_val
End If
End If
Dim ls = Val(Mid(lb_LS.Text, 1, 1)) * 100 + Val(Mid(lb_LS.Text, 2, 1)) * 10 + Val(Mid(lb_LS.Text, 3, 1))
If auto_ls_crt = 1 Then
If lb_LS.Text < setls Then
PictureBox1.Image = My.Resources.客厅开灯
Button1.Text = "客厅_关灯"
SendCom("E")
Else
PictureBox1.Image = My.Resources.客厅关灯
Button1.Text = "客厅_开灯"
SendCom("F")
End If
End If
End Sub
Private Sub ToolStripLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel1.Click
If ToolStripLabel1.Text = "启动光控" Then
auto_ls_crt = 1
ToolStripLabel1.Text = "关闭光控"
Else
auto_ls_crt = 0
ToolStripLabel1.Text = "启动光控"
End If
End Sub
Private Sub ToolStripLabel3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel3.Click
setls = InputBox("请输入设定光照度" & vbCrLf & "延迟发生时间2秒", "模拟数值", 0)
Label2.Text = setls
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "客厅_开灯" Then
PictureBox1.Image = My.Resources.客厅开灯
Button1.Text = "客厅_关灯"
If SerialPort1.IsOpen Then
SendCom("1")
End If
Else
PictureBox1.Image = My.Resources.客厅关灯
Button1.Text = "客厅_开灯"
If SerialPort1.IsOpen Then
SendCom("2")
End If
End If
End Sub
Private Sub SendCom(ByVal sendstr)
If SerialPort1.IsOpen Then
SerialPort1.Write(sendstr)
End If
End Sub
Private Sub Button2_Click(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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Button3.Text = "台灯_开启" Then
PictureBox3.Image = My.Resources.台灯亮
Button3.Text = "台灯_关闭"
SendCom("6")
Else
PictureBox3.Image = My.Resources.台灯灭
Button3.Text = "台灯_开启"
SendCom("6")
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If Button4.Text = "电视_开启" Then
PictureBox4.Image = My.Resources.电视开
SendCom("7")
Button4.Text = "电视_关闭"
Else
PictureBox4.Image = My.Resources.电视关
Button4.Text = "电视_开启"
SendCom("8")
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If Button5.Text = "音响_开启" Then
PictureBox5.Image = My.Resources.音响响
Button5.Text = "音响_关闭"
SendCom("9")
Else
PictureBox5.Image = My.Resources.音响停
Button5.Text = "音响_开启"
SendCom("A")
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Button6.Text = "报警_启动" Then
PictureBox6.Image = My.Resources.警报响
Button6.Text = "报警_关闭"
SendCom("B")
Else
PictureBox6.Image = My.Resources.警报停
Button6.Text = "报警_启动"
SendCom("C")
End If
End Sub
End Class
Public RXD$
Public LS_val
Dim auto_ls_crt = 0
Dim setls
Private Sub smarthome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each sp As String In (sp)
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(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 = "L" Then
LS_val = Mid(RXD, 2, 4)
lb_LS.Text = LS_val
End If
End If
Dim ls = Val(Mid(lb_LS.Text, 1, 1)) * 100 + Val(Mid(lb_LS.Text, 2, 1)) * 10 + Val(Mid(lb_LS.Text, 3, 1))
If auto_ls_crt = 1 Then
If lb_LS.Text < setls Then
PictureBox1.Image = My.Resources.客厅开灯
Button1.Text = "客厅_关灯"
SendCom("E")
Else
PictureBox1.Image = My.Resources.客厅关灯
Button1.Text = "客厅_开灯"
SendCom("F")
End If
End If
End Sub
Private Sub ToolStripLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel1.Click
If ToolStripLabel1.Text = "启动光控" Then
auto_ls_crt = 1
ToolStripLabel1.Text = "关闭光控"
Else
auto_ls_crt = 0
ToolStripLabel1.Text = "启动光控"
End If
End Sub
Private Sub ToolStripLabel3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel3.Click
setls = InputBox("请输入设定光照度" & vbCrLf & "延迟发生时间2秒", "模拟数值", 0)
Label2.Text = setls
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "客厅_开灯" Then
PictureBox1.Image = My.Resources.客厅开灯
Button1.Text = "客厅_关灯"
If SerialPort1.IsOpen Then
SendCom("1")
End If
Else
PictureBox1.Image = My.Resources.客厅关灯
Button1.Text = "客厅_开灯"
If SerialPort1.IsOpen Then
SendCom("2")
End If
End If
End Sub
Private Sub SendCom(ByVal sendstr)
If SerialPort1.IsOpen Then
SerialPort1.Write(sendstr)
End If
End Sub
Private Sub Button2_Click(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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Button3.Text = "台灯_开启" Then
PictureBox3.Image = My.Resources.台灯亮
Button3.Text = "台灯_关闭"
SendCom("6")
Else
PictureBox3.Image = My.Resources.台灯灭
Button3.Text = "台灯_开启"
SendCom("6")
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If Button4.Text = "电视_开启" Then
PictureBox4.Image = My.Resources.电视开
SendCom("7")
Button4.Text = "电视_关闭"
Else
PictureBox4.Image = My.Resources.电视关
Button4.Text = "电视_开启"
SendCom("8")
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If Button5.Text = "音响_开启" Then
PictureBox5.Image = My.Resources.音响响
Button5.Text = "音响_关闭"
SendCom("9")
Else
PictureBox5.Image = My.Resources.音响停
Button5.Text = "音响_开启"
SendCom("A")
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Button6.Text = "报警_启动" Then
PictureBox6.Image = My.Resources.警报响
Button6.Text = "报警_关闭"
SendCom("B")
Else
PictureBox6.Image = My.Resources.警报停
Button6.Text = "报警_启动"
SendCom("C")
End If
End Sub
End Class