private void Window_Closed(object sender, EventArgs e)
 {
     if (ArduinoPort.IsOpen)
     {
         ArduinoPort.Write("1000");
     }
 }
        private async void BtnSendData_Click(object sender, RoutedEventArgs e)
        {
            TlbExperiment.IsEnabled = false;
            SpItems.IsEnabled       = false;
            ArduinoPort.DiscardOutBuffer();
            ArduinoPort.DiscardInBuffer();
            TxtStatusBrush.Color = Colors.Black;
            ArduinoPort.Write("1001");
            TxtStatus.Text = "Sending data...";
            await Task.Delay(1500);

            byte[] IntToBytes = BitConverter.GetBytes(ExpProgram.CycleNumber); //sending number of cycles
            ArduinoPort.Write(IntToBytes, 0, 2);

            IntToBytes = BitConverter.GetBytes(Convert.ToInt16(ExpProgram.CycleItems.Count));  //sending number of cycleitems
            ArduinoPort.Write(IntToBytes, 0, 2);

            foreach (var item in ExpProgram.CycleItems) //sending all cycle parameters
            {
                IntToBytes = BitConverter.GetBytes(item.CycleDuration);
                ArduinoPort.Write(IntToBytes, 0, 2);
                IntToBytes = BitConverter.GetBytes(item.OnTime);
                ArduinoPort.Write(IntToBytes, 0, 2);
                IntToBytes = BitConverter.GetBytes(item.OffTime);
                ArduinoPort.Write(IntToBytes, 0, 2);
            }
            Boolean DataWereSent = false;
            Int16   GetCode;

            while (!DataWereSent)
            {
                if (ArduinoPort.BytesToRead > 0)
                {
                    Int16.TryParse(ArduinoPort.ReadLine(), out GetCode);
                    if (GetCode == 1604)
                    {
                        DataWereSent = true;
                    }
                    // DataWereSent = true;
                    // TxtStatus.Text = ArduinoPort.ReadLine();
                }
            }
            TlbExperiment.IsEnabled = true;
            SpItems.IsEnabled       = true;
            BtnStart.IsEnabled      = true;
            DataWereSentGlobal      = true;
            TxtStatus.Text          = "Data were succesfully sent, press start to continue...";
        }
        private async void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            if (ExpStarted == false)
            {
                if (ArduinoPort.IsOpen)
                {
                    ExpStarted = true;
                    ArduinoPort.Write("1234");
                    BtnStart.IsEnabled = false;
                    await Task.Delay(1500);

                    BtnStart.Content     = "Cancel";
                    BtnStart.IsEnabled   = true;
                    TxtStatusBrush.Color = Colors.Black;
                    int Sum = 0;
                    foreach (var item in ExpProgram.CycleItems)
                    {
                        Sum += item.CycleDuration;
                    }
                    TxtStatus.Text        = $"Program has started... Total program time is {Sum} s";
                    BtnConnect.IsEnabled  = false;
                    BtnOpen.IsEnabled     = false;
                    BtnSendData.IsEnabled = false;
                    MainMenu.IsEnabled    = false;
                    SpItems.IsEnabled     = false;
                    Boolean AnswerRecieved = false;
                    Int16   GetCode        = 0;
                    await Task.Run(() =>
                    {
                        while (!AnswerRecieved)
                        {
                            Task.Delay(100);
                            if (ArduinoPort.BytesToRead > 3)
                            {
                                Int16.TryParse(ArduinoPort.ReadLine(), out GetCode);
                                if (GetCode == 1604)
                                {
                                    AnswerRecieved = true;
                                }
                            }
                        }
                    });

                    TxtStatus.Text        = $"Program has been finished";
                    BtnStart.Content      = "Start";
                    ExpStarted            = false;
                    TxtStatusBrush.Color  = Colors.Black;
                    BtnConnect.IsEnabled  = true;
                    BtnOpen.IsEnabled     = true;
                    BtnSendData.IsEnabled = true;
                    MainMenu.IsEnabled    = true;
                    SpItems.IsEnabled     = true;
                }
            }
            else
            {
                ArduinoPort.Write("1000");
                BtnStart.IsEnabled = false;
                await Task.Delay(1500);

                BtnStart.IsEnabled    = true;
                BtnStart.Content      = "Start";
                ExpStarted            = false;
                TxtStatusBrush.Color  = Colors.Black;
                TxtStatus.Text        = "Experiment was terminated...";
                BtnConnect.IsEnabled  = true;
                BtnOpen.IsEnabled     = true;
                BtnSendData.IsEnabled = true;
                MainMenu.IsEnabled    = true;
                SpItems.IsEnabled     = true;
            }
        }