//List<string> _write_line = new List<string>(); private async void btn_board_v123_Click(object sender, RoutedEventArgs e) { Button obj = (Button)sender; vm.BoardTable_V123 = int.Parse(obj.Tag.ToString()); #region MultiMeter Setting GPIB_CMD myGPIB = new GPIB_CMD(0, 22); //MultiMeter GPIB Setting string mystring = "MEAS:VOLT:DC? 50,0.0001"; //MultiMeter GPIB Read Voltage Command Byte[] mybytes = System.Text.Encoding.Default.GetBytes(mystring); #endregion vm.port_PD = new SerialPort(vm.Selected_Comport, 115200, Parity.None, 8, StopBits.One); List <List <string> > _List_Bear_Say = new List <List <string> >(); for (int dac = initial_dac; dac <= 60000; dac += 10000) { if (vm.BoardTable_V123 == 1) { vm.WriteDac("1", "D", dac.ToString() + ",0,0"); //V1 Write Dac } else if (vm.BoardTable_V123 == 2) { vm.WriteDac("1", "D", "0," + dac.ToString() + ",0"); //V2 Write Dac } else { vm.WriteDac("1", "D", "0,0," + dac.ToString()); //V3 Write Dac } await vm.AccessDelayAsync(vm.Int_Write_Delay); vm.port_PD.Close(); myGPIB.Write(mybytes); string volt = myGPIB.Read(); if (vm.List_BoardTable.Count > 0) { double volt_before = 0; if (vm.BoardTable_V123 == 3) { List <double> list_voltage = new List <double>(); List <int> list_dac = new List <int>(); double final_voltage = 0; for (int c = 0; c < 1; c++) { if (vm.isStop) { break; } if (!vm.Bool_Gauge[c]) { continue; } string board_id = vm.BoardTable_SelectedBoard; string path = @"\\192.168.2.3\tff\Data\BoardCalibration\UFA\" + board_id + "-boardtable.txt"; if (!File.Exists(path)) { vm.Show_Bear_Window("UFV Board table is not exist", false, "String"); return; } StreamReader str = new StreamReader(path); str.ReadLine(); int count = 0; while (!vm.isStop) //Read board v3 data { string[] board_read = str.ReadLine().Split(','); //(一行一行讀取) if (board_read.Length < 1) { break; } string v3_table_voltage = board_read[0]; int v3_table_dac = int.Parse(board_read[1]); list_voltage.Add(Convert.ToDouble(v3_table_voltage)); list_dac.Add(v3_table_dac); if (v3_table_dac >= dac && count > 0) { int delta_x = (dac - list_dac[count - 1]); int delta_X = (list_dac[count] - list_dac[count - 1]); double delta_Y = (list_voltage[count] - list_voltage[count - 1]); final_voltage = (Convert.ToDouble(delta_x) / Convert.ToDouble(delta_X)) * delta_Y + list_voltage[count - 1]; final_voltage = Math.Round(final_voltage, 2); break; } count++; } str.Close(); //(關閉str) volt_before = final_voltage; } } else { volt_before = Convert.ToDouble(vm.List_BoardTable[vm.BoardTable_SelectedIndex][vm.BoardTable_V123]) * dac; //V1: BoardTable_V123 = 1 } double volt_after = Math.Round(Convert.ToDouble(volt), 4); double delta_volt = Math.Round(volt_after - volt_before, 4); vm.Str_Status = "Write DAC"; vm.Str_cmd_read = "DAC: " + dac.ToString() + " , Delta Volt: " + delta_volt.ToString(); #region Bear Say vm.txt_No = new string[] { "30000", "40000", "50000", "60000", "", "", "", "" }; _List_Bear_Say.Add(new List <string>() { volt_before.ToString(), volt_after.ToString() }); #endregion } } #region Show Bear Say vm._write_line = new List <string>(); for (int i = 0; i < 4; i++) //判斷是否NG { if (_List_Bear_Say[i].Count >= 2) { double NG_Criteria = i == 0 ? 0.015 : 0.015; //Setting delta V criteria double delta_volt = Math.Round(double.Parse(_List_Bear_Say[i][1]) - double.Parse(_List_Bear_Say[i][0]), 4); if (Math.Abs(delta_volt) > NG_Criteria) { _List_Bear_Say.Add(new List <string>() { delta_volt.ToString(), "NG" }); } else { _List_Bear_Say.Add(new List <string>() { delta_volt.ToString(), "PASS" }); } //Save Data to WriteLine vm._write_line.Add(vm.BoardTable_SelectedBoard + " , " + "V" + vm.BoardTable_V123 + " , " + (10000 * i + 30000).ToString() + " , " + _List_Bear_Say[i][0] + " , " + _List_Bear_Say[i][1] + " , " + delta_volt.ToString() + " , " + _List_Bear_Say[i + 4][1]); } } vm.List_bear_say = _List_Bear_Say; vm.Collection_bear_say.Add(_List_Bear_Say); vm.bear_say_all++; vm.bear_say_now = vm.bear_say_all; vm.Show_Bear_Window(vm.List_bear_say, false, "List"); #endregion }
private void btn_Save_Click(object sender, RoutedEventArgs e) { Button obj = (Button)sender; int beforeORafter = int.Parse(obj.Tag.ToString()); string product_type = ""; switch (vm.product_type) { case "UFA": product_type = "UFA"; break; case "UFA-T": product_type = "UFA"; break; case "UFA(H)": product_type = "UFA"; break; case "CTF": product_type = "CTF"; break; case "UTF": product_type = "UTF"; break; case "UTF400": product_type = "UTF"; break; case "UTF500": product_type = "UTF"; break; case "MTF": product_type = "MTF"; break; default: product_type = "UFA"; break; } bool _isChSelected = false; List <string> list_SN = new List <string>(); for (int i = 0; i < vm.ch_count; i++) { if (vm.Bool_Gauge[i]) { int errorCode = cmd.Save_K_WL_Data("K WL", vm.UserID, vm.list_SN[i], i, product_type, beforeORafter); if (errorCode != 0) { switch (errorCode) { case 1: vm.Show_Bear_Window("資料空白", false, "String", false); return; case 2: vm.Show_Bear_Window("工號空白", false, "String", false); return; case 3: vm.Show_Bear_Window("產品序號空白", false, "String", false); return; } } _isChSelected = true; } } if (_isChSelected) { vm.Show_Bear_Window("Saved", false, "String", false); } else { vm.Show_Bear_Window("Choose a channel to save data", false, "String", false); } }
public async Task <bool> CurFit_All(List <List <DataPoint> > Save_All_PD_Value, List <PointF> Points, List <double> BestCoeffs, string action) { if (Save_All_PD_Value.Count < 1) { vm.Show_Bear_Window("無細掃資訊", false, "String"); return(false); } else { if (action == "K TF" && Save_All_PD_Value[0].Count != 7) { //vm.Show_Bear_Window("細掃資料筆數不等於7", false, "String"); //return false; } vm.List_curfit_resultDac.Clear(); vm.List_curfit_resultWL.Clear(); vm.Str_Status = "Curve Fitting"; string Best_DAC = ""; vm.Str_cmd_read = " "; int all_ch_count = vm.ch_count; if (vm.PD_or_PM == true && action != "K WL") //PM mode { all_ch_count = 1; } vm.List_curfit_resultDac = Analysis.ListDefault <int>(vm.ch_count); for (int i = 0; i < all_ch_count; i++) { if (!vm.Bool_Gauge[i]) { vm.List_curfit_resultWL.Add(999); continue; } int mid_i = (int)Math.Round((double)Save_All_PD_Value[i].Count / 2); double mid_X = Math.Round(Save_All_PD_Value[i][mid_i].X, 2); Points = new List <PointF>(); if (action == "K WL") { foreach (DataPoint dp in Save_All_PD_Value[i]) { Points.Add(new PointF((float)(dp.X - Save_All_PD_Value[i][mid_i].X), (float)dp.Y)); } } else if (action == "K V3") { foreach (DataPoint dp in Save_All_PD_Value[i]) { Points.Add(new PointF((float)(dp.X - Save_All_PD_Value[i][mid_i].X), (float)dp.Y)); } } else { foreach (DataPoint dp in Save_All_PD_Value[i]) { Points.Add(new PointF((float)dp.X, (float)dp.Y)); } } if (Points.Count == 0) { continue; } // Find a good fit. int degree = 2; BestCoeffs = CurveFunctions.FindPolynomialLeastSquaresFit(Points, degree); if (degree == 2 && action == "K TF") { Best_DAC = Math.Round((-1 * BestCoeffs[1] / (2 * BestCoeffs[2]))).ToString(); } else { Best_DAC = Math.Round((-1 * BestCoeffs[1] / (2 * BestCoeffs[2])), 2).ToString(); //Best WL } string txt = ""; foreach (double coeff in BestCoeffs) { txt += ", " + Math.Round(coeff, 10).ToString(); } string coe = txt.Substring(1); string str_curfit_result = Best_DAC; //If Curfit result is not a number , error occurs. if (action == "K TF") { vm.List_curfit_resultDac[i] = Convert.ToInt32(Best_DAC); vm.Str_cmd_read = vm.Str_cmd_read + "," + str_curfit_result; } else if (action == "K WL") { vm.List_curfit_resultWL.Add(Convert.ToDouble(Best_DAC) + mid_X); vm.Str_cmd_read = vm.Str_cmd_read + "," + str_curfit_result; } else if (action == "K WL") { vm.List_curfit_resultWL.Add(Convert.ToDouble(Best_DAC) + mid_X); vm.Str_cmd_read = vm.Str_cmd_read + "," + str_curfit_result; } else { vm.Str_cmd_read = vm.Str_cmd_read + ", Error"; } if (action == "K TF") // 繪圖- CurveFit曲線 { double max_7points_dac = Save_All_PD_Value[i].LastOrDefault().X; double min_7points_dac = Save_All_PD_Value[i][0].X; double dac_gap = (max_7points_dac - min_7points_dac) / 100; Save_All_PD_Value[i].Clear(); for (double x = min_7points_dac; x <= max_7points_dac; x = x + dac_gap) { Save_All_PD_Value[i].Add(new DataPoint(x, (BestCoeffs[2] * Math.Pow(x, 2) + BestCoeffs[1] * x + BestCoeffs[0]))); } //vm.Chart_DataPoints = new List<DataPoint>(Save_All_PD_Value[i]); //The first lineseries } else //K WL { double max_7points_WL = (Save_All_PD_Value[i].LastOrDefault().X) - mid_X; double min_7points_WL = (Save_All_PD_Value[i][0].X) - mid_X; double dac_gap = Math.Round(max_7points_WL - min_7points_WL, 2) / 100; Save_All_PD_Value[i].Clear(); for (double x = min_7points_WL; x <= max_7points_WL; x = x + dac_gap) { Save_All_PD_Value[i].Add(new DataPoint(x + mid_X, (BestCoeffs[2] * Math.Pow(x, 2) + BestCoeffs[1] * x + BestCoeffs[0]))); } //vm.Chart_DataPoints = new List<DataPoint>(Save_All_PD_Value[i]); //The first lineseries } } vm.Str_cmd_read = vm.Str_cmd_read.Substring(2); return(true); } }