Esempio n. 1
0
        private void btn_MoistureMeasure_click(object sender, RoutedEventArgs e)
        {
            JumlahInterval   = ComboBox_NumberInterval.SelectedIndex + 1;
            counter_interval = JumlahInterval;

            TimeInterval  = ComboBox_TimeInterval.SelectedIndex;
            NumberGrain   = ComboBox_NumberGrain.SelectedIndex;
            NumberMeasure = ComboBox_NumberMeasure.SelectedIndex;

            //TimeInterval = ComboBox_TimeInterval.SelectedIndex;
            //int delay;
            switch (TimeInterval)
            {
            default:
                delay = 30000;
                break;

            case 0:
                delay = 30000;
                break;

            case 1:
                delay = 60000;
                break;

            case 2:
                delay = 90000;
                break;
            }


            switch (NumberGrain)
            {
            case 0:
                ResultGrain           = "10192\r";
                NumberGrain_Frekuensi = 10;
                break;

            case 1:
                ResultGrain           = "10293\r";
                NumberGrain_Frekuensi = 20;
                break;

            case 2:
                ResultGrain           = "10394\r";
                NumberGrain_Frekuensi = 30;
                break;

            case 3:
                ResultGrain           = "10495\r";
                NumberGrain_Frekuensi = 40;
                break;

            case 4:
                ResultGrain           = "10596\r";
                NumberGrain_Frekuensi = 50;
                break;

            default:
                ResultGrain           = "10697\r";
                NumberGrain_Frekuensi = 60;
                break;
            }

            switch (NumberMeasure)
            {
            case -1:
                ResultMeasure = "22094\r";
                break;

            case 0:
                ResultMeasure = "22094\r";
                break;

            case 1:
                ResultMeasure = "32095\r";
                break;

            case 2:
                ResultMeasure = "42096\r";
                break;

            case 3:
                ResultMeasure = "52097\r";
                break;

            case 4:
                ResultMeasure = "62098\r";
                break;

            case 5:
                ResultMeasure = "72094\r";
                break;

            case 6:
                ResultMeasure = "8209A\r";
                break;

            default:
                ResultMeasure = "22094\r";
                break;
            }
            //Sensor_input_Helper.Command_Write(mySerialPort, ResultGrain);
            //Sensor_input_Helper.Command_Write(mySerialPort, ResultMeasure);

            if (JumlahInterval < 0 || TimeInterval < 0 || NumberGrain < 0 || NumberMeasure < 0)
            {
                MessageBox.Show("Please fill All mandatory value", application_name);
            }

            else
            {
                ComboBox_NumberInterval.IsEnabled = false;
                ComboBox_TimeInterval.IsEnabled   = false;
                ComboBox_NumberGrain.IsEnabled    = false;
                ComboBox_NumberMeasure.IsEnabled  = false;

                //Sensor_input_Helper.Command_Check(mySerialPort);

                Sensor_input_Helper.Command_Write(mySerialPort, ResultGrain);
                //mySerialPort.DiscardOutBuffer();
                //mySerialPort.DiscardInBuffer();

                Sensor_input_Helper.Command_Write(mySerialPort, ResultMeasure);
                MessageBox.Show("Sensor Start Collecting Data", application_name);

                StatusListen = true;
            }
            //RunSensor();
        }
Esempio n. 2
0
        public void ProcessSensorData_local(object sender, SerialDataReceivedEventArgs args)
        {
            try
            {
                /*
                 * "\u0002129\u0003A1"
                 * \u0002128\u0003A0\r\u0002122\u00039A\r
                 * \u0002129\u0003A1\r\u0002116\u00039D\r
                 * \u0002125\u00039D\r\u0002123\u00039B"
                 * 1. Split character by Y
                 */
                Thread.Sleep(1000);// this solves the problem

                if (data_Average.Count() == counter_interval && current_interval > 0 && counter_interval > 0)
                {
                    mySerialPort.DiscardInBuffer();
                    mySerialPort.DiscardOutBuffer();
                    mySerialPort.Close();

                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        string Replace_Average = ",";
                        string temp_average;
                        float average = 0;
                        foreach (data_measure_2 data in data_Average)
                        {
                            temp_average = data.Measures;
                            temp_average = temp_average.Replace(Replace_Average, "");
                            average      = average + float.Parse(temp_average);
                        }
                        float final_average = average / 10 / data_Average.Count();

                        string final_average_str = final_average.ToString();

                        //final_average_str = String.Concat(final_average_str.Substring(0, final_average_str.Length - 1)
                        //            , ".", final_average_str.Substring(final_average_str.Length - 1, 1));

                        Average_Final.Text = final_average_str;
                        MessageBox.Show("All Measurement Finish", application_name);
                    }));
                }
                else
                {
                    byte[] readBuffer = new byte[mySerialPort.ReadBufferSize];
                    int    readLen    = mySerialPort.Read(readBuffer, 0, readBuffer.Length);
                    string readStr    = string.Empty;

                    readStr = Encoding.UTF8.GetString(readBuffer, 0, readLen);
                    //readStr = Encoding.UTF8.GetString(readBuffer,0,readLen);
                    readStr = readStr.Trim();
                    Console.WriteLine("ReadStr adalah: " + readStr);

                    char[]   delimiter_r     = { '\r' };
                    string[] Measures_With_U = readStr.Split(delimiter_r);

                    List <string> Measure_Results = new List <string>();

                    List <string> AllText = new List <string>();

                    foreach (var Measure in Measures_With_U)
                    {
                        //Test_Isengs = GetWords(Measure);

                        string Result_Parsing = GetWords(Measure).FirstOrDefault();
                        Console.WriteLine("Result Parsing adalah: " + Result_Parsing);
                        //Measure_Results.Add(Result_Parsing);

                        string[] charactersToReplace = new string[] { @"\t", @"\n", @"\r", " ", "<CR>", "<LF>" };

                        if (Result_Parsing != "" && Result_Parsing != null)
                        {
                            foreach (string s in charactersToReplace)
                            {
                                Result_Parsing = Result_Parsing.Replace(s, "");
                            }
                        }


                        if (Result_Parsing != "" && Result_Parsing != null && !Result_Parsing.Trim().ToLower().Contains("r"))
                        {
                            StatusListen = true;
                            Console.WriteLine("Nilai Measure adalah: " + Result_Parsing);



                            if (Result_Parsing.Contains("-") || (Result_Parsing.Length) > 4)
                            {
                                AllText = GetWords(Measure);
                                //Result_Parsing = AllText[1];

                                Result_Parsing = AllText[1].Substring(5, 3);
                                Result_Parsing = String.Concat(Result_Parsing.Substring(0, Result_Parsing.Length - 1)
                                                               , ".", Result_Parsing.Substring(Result_Parsing.Length - 1, 1));

                                //data_measure_2 data_final_update =

                                data_Average.Add(new data_measure_2(100, Result_Parsing, (DateTime.Now).ToString()));

                                //data_Average.Add(data_final_update);
                            }
                            else
                            {
                                Result_Parsing = String.Concat(Result_Parsing.Substring(0, Result_Parsing.Length - 1)
                                                               , ".", Result_Parsing.Substring(Result_Parsing.Length - 1, 1));

                                data_measure_2 data_final_update = new data_measure_2(counter_data + 1, Result_Parsing, (DateTime.Now).ToString());

                                data_finals_update.Add(data_final_update);
                                data_finals_update_2.Add(data_final_update);

                                //var result = from db.MyTable.Where(d => (double)d.Price >= minValue && (double)d.Price <= maxValue)

                                var data_Grid_1 = data_finals_update.Where(p => p.Id > 0 && p.Id <= 1 * NumberGrain_Frekuensi);
                                data_Grid_1_list_a = data_Grid_1.ToList();
                                data_Grid_1_list_b = data_Grid_1.ToList();

                                var data_Grid_2 = data_finals_update.Where(p => p.Id > 1 * NumberGrain_Frekuensi && p.Id <= 2 * NumberGrain_Frekuensi);
                                data_Grid_2_list_a = data_Grid_2.ToList();
                                data_Grid_2_list_b = data_Grid_2.ToList();

                                var data_Grid_3 = data_finals_update.Where(p => p.Id > 2 * NumberGrain_Frekuensi && p.Id <= 3 * NumberGrain_Frekuensi);
                                data_Grid_3_list_a = data_Grid_3.ToList();
                                data_Grid_3_list_b = data_Grid_3.ToList();

                                var data_Grid_4 = data_finals_update.Where(p => p.Id > 3 * NumberGrain_Frekuensi && p.Id <= 4 * NumberGrain_Frekuensi);
                                data_Grid_4_list_a = data_Grid_4.ToList();
                                data_Grid_4_list_b = data_Grid_4.ToList();

                                var data_Grid_5 = data_finals_update.Where(p => p.Id > 4 * NumberGrain_Frekuensi && p.Id <= 5 * NumberGrain_Frekuensi);
                                data_Grid_5_list_a = data_Grid_5.ToList();
                                data_Grid_5_list_b = data_Grid_5.ToList();

                                //data_pdfhistories = (data_pdfhistories_var.OrderBy(p => p.Id)).ToList();

                                //data_finals_ori.Add(data_final_ori);
                                //Data_Receive_Grid.ItemsSource = data_finals_ori;
                                counter_data = counter_data + 1;
                            }
                            Application.Current.Dispatcher.Invoke(new Action(() =>
                            {
                                //MessageBox.Show("Port is opened. Start Collecting Data", application_name);
                                //this.DataContext = this;
                                Data_Receive_Grid_1.ItemsSource = data_Grid_1_list_a;
                                Data_Receive_Grid_1.ItemsSource = data_Grid_1_list_b;

                                Data_Receive_Grid_2.ItemsSource = data_Grid_2_list_a;
                                Data_Receive_Grid_2.ItemsSource = data_Grid_2_list_b;

                                Data_Receive_Grid_3.ItemsSource = data_Grid_3_list_a;
                                Data_Receive_Grid_3.ItemsSource = data_Grid_3_list_b;

                                //var x = data_Average[0] != null ? 12 : (int?)null;
                                //if (val % 2 == 1) { output = “Number is odd”; } else { output = “Number is even”; }
                                if (data_Average.Count() == 1)
                                {
                                    Average_1.Text = data_Average[0].Measures;
                                }
                                else
                                {
                                    ;
                                }
                                if (data_Average.Count() == 2)
                                {
                                    Average_2.Text = data_Average[1].Measures;
                                }
                                else
                                {
                                    ;
                                }
                                if (data_Average.Count() == 3)
                                {
                                    Average_3.Text = data_Average[2].Measures;
                                }
                                else
                                {
                                    ;
                                }
                                if (data_Average.Count() == 4)
                                {
                                    Average_4.Text = data_Average[3].Measures;
                                }
                                else
                                {
                                    ;
                                }
                            }));
                        }


                        else if (data_finals_update.Count % NumberGrain_Frekuensi == 0 && data_finals_update.Count > 0 && StatusListen == true)
                        {
                            Sensor_input_Helper.Command_MoisturAggregate(mySerialPort);

                            if (data_Average.Count() == current_interval + 1)
                            {
                                mySerialPort.DiscardInBuffer();
                                mySerialPort.DiscardOutBuffer();

                                mySerialPort.Close();
                                StatusListen = false;

                                /*
                                 * Application.Current.Dispatcher.Invoke(new Action(() =>
                                 * {
                                 *  //MessageBox.Show("Port is opened. Start Collecting Data", application_name);
                                 *  //this.DataContext = this;
                                 *  //RunSensor();
                                 *
                                 *  MessageBox.Show("Please Wait Time Interval", application_name);
                                 *
                                 * }));
                                 */
                                Thread.Sleep(delay);
                                OpenCon_Port_local(mySerialPort, BaudRate);
                                //string start_command = String.Concat(ResultGrain," ", ResultMeasure);


                                //mySerialPort.DiscardInBuffer();
                                //mySerialPort.DiscardOutBuffer();


                                //Sensor_input_Helper.Command_Write(mySerialPort, ResultMeasure);
                                //StatusListen = true;
                                current_interval = current_interval + 1;
                                Sensor_input_Helper.Command_Write(mySerialPort, ResultGrain);
                                Sensor_input_Helper.Command_Write(mySerialPort, ResultMeasure);
                                //MessageBox.Show("Start Next Sequence", application_name);

                                /*Application.Current.Dispatcher.Invoke(new Action(() =>
                                 * {
                                 *  //MessageBox.Show("Port is opened. Start Collecting Data", application_name);
                                 *  //this.DataContext = this;
                                 *  //RunSensor();
                                 *  //Sensor_input_Helper.Command_Write(mySerialPort, ResultGrain);
                                 *  //Sensor_input_Helper.Command_Write(mySerialPort, ResultMeasure);
                                 *  MessageBox.Show("Start Next Sequence", application_name);
                                 *
                                 * }));*/
                            }

                            Console.WriteLine("Nilai Else If adalah: " + Result_Parsing);
                        }

                        else
                        {
                            Console.WriteLine("Nilai Else adalah: " + Result_Parsing);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                Console.WriteLine(ex);
                //return "";
            }
        }
Esempio n. 3
0
        public void RunSensor()
        {
            int NumberGrain   = ComboBox_NumberGrain.SelectedIndex;
            int NumberMeasure = ComboBox_NumberMeasure.SelectedIndex;


            int TimeInterval = ComboBox_TimeInterval.SelectedIndex;
            int delay;

            switch (TimeInterval)
            {
            default:
                delay = 30000;
                break;

            case 0:
                delay = 30000;
                break;

            case 1:
                delay = 60000;
                break;

            case 2:
                delay = 90000;
                break;
            }

            string ResultGrain;

            switch (NumberGrain)
            {
            case 0:
                ResultGrain           = "10192\r";
                NumberGrain_Frekuensi = 10;
                break;

            case 1:
                ResultGrain           = "10293\r";
                NumberGrain_Frekuensi = 20;
                break;

            case 2:
                ResultGrain           = "10394\r";
                NumberGrain_Frekuensi = 30;
                break;

            case 3:
                ResultGrain           = "10495\r";
                NumberGrain_Frekuensi = 40;
                break;

            case 4:
                ResultGrain           = "10596\r";
                NumberGrain_Frekuensi = 50;
                break;

            default:
                ResultGrain           = "10697\r";
                NumberGrain_Frekuensi = 60;
                break;
            }

            string ResultMeasure = "";

            switch (NumberMeasure)
            {
            case -1:
                ResultMeasure = "22094\r";
                break;

            case 0:
                ResultMeasure = "22094\r";
                break;

            case 1:
                ResultMeasure = "32095\r";
                break;

            case 2:
                ResultMeasure = "42096\r";
                break;

            case 3:
                ResultMeasure = "52097\r";
                break;

            case 4:
                ResultMeasure = "62098\r";
                break;

            case 5:
                ResultMeasure = "72094\r";
                break;

            case 6:
                ResultMeasure = "8209A\r";
                break;

            default:
                ResultMeasure = "22094\r";
                break;
            }

            if (counter_interval > 0)
            {
                counter_interval = counter_interval - 1;

                if (counter_interval == 0)
                {
                    MessageBox.Show("All Measurement finish", application_name);

                    /*
                     * if (temp_data_finals_2.Count > 0)
                     * {
                     *  //data_finals_ori.AddRange(temp_data_finals);
                     *
                     *  //data_finals_update.AddRange(temp_data_finals_2);
                     *  data_finals_update_2.AddRange(temp_data_finals_2);
                     *
                     *  temp_data_finals_2.Clear();
                     *
                     * }
                     */
                    //OpenCon_Port_local(mySerialPort, BaudRate);

                    //mySerialPort.Close();
                }
                else
                {
                    Thread.Sleep(delay);
                    OpenCon_Port_local(mySerialPort, BaudRate);
                    mySerialPort.DiscardInBuffer();
                    mySerialPort.DiscardOutBuffer();
                    Sensor_input_Helper.Command_Write(mySerialPort, ResultGrain);
                    mySerialPort.DiscardInBuffer();
                    mySerialPort.DiscardOutBuffer();

                    Sensor_input_Helper.Command_Write(mySerialPort, ResultMeasure);
                    //StatusListen = true;
                    mySerialPort.DiscardInBuffer();
                    mySerialPort.DiscardOutBuffer();

                    MessageBox.Show("Start Next Sequence", application_name);

                    /*
                     * Task.Delay(delay).ContinueWith(_ =>
                     * {
                     *  OpenCon_Port_local(mySerialPort, BaudRate);
                     *  Sensor_input_Helper.Command_Write(mySerialPort, ResultGrain);
                     *  Sensor_input_Helper.Command_Write(mySerialPort, ResultMeasure);
                     *  //StatusListen = true;
                     *
                     *  MessageBox.Show("Start Next Sequence", application_name);
                     * }
                     * );
                     */
                    //Thread.Sleep(delay);
                }
            }
        }