标题:在vb.net中怎样能使串口一选择好就可以接收传感器数值?
只看楼主
cury
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-11-4
结帖率:0
 问题点数:0 回复次数:0 
在vb.net中怎样能使串口一选择好就可以接收传感器数值?


我的这个程序是点了某个按钮之后才会接收传感器数值的,怎样能使串口一选择好就可以接收传感器数值?
程序代码:
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
搜索更多相关主题的帖子: Sub ByVal End If Text 
2017-08-08 19:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-479861-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.188111 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved