Beispiel #1
0
 private void disconnect()
 {
     try
     {
         readMessageTimer.Enabled = false;
         serialMessenger.Disconnect();
     }
     catch (Exception exception)
     {
         MessageBox.Show(exception.Message);
     }
 }
Beispiel #2
0
        private void btnSetSchoolAlarm_Click(object sender, EventArgs e)
        {
            // Make sure the departure time is correct
            if (departure.Date == DateTime.Today.Date && lbCountdown.Text == "00:00:00" || departure.Day == 1 && departure.Month == 1 && departure.Year == 0001)
            {
                MessageBox.Show("Log in with your Fontys account and fill in a correct living place and show the travel info");
            }
            else // Departure time is correct
            {
                string prepTimeString = "";
                int    prepTimeInt    = Convert.ToInt32(nudPrepTime.Value);

                if (prepTimeInt == 0) // If no prep time is filled in
                {
                    MessageBox.Show("Please fill in your estimated prep time");
                }
                else
                {
                    if (prepTimeInt != 0 && prepTimeInt < 60) // If prep time is within 1 hour
                    {
                        prepTimeString = "00:" + prepTimeInt.ToString() + ":00";
                    }
                    else if (prepTimeInt >= 60 && prepTimeInt < 120) // If prep time is between 1 and 2 hours
                    {
                        prepTimeInt    = prepTimeInt - 60;
                        prepTimeString = "01:" + prepTimeInt.ToString() + ":00";
                    }
                    else if (prepTimeInt >= 120 && prepTimeInt < 180) // If prep time is between 2 and 3 hours
                    {
                        prepTimeInt    = prepTimeInt - 120;
                        prepTimeString = "02:" + prepTimeInt.ToString() + ":00";
                    }
                    else if (prepTimeInt >= 180 && prepTimeInt < 240) // If prep time is between 3 and 4 hours
                    {
                        prepTimeInt    = prepTimeInt - 180;
                        prepTimeString = "03:" + prepTimeInt.ToString() + ":00";
                    }

                    DateTime prepTimeDateTime = Convert.ToDateTime(prepTimeString);
                    TimeSpan schoolAlarm      = departure.Subtract(prepTimeDateTime);

                    string alarm = schoolAlarm.Hours.ToString("00") + ":" + schoolAlarm.Minutes.ToString("00") + ":" + schoolAlarm.Seconds.ToString("00");

                    //alarm = "11:11:00"; // For testing the alarm

                    MessageBox.Show("School alarm set on: " + alarm);

                    // Arduino code
                    try // Connect
                    {
                        serialMessenger.Connect();
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message);
                    }

                    // Send message
                    serialMessenger.SendMessage(alarm);

                    // Disconnect
                    try
                    {
                        serialMessenger.Disconnect();
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message);
                    }
                }
            }
        }