Ejemplo n.º 1
0
 private void ButtonDisconnect_Click(object sender, EventArgs e)
 {
     PanelConnection.Focus();
     TimerSerial.Stop();
     SerialPort1.Close();
     ButtonDisconnect.SendToBack();
     ButtonConnect.BringToFront();
     LabelStatus.Text = "Status : Disconnect";
     PictureBoxStatusConnection.Visible   = true;
     PictureBoxStatusConnection.BackColor = Color.Red;
 }
Ejemplo n.º 2
0
        private void TimerSerial_Tick(object sender, EventArgs e)
        {
            try
            {
                StrSerialIn = SerialPort1.ReadExisting();  // --> Read incoming serial data
                var TB = new TextBox();
                TB.Multiline = true;
                TB.Text      = StrSerialIn; // --> Enter serial data into the textbox
                if (TB.Lines.Count() > 0)
                {
                    if (TB.Lines[0] == "Failed to read from DHT sensor!") // --> Check Arduino if it fails to read the DHT sensor, if this happens the connection is disconnected
                    {
                        TimerSerial.Stop();
                        SerialPort1.Close();
                        LabelStatus.Text = "Status : Disconnect";
                        ButtonDisconnect.SendToBack();
                        ButtonConnect.BringToFront();
                        PictureBoxStatusConnection.Visible   = true;
                        PictureBoxStatusConnection.BackColor = Color.Red;
                        MessageBox.Show("Failed to read from DHT sensor !!!, Please check the Hardware and Please connect again.", MessageBoxButtons.OK.ToString());
                        return;
                    }



                    StrSerialInRam = TB.Lines[0].Substring(0, 1);
                    if ((StrSerialInRam ?? "") == "H")
                    {
                        Hum  = TB.Lines[0];
                        HumL = Hum.Length;
                    }
                    else
                    {
                        Hum = Hum;
                    }

                    StrSerialInRam = TB.Lines[1].Substring(0, 1);
                    if ((StrSerialInRam ?? "") == "T")
                    {
                        Temp  = TB.Lines[1];
                        TempL = Temp.Length;
                    }
                    else
                    {
                        Temp = Temp;
                    }

                    HumResult         = Strings.Mid(Hum, 2, HumL);
                    TempResult        = Strings.Mid(Temp, 2, TempL);
                    TempToProgressBar = Conversions.ToSingle(TempResult);
                    CircularProgressBarHumidity.Value = Convert.ToInt32(HumResult);
                    CircularProgressBarHumidity.Text  = CircularProgressBarHumidity.Value + " %";
                    LabelTemperature.Text             = TempResult + " °C";

                    // -----------The process for making a progress bar using a picturebox (Vertical progress bar)-----------
                    vpb_sy = this.Map(TempToProgressBar, -20.0f, 60.0f, (float)0, (float)120);
                    if (vpb_sy > 120)
                    {
                        vpb_sy = 120;
                    }

                    if (vpb_sy < 0)
                    {
                        vpb_sy = 0;
                    }

                    PictureBoxPBTemp.Height = PictureBoxPBTempBack.Height * vpb_sy / 120;
                    vpb_ly = PictureBoxPBTempBack.Height - vpb_sy + PictureBoxPBTempBack.Location.Y;
                    PictureBoxPBTemp.Location = new Point(PictureBoxPBTemp.Location.X, vpb_ly);
                    // ------------------------------------------------------------------------------------------------------

                    // -----------Enter the temperature and humidity values into the chart-----------------------------------
                    Chart1.Series["Humidity       "].Points.AddY(HumResult);
                    if (Chart1.Series[0].Points.Count == ChartLimit)
                    {
                        Chart1.Series[0].Points.RemoveAt(0);
                    }

                    Chart2.Series["Temperature"].Points.AddY(TempResult);
                    if (Chart2.Series[0].Points.Count == ChartLimit)
                    {
                        Chart2.Series[0].Points.RemoveAt(0);
                    }
                    // ------------------------------------------------------------------------------------------------------

                    // -----------If the Then connection Is successful And running, PictureBoxStatusConnection will blink----
                    if (PictureBoxStatusConnection.Visible == true)
                    {
                        PictureBoxStatusConnection.Visible = false;
                    }
                    else if (PictureBoxStatusConnection.Visible == false)
                    {
                        PictureBoxStatusConnection.Visible = true;
                    }
                    // ------------------------------------------------------------------------------------------------------
                }
            }
            catch (Exception ex)
            {
                TimerSerial.Stop();
                SerialPort1.Close();
                LabelStatus.Text = "Status : Disconnect";
                ButtonDisconnect.SendToBack();
                ButtonConnect.BringToFront();
                PictureBoxStatusConnection.BackColor = Color.Red;
                MessageBox.Show("Please check the Hardware and Please connect again." + ex.Message);
                return;
            }
        }