/// <summary>
        /// <c>RunExperiment</c> is the main method of the <c>ConductivityExperiment</c>, it manages 
        /// all instruments to reach the required functionality.
        /// </summary>
        /// <param name="expParms">Experiment parameters passed by the user interface and set by the user.</param>
        /// <param name="pidParms">PID parameters that can be changed by the user.</param>
        /// <returns>If the experiment terminates normally, <c>true</c> is returned.</returns>
        public bool RunExperiment(Parameters expParms, PID.Controller.Parameters pidParms)
        {
            WindowsFormsApplication1.frmInterface mainform = new WindowsFormsApplication1.frmInterface();
               // WindowsFormsApplication1.ParserClass ParserClass_ = new WindowsFormsApplication1.ParserClass();

            this.runningExperiment = true;

            /* experiment always starts in the phase 1 */
            Contract.Requires(phase == Phases.PHASE1);

            //float T; /* temperature measured in the data acquisition */
            float lastT = 0; /* temperature before current one */
            //float rate; /* rate at which the temperature varies each time interval */
            float desiredRate = expParms.desiredRate;
            float envT = expParms.envTemperature;
            Limits Tlims = expParms.temperatureLims;
            float V_PowerSupply = 0; //expParms.amperimeterVoltage0;  /* voltage value to be sent to the power supply */
            int time = 0, dt = expParms.deltaTime; /* miliseconds */
            //float R = 0, i, V; /* resistance, current and voltage in the sample */
            float err = 0; /* error from desired rate */
            int counter = 0; /*counter*/
            Measurement measure;
            MessageBoxButtons buttons = MessageBoxButtons.OK;
            string caption = "Alerta";

            System.Console.Write("Start Experiment\n");

            pid = new PID.Controller(pidParms);
            file.Write(expParms);
            file.Write("\n"+Measurement.header);
            Init(expParms.amperimeterVoltage0);
            do {
                /*i = ampere.Read();
                V = ampere.readVoltage();
                T = data.Read();
                rate = (T - lastT) / (dt * 60); /* Celsius degree / sec */
                /*
                Contract.Invariant(i > 0);
                R = V / i;*/

                measure = this.Read(lastT, time);

                // list changes the status here, listeners are
                // interested in this event
                list.Add(measure);

                file.Write(measure);
               // System.Console.Write(measure);

                float Strtime = measure.getTime();
                float StrRate = measure.getRate();
                float StrT = measure.getT();
                float StrR = measure.getR();
                float[] measuresforChart = new float[] { measure.getTime(), measure.getRate(), measure.getT(), measure.getR() };
                float[] measuresforPIDChart = new float[] { measure.getTime(), (StrT - lastT) / dt };

                if (this.mainform != null)
                {

                    this.mainform.setStrValue(Strtime.ToString(), this.mainform.lblTime);
                    this.mainform.setStrValue(StrRate.ToString(), this.mainform.lblSampleCurrent);
                    this.mainform.setStrValue(StrT.ToString(), this.mainform.lblTemperature);
                    this.mainform.setStrValue(StrR.ToString(), this.mainform.lblResistorCurrent);

                    this.mainform.SetChartValues(measuresforChart, this.mainform.chart1, counter);
                    this.mainform.SetChartValues(measuresforPIDChart, this.mainform.chart2, counter);
                }

                /*
                 * In the freezing phase temperature is not controlled
                 * by PID because the rate of decreasing is very high
                 * and the power supply will change the voltage every
                 * time
                 */
                if (phase != Phases.PHASE2 || phase != Phases.PHASE4)
                {
                    err = desiredRate - measure.getRate();
                    lastT = measure.getT();

                    V_PowerSupply = V_PowerSupply * ( (desiredRate - pid.output(err, dt*60)) / desiredRate);
                    heater.Send(V_PowerSupply);
                }
                if ((measure.getT() > Tlims.max && (phase == Phases.PHASE1 || phase == Phases.PHASE3))
                       || (measure.getT() < Tlims.min && phase == Phases.PHASE2))
                    phase++;

                System.Threading.Thread.Sleep(dt);
                time += dt;
                counter++;

                if (phase != Phases.PHASE4 && measure.getT() >= Tlims.max)
                {
                    MessageBox.Show("Iniciar fase de resfriamento. Clique OK após o procedimento.", caption, buttons);
                }

            } while (this.runningExperiment);
            //while (phase != Phases.PHASE4 && measure.getT() < envT && this.runningExperiment);

            file.Close();
            Halt();
            this.runningExperiment = false;

            return true;
        }
        /// <summary>
        /// <c>RunExperiment</c> is the main method of the <c>ConductivityExperiment</c>, it manages 
        /// all instruments to reach the required functionality.
        /// </summary>
        /// <param name="expParms">Experiment parameters passed by the user interface and set by the user.</param>
        /// <param name="pidParms">PID parameters that can be changed by the user.</param>
        /// <returns>If the experiment terminates normally, <c>true</c> is returned.</returns>
        public bool RunExperiment(Parameters expParms, PID.Controller.Parameters pidParms)
        {
            this.runningExperiment = true;

            /* experiment always starts in the phase 1 */
            Contract.Requires(phase == Phases.PHASE1);

            //float T; /* temperature measured in the data acquisition */
            float lastT = 0; /* temperature before current one */
            //float rate; /* rate at which the temperature varies each time interval */
            float desiredRate = expParms.desiredRate;
            float envT = expParms.envTemperature;
            Limits Tlims = expParms.temperatureLims;
            float V_PowerSupply = 0; /* voltage value to be sent to the power supply */
            int time = 0, dt = 100; /* miliseconds */
            //float R = 0, i, V; /* resistance, current and voltage in the sample */
            float err = 0; /* error from desired rate */
            Measurement measure;

            System.Console.Write("Start Experiment\n");

            pid = new PID.Controller(pidParms);
            ExportToFile(fullpath, expParms.ToString()); //fileacq.Write(expParms);
            ExportToFile(fullpath, "\n" + Measurement.header); //fileacq.Write("\n" + Measurement.header);
            if (Init(expParms.amperimeterVoltage0))
            {
                do
                {
                    /*i = ampere.Read();
                    V = ampere.readVoltage();
                    T = data.Read();
                    rate = (T - lastT) / (dt * 60); /* Celsius degree / sec */
                    /*
                    Contract.Invariant(i > 0);
                    R = V / i;*/

                    measure = this.Read(lastT, time);

                    // list changes the status here, listeners are
                    // interested in this event
                    list.Add(measure);

                    fileacq.Write(measure);
                    System.Console.Write(measure);
                    /*
                     * In the freezing phase temperature is not controlled
                     * by PID because the rate of decreasing is very high
                     * and the power supply will change the voltage every
                     * time
                     */
                    if (phase != Phases.PHASE2 || phase != Phases.PHASE4)
                    {
                        err = desiredRate - measure.getRate();
                        lastT = measure.getT();

                        V_PowerSupply = V_PowerSupply * ((desiredRate - pid.output(err, dt * 60)) / desiredRate);
                        heater.Send(V_PowerSupply);
                    }
                    if ((measure.getT() > Tlims.max && (phase == Phases.PHASE1 || phase == Phases.PHASE3))
                           || (measure.getT() < Tlims.min && phase == Phases.PHASE2))
                        phase++;

                    System.Threading.Thread.Sleep(dt);
                    time += dt;
                } while (phase != Phases.PHASE4 && measure.getT() < envT && this.runningExperiment);

                fileacq.Close();
                Halt();
                this.runningExperiment = false;
                return true;
            }
            else return false;
        }