0

我目前正在做一个关于降雨监测系统的小项目。对于这个项目,我想通过使用 Visual C# 2008 中的计时器来显示是大雨、中雨、小雨还是无雨。这个项目由 6 个水位传感器组成,可以检测每一层的水。通过使用计时器:

如果水在不到 30 秒内达到 6 级,则为大雨。如果水位超过 60 秒达到 6 级,则为小雨。如果水在 60 秒内达到 6 级,则为中雨。如果在 1 级未检测到水,则为无雨。

目前,我已经做了一些程序来检测每一层的水。这就是我的代码的样子。

if (e.KeyChar.ToString() == "1")
    {
        PictureBox1.BackgroundImage = Properties.Resources.Level0;
        label8.BackColor = Color.LimeGreen;
        Label1.BackColor = Color.White;
        Label2.BackColor = Color.White;
        Label3.BackColor = Color.White;
        Label4.BackColor = Color.White;
        Label5.BackColor = Color.White;
        Label7.Text = "Rain Detected";

        sql = "INSERT INTO RMSDatabase(Level_0)values('" + DateTime.Now + "')";
        da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
        da.Fill(ds, "RMSDatabase");

    }
    else if (e.KeyChar.ToString() == "2")
    {
        PictureBox1.BackgroundImage = Properties.Resources.Level1;
        Label1.BackColor = Color.LightGreen;
        Label2.BackColor = Color.White;
        Label3.BackColor = Color.White;
        Label4.BackColor = Color.White;
        Label5.BackColor = Color.White;
        Label7.Text = "1 Litre";

        sql = "INSERT INTO RMSDatabase(Level_1)values('" + DateTime.Now + "')";
        da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
        da.Fill(ds, "RMSDatabase");

    }
    else if (e.KeyChar.ToString() == "3")
    {
        PictureBox1.BackgroundImage = Properties.Resources.Level2;
        Label2.BackColor = Color.GreenYellow;
        Label3.BackColor = Color.White;
        Label4.BackColor = Color.White;
        Label5.BackColor = Color.White;
        Label7.Text = "2 Litre";

        sql = "INSERT INTO RMSDatabase(Level_2)values('" + DateTime.Now + "')";
        da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
        da.Fill(ds, "RMSDatabase");

    }
    else if (e.KeyChar.ToString() == "4")
    {
        PictureBox1.BackgroundImage = Properties.Resources.Level3;
        Label3.BackColor = Color.Yellow;
        Label4.BackColor = Color.White;
        Label5.BackColor = Color.White;
        Label7.Text = "3 Litre";

        sql = "INSERT INTO RMSDatabase(Level_3)values('" + DateTime.Now + "')";
        da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
        da.Fill(ds, "RMSDatabase");

    }
    else if (e.KeyChar.ToString() == "5")
    {
        PictureBox1.BackgroundImage = Properties.Resources.Level4;
        Label4.BackColor = Color.OrangeRed;
        Label5.BackColor = Color.White;
        Label7.Text = "4 Litre";

        sql = "INSERT INTO RMSDatabase(Level_4)values('" + DateTime.Now + "')";
        da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
        da.Fill(ds, "RMSDatabase");

    }
    else if (e.KeyChar.ToString() == "6")
    {
        PictureBox1.BackgroundImage = Properties.Resources.Level5;
        Label5.BackColor = Color.Red;
        Label7.Text = "5 Litre";
        if (Label7.Text == "5 Litre")
        {
            My.Computer.Audio.Play(Properties.Resources.alarm, Microsoft.VisualBasic.AudioPlayMode.BackgroundLoop);
            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }
            try
            {
                serialPort.PortName = Form2.DefaultInstance.ComboBox1.Text;
                serialPort.BaudRate = 96000;
                serialPort.Parity = System.IO.Ports.Parity.None;
                serialPort.DataBits = 8;
                serialPort.StopBits = System.IO.Ports.StopBits.One;

                serialPort.Open();
                serialPort.Write("ATDT" + Form2.DefaultInstance.ComboBox2.Text + Environment.NewLine);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }

            MessageBox.Show("Alarm system initiated!", "Warning!!", MessageBoxButtons.OK);

            My.Computer.Audio.Stop();
            serialPort.Close();

            sql = "INSERT INTO RMSDatabase(Level_5, Contact_Number)values('" + DateTime.Now + "', '" + Form2.DefaultInstance.ComboBox2.Text + "')";
            da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
            da.Fill(ds, "RMSDatabase");

        }
    }
    else if (e.KeyChar.ToString() == null)
    {
        PictureBox1.BackgroundImage = Properties.Resources.Level0;
    }

大雨、中雨、小雨和无雨将使用标签显示。有人可以根据我上面给出的条件帮助我如何设置计时器吗?

这是有效的编辑版本。

Dim timercount As integer = 0
If e.KeyChar.ToString = "1" Then
        Timer2.Interval = 1000 '1000 milisecond is equal to 1 second
        Timer2.Enabled = True 'Start the timer
        PictureBox1.BackgroundImage = My.Resources.Level0
        Label8.BackColor = Color.LimeGreen
        Label1.BackColor = Color.White
        Label2.BackColor = Color.White
        Label3.BackColor = Color.White
        Label4.BackColor = Color.White
        Label5.BackColor = Color.White
        Label9.Text = "Rain Detected"

        sql = "INSERT INTO Table1(Level_0)values('" & DateTime.Now & "')"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Table1")

    ElseIf e.KeyChar.ToString = "2" Then
        PictureBox1.BackgroundImage = My.Resources.Level1
        Label1.BackColor = Color.LightGreen
        Label2.BackColor = Color.White
        Label3.BackColor = Color.White
        Label4.BackColor = Color.White
        Label5.BackColor = Color.White
        Label7.Text = "1 Litre"

        sql = "INSERT INTO Table1(Level_1)values('" & DateTime.Now & "')"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Table1")

    ElseIf e.KeyChar.ToString = "3" Then
        PictureBox1.BackgroundImage = My.Resources.Level2
        Label2.BackColor = Color.GreenYellow
        Label3.BackColor = Color.White
        Label4.BackColor = Color.White
        Label5.BackColor = Color.White
        Label7.Text = "2 Litre"

        sql = "INSERT INTO Table1(Level_2)values('" & DateTime.Now & "')"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Table1")

    ElseIf e.KeyChar.ToString = "4" Then
        PictureBox1.BackgroundImage = My.Resources.Level3
        Label3.BackColor = Color.Yellow
        Label4.BackColor = Color.White
        Label5.BackColor = Color.White
        Label7.Text = "3 Litre"

        sql = "INSERT INTO Table1(Level_3)values('" & DateTime.Now & "')"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Table1")

    ElseIf e.KeyChar.ToString = "5" Then
        PictureBox1.BackgroundImage = My.Resources.Level4
        Label4.BackColor = Color.OrangeRed
        Label5.BackColor = Color.White
        Label7.Text = "4 Litre"

        sql = "INSERT INTO Table1(Level_4)values('" & Date.Now & "')"
        da = New System.Data.OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Table1")

    ElseIf (e.KeyChar.ToString() = "6" And timercount > 60) Then
        PictureBox1.BackgroundImage = My.Resources.Level5
        Label5.BackColor = Color.Red
        Label7.Text = "5 Litre"
        Label9.Text = "Light Rain"

        sql = "INSERT INTO Table1(Level_5)values('" & Date.Now & "')"
        da = New System.Data.OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Table1")

    ElseIf (e.KeyChar.ToString() = "6" AndAlso timercount > 30 And timercount <= 60) Then
        PictureBox1.BackgroundImage = My.Resources.Level5
        Label5.BackColor = Color.Red
        Label7.Text = "5 Litre"
        Label9.Text = "Medium Rain"

        sql = "INSERT INTO Table1(Level_5)values('" & Date.Now & "')"
        da = New System.Data.OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Table1")

    ElseIf (e.KeyChar.ToString = "6" And timercount <= 30) Then
        PictureBox1.BackgroundImage = My.Resources.Level5
        Label5.BackColor = Color.Red
        Label7.Text = "5 Litre"
        Label9.Text = "Heavy Rain"

        If Label9.Text = "Heavy Rain" Then
            My.Computer.Audio.Play(My.Resources.alarm, AudioPlayMode.BackgroundLoop)
            If serialPort.IsOpen Then
                serialPort.Close()
            End If
            Try
                With serialPort
                    .PortName = Form2.ComboBox1.Text
                    .BaudRate = 96000
                    .Parity = IO.Ports.Parity.None
                    .DataBits = 8
                    .StopBits = IO.Ports.StopBits.One
                End With

                serialPort.Open()
                serialPort.Write("ATDT" & Form2.ComboBox2.Text & vbCrLf)

            Catch ex As Exception
                MsgBox(ex.ToString)

            End Try

            MsgBox("Alarm system initiated!", MessageBoxIcon.Warning, "Warning!!")

            My.Computer.Audio.Stop()
            serialPort.Close()

            sql = "INSERT INTO Table1(Level_5, Contact_Number)values('" & DateTime.Now & "', '" & Form2.ComboBox2.Text & "')"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "Table1")

        End If
    End If
End Sub

这就是我尝试在 If ElseIf 语句中使用 IF 语句的方法..但它不起作用。

Dim timercount As integer = 0
Dim timercount2 As integer = 10
If e.KeyChar.ToString = "1" Then
        Timer2.Interval = 1000 '1000 milisecond is equal to 1 second
        Timer2.Enabled = True 'Start the timer
        Timer3.Interval = 1000
        Timer3.Enabled = True
        PictureBox1.BackgroundImage = My.Resources.Level0
        Label8.BackColor = Color.LimeGreen
        Label1.BackColor = Color.White
        Label2.BackColor = Color.White
        Label3.BackColor = Color.White
        Label4.BackColor = Color.White
        Label5.BackColor = Color.White
        Label9.Text = "Rain Detected"

        sql = "INSERT INTO Table1(Level_0)values('" & DateTime.Now & "')"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Table1")

        If timercount2 = 0 Then
            Timer2.Enabled = False
            Timer3.Enabled = False
            Label8.BackColor = Color.White
            Label9.Text = "Just a waterdrop"
        End If
    End If
End Sub
4

1 回答 1

0

好吧,根据您的代码,您似乎正在使用 Windows 窗体。添加 System.Windows.Form.Timer 的实例,对其进行初始化并为厚事件添加事件处理程序。在事件处理程序代码中,做任何你想做的事情。

看到这个线程: Winforms Timer for Dummies

但是,我强烈建议创建一个 Weather 类,并使用事件来指示下雨情况。我说的是使用您的代码所缺少的面向对象的方法。

于 2013-04-29T20:59:56.080 回答