public void initCycle(Standby sPop, bool oneFile)
        {
            try
            {
                File.Delete(folderBrowserDialog1.SelectedPath + "\\" + "Data" + ext); // Delete single results file (if previosuly done, because append is on)

                // BEGIN LCR SETUP
                serialPort1.NewLine = Environment.NewLine;                                                                                                                                                                                      // Sets the line feed code to CR+LF

                serialPort1.Open();                                                                                                                                                                                                             // Opens serial communication port
                serialPort1.WriteLine(":BEEPer:KEY OFF");                                                                                                                                                                                       // Turn Beep Off
                serialPort1.WriteLine(":BEEPer:JUDGment OFF");                                                                                                                                                                                  // Turn Beep Off
                serialPort1.WriteLine(":MODE LCR");                                                                                                                                                                                             // Mode: LCR
                serialPort1.WriteLine(":PARameter1 " + param1.getSymbol().ToUpper() + ";:PARameter2 " + param2.getSymbol().ToUpper() + ";:PARameter3 " + param3.getSymbol().ToUpper() + ";:PARameter4 " + param4.getSymbol().ToUpper() + "; "); // Displays measurement finished message
                Console.WriteLine(MR1.ToString() + "," + MR2.ToString() + "," + MR3.ToString());
                serialPort1.WriteLine(":MEAS:ITEM " + MR1.ToString() + "," + MR2.ToString() + "," + MR3.ToString());                                                                                                                            //Define measurement parameters
                serialPort1.WriteLine(":HEAD OFF");                                                                                                                                                                                             // Header: OFF
                serialPort1.WriteLine(":LEV V");                                                                                                                                                                                                // Signal level: Open-circuit voltage
                //serialPort1.WriteLine(":LEV:VOLT 0.5");         // Signal level: 500 mV signal level
                //serialPort1.WriteLine(":FREQ 1E3");          // Measurement frequency: 1kHz
                serialPort1.WriteLine("TRIG EXT");             // Trigger: External trigger
                                                               // END LCR SETUP

                // BEGIN ARDUINO SETUP

                serialPort2.BaudRate = 9600;
                serialPort2.PortName = "COM4";
                serialPort2.Open();
                //serialPort2.WriteLine("O");
                //string a = serialPort2.ReadExisting();
                //Console.WriteLine(a);
                //serialPort2.Close();

                // END ARDUINO SETUP


                // BEGIN TEMPERATURE SWEEP

                decimal   furnaceTemp = 0;
                string    rd;
                decimal[] vals          = new decimal[5];
                string[]  correctOutput = new string[4]; // CHANGE IF YOU WANT MORE THAN 4 PARAMETERS

                this.BeginInvoke((MethodInvoker) delegate { sPop.setMinMaxTemp((decimal)tempVals.ElementAt <double>(tempVals.Count - 1), (decimal)tempVals.ElementAt <double>(0)); });
                //StandbyPopup.setMaxTemp((decimal)tempVals.ElementAt<double>(tempVals.Count-1));

                // These are to clear arduino reading
                serialPort2.ReadLine();
                serialPort2.ReadLine();


                while (tempVals.Count > 0 && furnaceTemp <= tempEnd)
                {
                    //Console.WriteLine("1");
                    // ****update temp using arduino data****
                    decimal.TryParse(serialPort2.ReadLine(), out furnaceTemp);
                    //furnaceTemp = furnaceTemp;

                    this.BeginInvoke((MethodInvoker) delegate { sPop.setCurrentTemp(furnaceTemp, (decimal)tempVals[0]); });
                    // this.BeginInvoke((MethodInvoker)delegate { stopRun = sPop.getStopCommand(); });
                    //StandbyPopup.setCurrentTemp(furnaceTemp);

                    Console.WriteLine("temp: " + furnaceTemp);
                    // Console.WriteLine("2 -- " + ((decimal)Math.Abs((decimal)tempVals[0] - furnaceTemp)).ToString() + " < " + tempError.ToString());

                    // *** if you are within some error value of desired temperature start a sweep***
                    if ((decimal)Math.Abs((decimal)tempVals[0] - furnaceTemp) < tempError)
                    {
                        // Console.WriteLine("2.5");
                        StreamWriter fp;
                        if (!oneFile)                                                                                                                                                       // not one file, put every temp in its own folder
                        {
                            fp = new System.IO.StreamWriter(folderBrowserDialog1.SelectedPath + "\\" + tempVals[0].ToString() + ext, false, System.Text.Encoding.GetEncoding("shift_jis")); // File open
                        }
                        else // append to same file every time
                        {
                            fp = new System.IO.StreamWriter(folderBrowserDialog1.SelectedPath + "\\" + "Data" + ext, true, System.Text.Encoding.GetEncoding("shift_jis")); // File open
                            fp.Write("Temperature: " + tempVals[0] + Environment.NewLine);                                                                                 // Write the temp for reference on single sheet
                        }

                        fp.Write("Frequency(Hz)" + "," + param1.getSymbol() + "," + param2.getSymbol() + "," + param3.getSymbol() + "," + param4.getSymbol() + Environment.NewLine);                // Outputs the header to file

                        /*
                         *         TODO: freqAvg
                         *
                         */

                        if (tempDelay > 0)                                          // delay after particular temperature reached
                        {
                            System.Threading.Thread.Sleep((int)(tempDelay * 1000)); // s
                        }
                        // Console.WriteLine("3");
                        for (int s = 0; s < freqNum; s++) // repeat measurements
                        {
                            // Console.WriteLine("4");
                            if (bulkDelay > 0) // delay between mearuements
                            {
                                System.Threading.Thread.Sleep((int)(bulkDelay * 1000));
                            }

                            for (int r = 0; r < freqVals.Count; r++)
                            {
                                // Console.WriteLine("5");
                                if (freqDelay > 0)                                          // user specified sleep between each frequency measurement
                                {
                                    System.Threading.Thread.Sleep((int)(freqDelay * 1000)); // s
                                }
                                //Console.WriteLine(freqVals[r].ToString());
                                serialPort1.WriteLine(":FREQ " + freqVals[r].ToString()); // Set Measurement frequency:
                                string f = freqVals[r].ToString();
                                serialPort1.WriteLine("*TRG;:MEAS?");                     // Reads trigger and measurement results
                                rd = serialPort1.ReadLine();                              // Acquires measurement results

                                // FIX ORDER OF OUTPUT START
                                string[] paramOutput = rd.Split(',');
                                correctOutput[0] = paramOutput[paramOrder[0]];
                                correctOutput[1] = paramOutput[paramOrder[1]];
                                correctOutput[2] = paramOutput[paramOrder[2]];
                                correctOutput[3] = paramOutput[paramOrder[3]];
                                string finalOutput = f + "," + correctOutput[0] + "," + correctOutput[1] + "," + correctOutput[2] + "," + correctOutput[3];
                                // FIX ORDER OF OUTPUT END
                                // Console.WriteLine("6");
                                this.BeginInvoke((MethodInvoker) delegate { sPop.addPoint(furnaceTemp, f, (decimal)double.Parse(correctOutput[0]), (decimal)double.Parse(correctOutput[1]), (decimal)double.Parse(correctOutput[2]), (decimal)double.Parse(correctOutput[3])); });
                                fp.Write(finalOutput);                               // Outputs the measurement results to file
                                fp.Write(Environment.NewLine);                       // Outputs the line feed to file
                            }
                        }


                        fp.Close();



                        tempVals.RemoveAt(0); // if temp reached, remove it from list
                        //fp.Close();
                    }
                }



                serialPort1.Close();
                // serialPort2.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERRPR");
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 private void StandbyPopup_FormClosed(object sender, FormClosedEventArgs e)
 {
     button3.Enabled = true;
     StandbyPopup    = new Standby();
 }