/// <summary> /// 读取.txt数据文件 /// </summary> private void ReadTXT() { //List<String[]> ls = new List<string[]>(); string[] tmpStr = null; string fileName = ""; string strLine = ""; int cnt = 0; bool flag1 = false, flag2 = false, flag3 = false, flag4 = false; //打开波形显示窗口 f2 = new ZGraphTest(); f2.MdiParent = this; //四个通道设置为全选中 for (int t = 0; t < f2.checkedListBox1.Items.Count; t++) { f2.checkedListBox1.SetItemChecked(t, true); } //清空数据 DataClear(); f2.Show(); //读入数据 using (OpenFileDialog fileDlg = new OpenFileDialog()) { fileDlg.Filter = "TXT Files|*.txt|All Files|*.*"; if (fileDlg.ShowDialog() == DialogResult.OK) { if (fileDlg.FileName.Length == 0) { return; } fileName = fileDlg.FileName; StreamReader fileReader = new StreamReader(fileName, Encoding.Default); //读入第一行数据 strLine = fileReader.ReadLine(); if (strLine != null && strLine.Length > 0) { //正则匹配一个或多个空格 tmpStr = Regex.Split(strLine, "\\s+"); f2.TmpChNum = (tmpStr.Count() - 1) / 2; for (int i = 0; i < tmpStr.Count(); i++) { if (string.Compare(tmpStr[i], "CH1") == 0) { flag1 = true; } else if (string.Compare(tmpStr[i], "CH2") == 0) { flag2 = true; } else if (string.Compare(tmpStr[i], "CH3") == 0) { flag3 = true; } else if (string.Compare(tmpStr[i], "CH4") == 0) { flag4 = true; } } } else { return; } //过滤掉第二、三行数据 strLine = fileReader.ReadLine(); strLine = fileReader.ReadLine(); //读取Second、CH1、CH2、CH3、CH4 while (strLine != null) { strLine = fileReader.ReadLine(); if (strLine != null && strLine.Length > 0) { //ls.Add(strLine.Split(',')); cnt = 1; tmpStr = Regex.Split(strLine, "\\s+"); if (flag1) { f2.X1.Add(float.Parse(tmpStr[0]) * (float)Math.Pow(10, 6)); f2.Y1.Add(float.Parse(tmpStr[cnt])); cnt += 2; } if (flag2) { f2.X2.Add(float.Parse(tmpStr[0]) * (float)Math.Pow(10, 6)); f2.Y2.Add(float.Parse(tmpStr[cnt])); cnt += 2; } if (flag3) { f2.X3.Add(float.Parse(tmpStr[0]) * (float)Math.Pow(10, 6)); f2.Y3.Add(float.Parse(tmpStr[cnt])); cnt += 2; } if (flag4) { f2.X4.Add(float.Parse(tmpStr[0]) * (float)Math.Pow(10, 6)); f2.Y4.Add(float.Parse(tmpStr[cnt])); cnt += 2; } } } fileReader.Close(); //绘制波形图 f2.drawWave(); } } return; }
/// <summary> /// 打开wav外部数据文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void 打开wav外部数据文件WToolStripMenuItem_Click(object sender, EventArgs e) { //打开波形显示窗口 f2 = new ZGraphTest(); f2.MdiParent = this; //四个通道设置为全选中 for (int t = 0; t < f2.checkedListBox1.Items.Count; t++) { f2.checkedListBox1.SetItemChecked(t, true); } //清空数据 DataClear(); f2.Show(); //读入数据 using (OpenFileDialog fileDlg = new OpenFileDialog()) { fileDlg.Filter = "WAV Files|*.wav|All Files|*.*"; if (fileDlg.ShowDialog() == DialogResult.OK) { f2.FileNameWAV = fileDlg.FileName; //读取文件的绝对路径并在窗口中显示 StringBuilder sb = new StringBuilder(); sb.Append(fileDlg.FileName); f2.Text = sb.ToString(); //如果.WAV数据只有一个通道,根据通道名称把数据存储在对应的通道 if (f2.TmpChNum == 1) { string tmpStr = new string(f2.TmpName); //char[]->string string str1 = "CH1\0\0\0\0\0\0\0"; string str2 = "CH2\0\0\0\0\0\0\0"; string str3 = "CH3\0\0\0\0\0\0\0"; string str4 = "CH4\0\0\0\0\0\0\0"; if (string.Equals(tmpStr, str1)) { } else if (string.Equals(tmpStr, str2)) //CH1->CH2 { for (int t = 0; t < f2.X1.Count; t++) { f2.X2.Add(f2.X1[t]); f2.Y2.Add(f2.Y1[t]); } f2.X1.Clear(); //CH1数据清空 } else if (string.Equals(tmpStr, str3)) //CH1->CH3 { for (int t = 0; t < f2.X1.Count; t++) { f2.X3.Add(f2.X1[t]); f2.Y3.Add(f2.Y1[t]); } f2.X1.Clear(); } else if (string.Equals(tmpStr, str4)) //CH1->CH4 { for (int t = 0; t < f2.X1.Count; t++) { f2.X4.Add(f2.X1[t]); f2.Y4.Add(f2.Y1[t]); } f2.X1.Clear(); } } } } //绘制波形图 f2.drawWave(); }