private void CalWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string command;



            for (; ;)
            {
                if (CalWorker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }


                ///////////


                for (int k = 0; k < 4; k++)
                {
                    R_J10x[k] = 0;
                }
                temperature = 0;


                // for (int p = 0; p < AVG_SAMPLES; p++)
                // {
                //  MessageBox.Show(therm.Measure().ToString());
                //Thread.Sleep(100);
                temperature = therm.Measure();
                // }
                // temperature = temperature / AVG_SAMPLES;
                // MessageBox.Show(temperature.ToString());



                for (int k = 0; k < 4; k++)
                {
                    command = "SPA" + Convert.ToString(k * 4, 2).PadLeft(4, '0');;
                    // MessageBox.Show(command);
                    tools.send_commnad(command, 1);
                    Thread.Sleep(SAMPLE_DELAY);
                    for (int p = 0; p < AVG_SAMPLES; p++)
                    {
                        //  MessageBox.Show(command + "\n" + tools.read_command("RUC01", 1));
                        R_J10x[k] += convert_data_to_R(Convert.ToDouble(tools.read_command("RUC01", 1)), 1.25, 10000);
                        //Thread.Sleep(SAMPLE_DELAY * 10);
                    }
                    R_J10x[k] = R_J10x[k] / AVG_SAMPLES;
                }

                for (int k = 0; k < 4; k++)
                {
                    R_J10x_q[k] = R_J10x[k];
                }
                temperature_q = temperature;

                CalWorker.ReportProgress(0);
                //Thread.Sleep(100);
                //////////
            }
        }
예제 #2
0
        private void Second_Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            string command;

            double[] temp_temps = new double[4];

            for (; ;)
            {
                // worker_counter++;
                if (Second_Worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }


                //_________________________________________Thermostat Reading___________________________________
                tools.send_commnad("SK0", 0);
                for (int k = 0; k < 4; k++)
                {
                    command = "SPA" + Convert.ToString(k, 2).PadLeft(4, '0');;
                    tools.send_commnad(command, 0);
                    Thread.Sleep(100);
                    DataR[k][worker_counter % SAMPLES] = convert_data_to_R(Convert.ToDouble(tools.read_command("RUC11", 0)), 1.25, 10000);
                    Data[k][worker_counter % SAMPLES]  = Cal.SettingsForm.convert_R_to_T(DataR[k][worker_counter % SAMPLES], (k + 1));
                    Status[k] = (Data[k].Average() <= (37 + THRESH_HOLD_Enviroment)) && (Data[k].Average() >= (37 - THRESH_HOLD_Enviroment));
                    //  MessageBox.Show(Data[k][worker_counter % SAMPLES].ToString());
                }
                temp_avg = ((Data[0].Average() + Data[1].Average() + Data[2].Average() + Data[3].Average()) / 4);
                tools.send_commnad("RK0", 0);
                //_______________________________________________________________________________________________

                //_______________________________________________________________________________________________


                //_________________________________________Heater Reading________________________________________
                int    kflipr;
                int    kk     = 0;
                int    avg_cc = 0;
                double sum_calculator;

                double[] Sampler = new double[AVG_SAMPLES];

                for (kk = 0; kk < 16; kk++)
                {
                    kflipr = four_bit_fliprl(kk);
                    for (int p = 0; p < AVG_SAMPLES; p++)
                    {
                        command = "SPA" + Convert.ToString(kflipr, 2).PadLeft(4, '0');;
                        //  MessageBox.Show(command);

                        tools.send_commnad(command, 0);
                        Thread.Sleep(40);
                        if (p == 0)
                        {
                            Sampler[p] = convert_data_to_T(Convert.ToDouble(tools.read_command("RUC02", 0)));
                        }
                        else
                        {
                            Sampler[p] = convert_data_to_T(Convert.ToDouble(tools.read_command("RUC02", 0)));
                        }
                    }

                    sum_calculator = 0;
                    avg_cc         = 0;

                    for (int p = 0; p < AVG_SAMPLES; p++)
                    {
                        if (Sampler[p] > Sampler.Average())
                        {
                            sum_calculator += Sampler[p];
                            avg_cc++;
                        }
                    }

                    Data[kk + 4][worker_counter % SAMPLES] = sum_calculator / avg_cc;
                    Status[kk + 4] = (Data[kk + 4][worker_counter % SAMPLES]) > 20;
                }
                //__________________________________________________________________________________________________



                for (int k = 0; k < 16; k++)
                {
                    //_______________________________________offset calculations________________________________________
                    Offset[k + 4][worker_counter % SAMPLES] = (!Status[k + 4] || Offset_Status[k + 4]) ? Offset[k + 4][worker_counter % SAMPLES] : Data[k + 4][worker_counter % SAMPLES] - temp_avg;
                    //__________________________________________________________________________________________________

                    //_______________________________________detects if offset is stablised__________________________
                    double[] temps = { Data[0].Average(), Data[1].Average(), Data[2].Average(), Data[3].Average() };
                    Offset_Status[k + 4] = ((Math.Abs(Offset[k + 4].Max() - Offset[k + 4].Min())) <= THRESH_HOLD_Stable) && ref_therm_ok;



                    //_______________________________________Log Heater Data__________________________


                    File.AppendAllText(@"data\" + datetime + "_Offset.txt", Offset[k + 4][worker_counter % SAMPLES].ToString() + "\t");
                    File.AppendAllText(@"data\" + datetime + "_Data.txt", Data[k + 4][worker_counter % SAMPLES].ToString() + "\t");
                }

                File.AppendAllText(@"data\" + datetime + "_Offset.txt", Environment.NewLine);
                File.AppendAllText(@"data\" + datetime + "_Data.txt", Environment.NewLine);

                ref_therm_ok = Status[0] &&
                               Status[1] &&
                               Status[2] &&
                               Status[3];

                Cal_Sucess = true;
                for (int row = 0; row < 16; row++)
                {
                    Cal_Sucess = Cal_Sucess && (!(Status[row + 4] ^ Offset_Status[row + 4]));
                    //MessageBox.Show(Cal_Sucess.ToString() + "\t" + Status[row + 4].ToString() + "\t" + Offset_Status[row + 4]);
                }

                Cal_Sucess = Cal_Sucess && ref_therm_ok;

                //Cal_Sucess = Cal_Sucess &&
                TimeOut = worker_counter >= TIMEOUT;

                if ((Cal_Sucess && worker_counter > MINMUM_COUNTS) || TimeOut)
                {
                    Sound.Play();
                    Second_Worker.CancelAsync();

                    while (!Second_Worker.CancellationPending)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                    if (Second_Worker.CancellationPending)
                    {
                        e.Cancel = true;
                    }

                    Connect2.Enabled       = true;
                    Print_PB.Enabled       = true;
                    Calibration_PB.Enabled = true;
                    LotNum.Enabled         = true;

                    for (int k = 0; k < 16; k++)
                    {
                        if (k == 0 && Status[k + 4])
                        {
                            HTR1_Radio.Enabled = true;
                        }
                        if (k == 1 && Status[k + 4])
                        {
                            HTR2_Radio.Enabled = true;
                        }
                        if (k == 2 && Status[k + 4])
                        {
                            HTR3_Radio.Enabled = true;
                        }
                        if (k == 3 && Status[k + 4])
                        {
                            HTR4_Radio.Enabled = true;
                        }
                        if (k == 4 && Status[k + 4])
                        {
                            HTR5_Radio.Enabled = true;
                        }
                        if (k == 5 && Status[k + 4])
                        {
                            HTR6_Radio.Enabled = true;
                        }
                        if (k == 6 && Status[k + 4])
                        {
                            HTR7_Radio.Enabled = true;
                        }
                        if (k == 7 && Status[k + 4])
                        {
                            HTR8_Radio.Enabled = true;
                        }
                        if (k == 8 && Status[k + 4])
                        {
                            HTR9_Radio.Enabled = true;
                        }
                        if (k == 9 && Status[k + 4])
                        {
                            HTR10_Radio.Enabled = true;
                        }
                        if (k == 10 && Status[k + 4])
                        {
                            HTR11_Radio.Enabled = true;
                        }
                        if (k == 11 && Status[k + 4])
                        {
                            HTR12_Radio.Enabled = true;
                        }
                        if (k == 12 && Status[k + 4])
                        {
                            HTR13_Radio.Enabled = true;
                        }
                        if (k == 13 && Status[k + 4])
                        {
                            HTR14_Radio.Enabled = true;
                        }
                        if (k == 14 && Status[k + 4])
                        {
                            HTR15_Radio.Enabled = true;
                        }
                        if (k == 15 && Status[k + 4])
                        {
                            HTR16_Radio.Enabled = true;
                        }
                    }
                    Kenny.Hide();
                    fireworks.Show();
                }
                worker_counter++;

                Second_Worker.ReportProgress(0);
            }
        }