private void timSerial_Tick(object sender, EventArgs e) { if (serialPort1.IsOpen) { // COMが切断から復帰した時に再スタートする if (ComRecoveryFlg) { ComRecoveryFlg = false; for (int i = 0; i < lines.Count; i++) { lines[i].Points.Clear(); } timSerial.Start(); panel1.Enabled = true; btnConnect.Text = Resources.Disconnecting_Str; ViewUpdateTimer.Start(); recv_counter.Start(); Send("\r\n"); Send("stop\r\n"); Send("GET_STATUS\r\n"); Send("DUMP_PARAM\r\n"); Send("GET_PROD_ID\r\n"); Send("GET_BOARD_NAME\r\n"); Send("GET_FORMAT\r\n"); Send("GET_VERSION\r\n"); } } else { // COMが切断した時に接続を試みる ComSearchFlg = true; StatusLabel.Text = Resources.ComSearch_Str; StatusUpdateTimer.Stop(); StartBtn.Enabled = false; StopBtn.Enabled = false; Connect((String)comGimPort.SelectedItem, 115200); // 接続できたら復帰フラグを立てる if (IsCpmmected) { ComRecoveryFlg = true; ComSearchFlg = false; Debug.WriteLine("ReConeected"); } } }
/// <summary> /// 受信文字の解読 /// </summary> /// <param name="text"></param> private void text_parser(string text) { try { text = text.Replace("\r", ""); string[] split_str = text.Split(','); //描画タスク BeginInvoke((Action) delegate() { UInt32 r; double d; if (!double.TryParse(split_str[0], out d) && !UInt32.TryParse(split_str[0], NumberStyles.AllowHexSpecifier, new CultureInfo("en-US"), out r)) { if (split_str[0].Equals("READ_TEMP")) { TempLabel.Text = double.Parse(split_str[1]).ToString("F3") + "℃"; } else if (split_str[0].Equals("PAGE_DUMP")) { int j = 0; for (int i = 2; i < split_str.Length; i++) { this.dataGridView1.Rows.Add(new Object[] { split_str[1], "0x" + j.ToString("X2"), split_str[i] }); j += 2; } } else if (split_str[0].StartsWith("ERROR") || split_str[0].StartsWith("ERR")) { if (split_str[0].StartsWith("ERROR_PRODUCT_ID_INCORRECT")) { Disconnect(); DialogResult result = MessageBox.Show(Resources.Sens_Inco_Str, Resources.Error_Str, MessageBoxButtons.OK); } else if (split_str[0].Equals("ERR_NONE_CMD") || split_str[0].Equals("ERROR_NONE_CMD")) { if (split_str[1].Equals("DUMP_PARAM")) { Disconnect(); DialogResult result = MessageBox.Show(Resources.Old_firm_Str, Resources.Error_Str, MessageBoxButtons.OK); } } else if (split_str[0].Equals("ERROR_GET_FORMAT")) { Disconnect(); DialogResult result = MessageBox.Show(Resources.Format_Err_Str, Resources.Error_Str, MessageBoxButtons.OK); } } else if (split_str[0].Equals("DUMP_PARAM")) { VersionLabel.Text = split_str[1]; Kp_val.Value = (decimal)double.Parse(split_str[2]); Ki_val.Value = (decimal)double.Parse(split_str[3]); SendCycleNum.Value = (decimal)double.Parse(split_str[4]); StartupTimeNum.Value = (decimal)double.Parse(split_str[5]); } else if (split_str[0].Equals("GET_FORMAT")) { lines.Clear(); myPlotModel.Series.Clear(); csv_format = ""; for (int i = 0; i < split_str.Length - 1; i++) { LineSeries myLine = new LineSeries(); myLine.Title = split_str[i + 1]; myPlotModel.Series.Add(myLine); lines.Add(myLine); csv_format += split_str[i + 1] + ","; } Debug.WriteLine("軸数:" + myPlotModel.Series.Count); } else if (split_str[0].Equals("GET_PROD_ID")) { ProductIdLabel.Text = split_str[1]; } else if (split_str[0].Equals("GET_SENSI")) { Gyro_Sensi = double.Parse(split_str[1]); Acc_Sensi = double.Parse(split_str[2]); } else if (split_str[0].Equals("SET_SEND_CYCLE")) { SendCycleNum.Value = (decimal)double.Parse(split_str[1]); } else if (split_str[0].Equals("SET_STARTUP_TIME")) { StartupTimeNum.Value = (decimal)double.Parse(split_str[1]); } else if (split_str[0].Equals("GET_BOARD_NAME")) { BoardNameLabel.Text = split_str[1]; } else if (split_str[0].Equals("GET_VERSION")) { VersionLabel.Text = split_str[1]; } else if (split_str[0].Equals("GET_STATUS")) { if (split_str[1].Equals("AutoBiasUpdating")) { int time = int.Parse(split_str[2]); StatusLabel.Text = Resources.Bias_Updating_Str + time + Resources.Sec_Str; StatusUpdateTimer.Start(); StartBtn.Enabled = false; StopBtn.Enabled = false; } else if (split_str[1].Equals("Ready")) { StatusLabel.Text = Resources.Ready_Str; StatusUpdateTimer.Stop(); StartBtn.Enabled = true; StopBtn.Enabled = false; } else if (split_str[1].Equals("Running")) { StatusLabel.Text = Resources.Running_Str; StatusUpdateTimer.Stop(); StartBtn.Enabled = false; StopBtn.Enabled = true; } } else { } TxtLog.AppendText(text + "\r\n"); } else { recv_cnt++; int dat_len = SAMPLING_CNT;//サンプル数(X軸) List <double> dat_list = new List <double>(); if (UInt32.TryParse(split_str[0], NumberStyles.AllowHexSpecifier, new CultureInfo("en-US"), out r)) { for (int i = 0; i < 3; i++) { UInt32 result; UInt32.TryParse(split_str[i], NumberStyles.AllowHexSpecifier, new CultureInfo("en-US"), out result); dat_list.Add((double)((Int32)result / Gyro_Sensi * Math.PI / 180.0)); } for (int i = 3; i < 6; i++) { UInt32 result; UInt32.TryParse(split_str[i], NumberStyles.AllowHexSpecifier, new CultureInfo("en-US"), out result); dat_list.Add((double)((Int32)result * 9.80665 / Acc_Sensi)); } dat_list.Add(0); //CSUM } else { for (int i = 0; i < split_str.Length; i++) { dat_list.Add(double.Parse(split_str[i])); } } //エラー回避 if (lines.Count != dat_list.Count) { return; } //X軸の限界数以下であれば普通に保存する if (lines[0].Points.Count < dat_len) { for (int i = 0; i < dat_list.Count; i++) { lines[i].Points.Add(new OxyPlot.DataPoint(lines[i].Points.Count, dat_list[i])); } } else //X軸の限界数を上回ろうとしたら最後尾のデータを消して先頭に最新データを入れる。 { //先頭にデータが格納できるようにデータをズラす for (int i = 1; i < dat_len; i++) { for (int j = 0; j < dat_list.Count; j++) { lines[j].Points[i - 1] = new OxyPlot.DataPoint(lines[j].Points[i].X - 1, lines[j].Points[i].Y); } } //最後尾のデータを削除して、最新データを入力していく for (int i = 0; i < dat_list.Count; i++) { lines[i].Points.RemoveAt(dat_len - 1); lines[i].Points.Add(new OxyPlot.DataPoint(lines[i].Points.Count, dat_list[i])); } } plotView1.Invalidate(); // --(2) , ここでデータの変更が反映され、PlotViewが更新される myPlotModel.InvalidatePlot(true); // -- (3) , ここで軸設定が反映され、PlotViewが更新される if (LogFile != null) { LogFile.WriteLine(text); } } }); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }