예제 #1
0
        /*-----------------------START_TEST-------------------------------*/

        //Start Test Button Handling COMMUNICATION HAPPENS HERE
        private void button1_Click(object sender, EventArgs e)
        {
            //The button says pause test so a test has already been started
            if (run_test_button.Text == "Pause Test")
            {
                //change the button to a neutral color and say resume test
                run_test_button.BackColor = System.Drawing.Color.Blue;
                run_test_button.Text      = "Resume Test";
            }
            //The button says resume test because a test has already been started and paused and no values have been channged since then
            else if (run_test_button.Text == "Resume Test")
            {
                //send some form of restart signal to the edison
            }
            //The button says start test so it is starting a new test
            else
            {
                //If the frequency start and end are ready and the start is less than the end value
                if (frequency_start_ready == true && frequency_end_ready == true && frequency_end > frequency_start)
                {
                    frequency_ready = true;
                }
                //The frequency start and end are not ready or the start is not less than the end value
                else
                {
                    frequency_ready = false;
                }
                //All the values are ready and the file name is not empty
                if (amplitude_ready == true && frequency_ready == true && sweep_ready == true && offset_ready == true && file_name_val.Text != "")
                {
                    String fileName = file_name_val.Text;
                    //Default to .csv
                    fileName += ".csv";
                    //reset the label
                    file_name_label.ForeColor = System.Drawing.Color.Black;
                    file_name_label.Text      = "Output File Name:";
                    //get invalid file name characters
                    char[] invalid          = Path.GetInvalidFileNameChars();
                    bool   contains_invalid = false;
                    //check if the filename has invalid characters
                    for (int i = 0; i < invalid.Length; i++)
                    {
                        String c = invalid[i].ToString();
                        if (fileName.Contains(c))
                        {
                            contains_invalid = true;
                        }
                    }
                    //The file name does not have any invalid characters
                    if (contains_invalid == false)
                    {
                        //reset the label
                        file_name_label.ForeColor = System.Drawing.Color.Black;
                        file_name_label.Text      = "Output File Name:";
                        //set the path name
                        String pathString = System.IO.Path.Combine(workspace, fileName);
                        //if the file doesn't already exist
                        if (!File.Exists(pathString))
                        {
                            //reset the label
                            file_name_label.ForeColor = System.Drawing.Color.Black;
                            file_name_label.Text      = "Output File Name:";

                            bool handshake_success;
                            if (!debug)
                            {
                                handshake_success = com.HandShake("HI");
                            }
                            else
                            {
                                handshake_success = true;
                            }

                            if (handshake_success)
                            {
                                int  i = 0;
                                bool start = true, end = true, rate = true, sw = true, amp = true, off = true;
                                if (!debug)
                                {
                                    //communication stuff: transmit the user input to the Edison
                                    start = com.Transmit((i++) + "" + frequency_str_start);
                                    end   = com.Transmit((i++) + "" + frequency_str_end);
                                    rate  = com.Transmit((i++) + "" + sweep_rate_str);
                                    string type;
                                    if (sweep_type)
                                    {
                                        type = "0";
                                    }
                                    else
                                    {
                                        type = "1";
                                    }

                                    sw  = com.Transmit((i++) + "" + type);
                                    amp = com.Transmit((i++) + "" + amplitude_str);
                                    off = com.Transmit((i++) + "" + offset_str);
                                }


                                if (start && end && rate && sw && amp && off)
                                {
                                    //reset the button (will need to be moved to a different location later)
                                    run_test_button.BackColor = System.Drawing.Color.Red;
                                    run_test_button.Text      = "Pause Test";

                                    this.path = pathString;

                                    if (!debug)
                                    {
                                        //Edison communication
                                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@pathString))
                                        {
                                            //initialize the file with the user inputs
                                            file.WriteLine("Start Frequency: " + frequency_start);
                                            file.WriteLine("End Frequency: " + frequency_end);
                                            file.WriteLine("Amplitude: " + amplitude);
                                            if (sweep_type)
                                            {
                                                file.WriteLine("Linear Sweep Rate: " + this.sweep);
                                            }
                                            else
                                            {
                                                file.WriteLine("Logarithmic Sweep Rate: " + this.sweep);
                                            }
                                            file.WriteLine("DC Offset: " + offset);
                                            file.WriteLine("Frequency Gain Phase Change");
                                            test_output_label.Text = "Test in Progress";
                                            //read the output from the Edison
                                            com.ReadIn();
                                            String output = com.output;
                                            //While we have not reached the end of the output
                                            while (output[0] != 'E')
                                            {
                                                //write the output to the file
                                                file.WriteLine(output);

                                                /* Multithreading should be added here in the future. */
                                                //System.Threading.Thread newThread =new System.Threading.Thread(com.ReadIn);
                                                //newThread.Start();
                                                //while(output==com.output)
                                                //{

                                                //}

                                                //Read in the next output
                                                com.ReadIn();
                                                output = com.output;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //dummy data is being written to a file
                                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@pathString))
                                        {
                                            //write the user inputs to the file
                                            file.WriteLine("Start Frequency: " + frequency_start);
                                            file.WriteLine("End Frequency: " + frequency_end);
                                            file.WriteLine("Amplitude: " + amplitude);
                                            if (sweep_type)
                                            {
                                                file.WriteLine("Linear Sweep Rate: " + this.sweep);
                                            }
                                            else
                                            {
                                                file.WriteLine("Logarithmic Sweep Rate: " + this.sweep);
                                            }
                                            file.WriteLine("DC Offset: " + offset);
                                            file.WriteLine("Frequency Gain Phase Change");

                                            //Write random data to the file at each of the intervals for frequency
                                            Random test = new Random();
                                            float  f    = frequency_start;
                                            for (i = 1; f <= frequency_end; i++)
                                            {
                                                file.WriteLine(f + "," + Math.Round((test.NextDouble() * amplitude * i), 3) + "," + Math.Round((test.NextDouble() * Math.Abs(offset * i)), 3));
                                                f = (frequency_start + (i * sweep));
                                            }
                                        }
                                    }

                                    //set the test to complete (will need to be moved to a different location later)
                                    test_complete = true;

                                    run_test_button.BackColor = System.Drawing.Color.Green;
                                    run_test_button.Text      = "Start Test";

                                    //reset the output label (will need to be moved to a different location later)
                                    test_output_label.ForeColor = System.Drawing.Color.Green;
                                    test_output_label.Text      = "Test Complete";
                                }
                                //Output an error when the data transmission fails
                                else
                                {
                                    test_output_label.ForeColor = System.Drawing.Color.Red;
                                    test_output_label.Text      = "Data Transmission Failed";
                                }
                            }
                            //Output an error when the communication fails
                            else
                            {
                                test_output_label.ForeColor = System.Drawing.Color.Red;
                                test_output_label.Text      = "Communication Failure";
                            }
                        }
                        //The file name already exists, set the label to invalid
                        else
                        {
                            file_name_label.ForeColor = System.Drawing.Color.Red;
                            file_name_label.Text      = "File name already exists";
                        }
                    }
                    //The file name has invalid characters, set the label to invalid
                    else
                    {
                        file_name_label.ForeColor = System.Drawing.Color.Red;
                        file_name_label.Text      = "Invalid File Name";
                    }
                }
                //one of the values has not been set or is still not valid
                else
                {
                    test_output_label.ForeColor = System.Drawing.Color.Red;
                    test_output_label.Text      = "One or more values not set or valid";
                }
            }
        }