using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Threading;using System.IO.Ports;using System.IO;using System.Text.RegularExpressions;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public delegate void TextCallback(System.Windows.Forms.TextBox TexBoxName, string text); public delegate void SetLabelCallback(System.Windows.Forms.Label LabelName, string text); public delegate string TexCallback(System.Windows.Forms.Label LabelNamet); public delegate void ChartPointAddXYCallback(double XValue, double YValue, int SeriesName); public delegate void SetAxisXCallback(double AxisXMinimum, double AxisXMaximum); public delegate void SetAxisYCallback(double AxisXMinimum, double AxisXMaximum); public delegate void SeriesEnabledCallback(int SeiresName,bool Enabled); public delegate void SwitchSerialPortCallback(bool Switch); public delegate void RemovePointsCallback(double AxisXMin, int SeriesName); public Form1() { InitializeComponent(); UesrInit(); } string filename; public void UesrInit() { filename = DateTime.Now.ToString("yy-MM-dd-HH-mm-ss"); DataSaveFlag = 0; CheckBox[] CheckBoxName = new CheckBox[10] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10 }; TextBox[] TextBoxName = new TextBox[13] { textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9, textBox10, textBox11, textBox12, textBox13, textBox14, textBox15 }; try { string data = System.IO.File.ReadAllText(@"data", System.Text.Encoding.Default); byte[] datebytes = System.Text.Encoding.Default.GetBytes(data); int[] dataposit = new int[30]; for (int i = 0, j = 0; i < datebytes.Length; i , j ) { if (datebytes[i] == 44) { dataposit[0] ; dataposit[dataposit[0]] = j; } else if (datebytes[i] > 127) i ; } if (dataposit[0] == 24) { string datatemp; for (int i = 0; i < 10;i ) { datatemp = data.Remove(dataposit[i 1]); if(i>0)datatemp = datatemp.Remove(0, dataposit[i] 1); if (datatemp == "True") CheckBoxName[i].Checked = true; else CheckBoxName[i].Checked = false; } for (int i = 10; i < 23;i ) { datatemp = data.Remove(dataposit[i 1]); datatemp= datatemp.Remove(0, dataposit[i] 1); TextBoxName[i-10].Text = datatemp; } datatemp = data.Remove(dataposit[24]); this.comboBox1.Text = datatemp.Remove(0, dataposit[23] 1); } else { System.IO.File.WriteAllText(@"data", "True,True,True,True,True,True,True,True,True,True,,,,,,,,,,,30,100,0,COM4,", System.Text.Encoding.Default); for (int i = 0; i < 10;i ) { CheckBoxName[i].Checked = true; } for (int i = 0; i < 10;i ) { TextBoxName[i].Text = ""; } this.textBox13.Text = "30"; this.textBox14.Text = "100"; this.textBox15.Text = "0"; this.comboBox1.Text = "COM4"; } } catch { System.IO.File.WriteAllText(@"data", "True,True,True,True,True,True,True,True,True,True,,,,,,,,,,,30,100,0,COM4,", System.Text.Encoding.Default); for (int i = 0; i < 10; i ) { CheckBoxName[i].Checked = true; } for (int i = 0; i < 10; i ) { TextBoxName[i].Text = ""; TextBoxName[i].Enabled = true; } this.textBox13.Text = "30"; this.textBox14.Text = "100"; this.textBox15.Text = "0"; this.comboBox1.Text = "COM4"; } for (int i = 0; i < 10;i ) { this.chart1.Series[i].Enabled = CheckBoxName[i].Checked; } for (int i = 0; i < 13; i ) TextBoxName[i].Enabled = true; ChartConfig(); ChartPointAddXY(0, 0, 10); } public void ConfigDateSave() { try { System.IO.File.WriteAllText(@"data", this.checkBox1.Checked.ToString() "," this.checkBox2.Checked.ToString() "," this.checkBox3.Checked.ToString() "," this.checkBox4.Checked.ToString() "," this.checkBox5.Checked.ToString() "," this.checkBox6.Checked.ToString() "," this.checkBox7.Checked.ToString() "," this.checkBox8.Checked.ToString() "," this.checkBox9.Checked.ToString() "," this.checkBox10.Checked.ToString() "," this.textBox3.Text "," this.textBox4.Text "," this.textBox5.Text "," this.textBox6.Text "," this.textBox7.Text "," this.textBox8.Text "," this.textBox9.Text "," this.textBox10.Text "," this.textBox11.Text "," this.textBox12.Text "," this.textBox13.Text "," this.textBox14.Text "," this.textBox15.Text "," this.comboBox1.Text ",", System.Text.Encoding.Default); SetTextBox(textBox2, "配置数据保存成功"); } catch { SetTextBox(textBox2, "配置数据保存失败"); } } int DataSaveFlag = 0; public void RecvDataSave(string temp) { if(DataSaveFlag==0) { DataSaveFlag = 1; ConfigDateSave(); if (!Directory.Exists("savedata")) { Directory.CreateDirectory("savedata"); } try { System.IO.File.AppendAllText("savedata\\" filename ".txt", DateTime.Now.ToString("yy-MM-dd-HH-mm-ss") " " textBox3.Text ", " textBox4.Text ", " textBox5.Text ", " textBox6.Text ", " textBox7.Text ", " textBox8.Text ", " textBox9.Text ", " textBox10.Text ", " textBox11.Text ", " textBox12.Text "\r\n"); SetTextBox(textBox2, "表头保存成功"); } catch { SetTextBox(textBox2, "表头保存出错"); } } if (!Directory.Exists("savedata")) { Directory.CreateDirectory("savedata"); } try { System.IO.File.AppendAllText("savedata\\" filename ".txt", DateTime.Now.ToString("yy-MM-dd-HH-mm-ss") " " temp); SetTextBox(textBox2, "数据保存成功"); } catch { SetTextBox(textBox2, "数据保存出错"); } } public void ChartConfig() { try { if (this.textBox13.Text != "") { if (System.Convert.ToDouble(this.textBox13.Text) < 0.1) this.textBox13.Text = "0.1"; if (System.Convert.ToDouble(this.textBox13.Text) > 1440) this.textBox13.Text = "1440"; AxisXMax = System.Convert.ToDouble(this.textBox13.Text)/24/60; ChartInterval = ((int)(System.Math.Sqrt(AxisXMax * 100) 1 AxisXMax / 0.02));//串口绘图间隔 SetAxisX(ChartStartTime, ChartStartTime AxisXMax); SetTextBox(textBox2, "设置X轴坐标成功"); } else { this.textBox13.Text = "30"; AxisXMax = System.Convert.ToDouble(this.textBox13.Text) / 24 / 60; ChartInterval = ((int)(System.Math.Sqrt(AxisXMax * 100) 1 AxisXMax / 0.02));//串口绘图间隔 SetAxisX(ChartStartTime, ChartStartTime AxisXMax); } } catch { SetTextBox(textBox2, "设置X轴坐标失败"); } try { if (this.textBox14.Text != "" && this.textBox15.Text != "") { if (System.Convert.ToDouble(this.textBox14.Text) > 170) this.textBox14.Text = "170"; if (System.Convert.ToDouble(this.textBox15.Text) < -50) this.textBox15.Text = "-50"; if (System.Convert.ToDouble(this.textBox14.Text) <= System.Convert.ToDouble(this.textBox15.Text)) { this.textBox14.Text = "100"; this.textBox15.Text = "0"; } SetAxisY(System.Convert.ToDouble(this.textBox15.Text), System.Convert.ToDouble(this.textBox14.Text)); SetTextBox(textBox2, "设置Y轴坐标成功"); } else { this.textBox14.Text = "100"; this.textBox15.Text = "0"; SetAxisY(System.Convert.ToDouble(this.textBox15.Text), System.Convert.ToDouble(this.textBox14.Text)); } } catch { SetTextBox(textBox2, "设置Y轴坐标失败"); } try { this.chart1.Series[10].Points.Clear(); } catch { SetTextBox(textBox2, "数据清除失败"); } } int ThreadreturnFlag = 0; public void ChartSeries(object obj)//串口接收子线程 { double []temptures=(double[])obj; if ( ChartTotalDays<=AxisXMax) { for(int i=0;i<10;i ) { try { ChartPointAddXY(ChartStartTime ChartTotalDays, temptures[i], i); } catch { SetTextBox(textBox2, "调用绘图委托失败"); } } } else { SetAxisX(ChartStartTime ChartTotalDays - AxisXMax, ChartStartTime ChartTotalDays); for (int i = 0; i < 10; i ) { try { RemovePoints(ChartStartTime ChartTotalDays - AxisXMax, i); ChartPointAddXY(ChartStartTime ChartTotalDays, temptures[i], i); } catch { SetTextBox(textBox2, "调用绘图委托失败"); } } } ThreadreturnFlag = 0; } int DataRecvCount = 0; int ChartInterval = 1; int DataRecvFlag = 0; double AxisXMax = 30/24/60; private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口数据接收事件 { DataRecvFlag = 1; try { if(this.serialPort1.IsOpen) { int[] chanelposit = new int[11]; chanelposit[0] = 0; string temp=""; try { temp = serialPort1.ReadTo("\r\n") "\r\n"; } catch { } double[] temptures = new double[10]; if (this.serialPort1.BytesToRead != 0) this.serialPort1.ReadExisting(); byte[] byteArray = System.Text.Encoding.Default.GetBytes(temp); for (int i = 0; i < byteArray.Length; i ) { if (byteArray[i] == ',') { chanelposit[0] ; chanelposit[chanelposit[0]] = i; } } chanelposit[chanelposit[0] 1] = byteArray.Length - 2; if (chanelposit[0] == 9) { RecvDataSave(temp);//保存接收数据 timer1counter = 0;//喂狗 string temp1 = temp; Label[] Temshow = new Label[10] { Temshow1, Temshow2, Temshow3, Temshow4, Temshow5, Temshow6, Temshow7, Temshow8, Temshow9, Temshow10 }; for (int i = 0; i < 10; i ) { temp1 = temp.Remove(chanelposit[i 1]); if (i != 0) temp1 = temp1.Remove(0, chanelposit[i] 2); if (temp1 == "NULL") { SetLabelText(Temshow[i], temp1); temptures[i] = 0xffff; } else if (Regex.IsMatch(temp1, @"^[ -]?\d*[.]?\d*$"))//判断字符串是否可以转换为数字 { temptures[i] = System.Convert.ToDouble(temp1); SetLabelText(Temshow[i], temp1 "℃"); } else return; } if (ThreadreturnFlag == 0)//进程已经退出 { if (DataRecvCount % ChartInterval == 0) { try { Thread thread1 = new Thread(new ParameterizedThreadStart(ChartSeries)); thread1.Start(temptures); SetTextBox(textBox2, "线程创建成功"); } catch { SetTextBox(textBox2, "线程创建失败"); } } } else if (DataRecvCount % ChartInterval == 0) DataRecvCount--;//等待*/ DataRecvCount ;//计数器 } } } catch { SetTextBox(textBox2,"串口数据接收出错,请检查!"); } DataRecvFlag = 0; } /// <summary> /// 已重载,设置TextBox的text内容委托函数,第一个参数为TextBox名称,第二个参数为要写入TextBox的内容 /// </summary> public void SetTextBox(System.Windows.Forms.TextBox TexBoxName,string text) { if (TexBoxName.InvokeRequired) { TextCallback d = new TextCallback(SetTextBox); this.Invoke(d, new object[] { TexBoxName,text }); } else { TexBoxName.Text = text; } } /// <summary> /// 已重载,读取TextBox内容委托,参数为TextBox名称 /// </summary> public string SetTextBox(System.Windows.Forms.TextBox TexBoxName) { string TextBoxText=""; if (TexBoxName.InvokeRequired) { TextCallback d = new TextCallback(SetTextBox); this.Invoke(d, new object[] { TexBoxName }); } else { TextBoxText=TexBoxName.Text; } return TextBoxText; } /// <summary> /// 向指定series中添加点,第一个参数为X(时间),第二个参数为Y,第三个参数为series数组下表号 /// </summary> public void ChartPointAddXY(double XValue,double YValue,int SeriesName) { try { if (chart1.InvokeRequired) { ChartPointAddXYCallback d = new ChartPointAddXYCallback(ChartPointAddXY); this.Invoke(d, new object[] { XValue, YValue, SeriesName }); } else { try { if (YValue == 0xffff) this.chart1.Series[SeriesName].Enabled = false; else this.chart1.Series[SeriesName].Points.AddXY(XValue, YValue); } catch { SetTextBox(textBox2, "绘图出错"); } } } catch { SetTextBox(textBox2, "绘图出错0"); } } public void RemovePoints(double AxisXMin,int SeriesName) { try { if (chart1.InvokeRequired) { RemovePointsCallback d = new RemovePointsCallback(RemovePoints); this.Invoke(d, new object[] { AxisXMin, SeriesName }); } else { try { if (chart1.Series[SeriesName].Points.Count > 5) while (chart1.Series[SeriesName].Points[1].XValue < AxisXMin) chart1.Series[SeriesName].Points.RemoveAt(0); } catch { SetTextBox(textBox2, "数据点移除失败"); } } } catch { SetTextBox(textBox2, "数据点移除失败0"); } } public void SetAxisX(double AxisXMinimum, double AxisXMaximum) { if (this.chart1.InvokeRequired) { SetAxisXCallback d = new SetAxisXCallback(SetAxisX); this.Invoke(d, new object[] { AxisXMinimum, AxisXMaximum }); } else { this.chart1.ChartAreas[0].AxisX.Minimum = AxisXMinimum; this.chart1.ChartAreas[0].AxisX.Maximum = AxisXMaximum; } } public void SetAxisY(double AxisYMinimum, double AxisYMaximum) { if (this.chart1.InvokeRequired) { SetAxisYCallback d = new SetAxisYCallback(SetAxisX); this.Invoke(d, new object[] { AxisYMinimum, AxisYMaximum }); } else { this.chart1.ChartAreas[0].AxisY.Minimum = AxisYMinimum; this.chart1.ChartAreas[0].AxisY.Maximum = AxisYMaximum; } } public void SetLabelText(System.Windows.Forms.Label LabelName, string text) { if (this.textBox2.InvokeRequired) { SetLabelCallback d = new SetLabelCallback(SetLabelText); this.Invoke(d, new object[] { LabelName, text }); } else { LabelName.Text = text; } } public void SwitchSerialPort(bool Switch) { SwitchSerialPortCallback d = new SwitchSerialPortCallback(SwitchSerialPort1); this.Invoke(d, new object[] { Switch }); } //串口开关委托 public void SwitchSerialPort1(bool Switch) { if (Switch) { if (!this.serialPort1.IsOpen) this.serialPort1.Open(); } else { if (this.serialPort1.IsOpen) this.serialPort1.Close(); } } /// <summary> /// 软件运行总时间,一天时值为1 /// </summary> double TotalDays = 0; /// <summary> /// 绘图串口运行总时间,一天时值为1 /// </summary> double ChartTotalDays = 0; double StartTime = DateTime.Now.TimeOfDay.TotalDays; double ChartStartTime = DateTime.Now.TimeOfDay.TotalDays; DateTime StartTime1 = DateTime.Now; DateTime ChartStartTime1 = DateTime.Now; private void timer2_Tick(object sender, EventArgs e) { if (TotalDays == 0)StartTime1 = DateTime.Now; TotalDays = DateDiff(DateTime.Now, StartTime1) 1e-8; int TotalSecond = (int)(TotalDays * 3600 * 24); SetLabelText(label39,System.Convert.ToString(TotalSecond / 3600) "小时" System.Convert.ToString((TotalSecond % 3600) / 60) "分钟" System.Convert.ToString((TotalSecond % 3600) % 60) "秒"); if (TotalDays == 0) ChartStartTime1 = DateTime.Now; ChartTotalDays = DateDiff(DateTime.Now, ChartStartTime1) 1e-8; } /// <summary> /// 计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值. /// </summary> /// <param name="DateTime1">第一个日期和时间</param> /// <param name="DateTime2">第二个日期和时间</param> /// <returns></returns> private double DateDiff(DateTime DateTime1, DateTime DateTime2) { double dateDiff=0; try { TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); dateDiff = ts.TotalDays; } catch { } return dateDiff; } /// <summary> /// timer1看门狗清除位 /// </summary> int timer1counter = 0; private void timer1_Tick(object sender, EventArgs e) { if (this.timer1counter > 0) { if (this.button1.Text == "连接") { try { this.serialPort1.PortName = comboBox1.Text; this.serialPort1.BaudRate = 9600; SwitchSerialPort(true); this.button1.Text = "断开连接"; SetTextBox(this.textBox2, "串口重连成功"); this.timer1.Interval = 3000; } catch { SwitchSerialPort(false); this. button1.Text = "连接"; SetTextBox(this.textBox2, "串口重连失败"); this.timer1.Interval = 300; } } else if (this.button1.Text == "断开连接") { try { int i = 1000000; while ((ThreadreturnFlag == 1 || DataRecvFlag == 1)&&i>0) i--;//等待串口中断及其子线程退出 SwitchSerialPort(false); this.button1.Text = "连接"; SetTextBox(this.textBox2, "串口已断开"); this.timer1.Interval = 300; } catch { } } } else timer1counter = 1; } private void button3_Click(object sender, EventArgs e) { if (this.button1.Text == "断开连接") { try { int i = 1000000; while ((ThreadreturnFlag == 1 || DataRecvFlag == 1) && i > 0) i--;//等待串口中断及其子线程退出 SwitchSerialPort(false); this.button1.Text = "连接"; this.comboBox1.Enabled = true; this.textBox13.Enabled = true; this.textBox14.Enabled = true; this.textBox15.Enabled = true; SetTextBox(this.textBox2, "串口已断开"); try { this.timer1.Interval = 3000; this.timer1.Stop(); } catch { SetTextBox(this.textBox2, "停止定时器异常"); } } catch { SetTextBox(this.textBox2, "串口断开失败"); } } try { for (int i = 0; i < 10; i ) chart1.Series[i].Points.Clear(); UesrInit(); SetTextBox(textBox2, "操作成功!"); } catch { SetTextBox(textBox2, "操作失败!"); } } private void button2_Click(object sender, EventArgs e) { if (this.button1.Text == "断开连接") { try { while (ThreadreturnFlag == 1 || DataRecvFlag == 1) ;//等待串口中断及其子线程退出 SwitchSerialPort(false ); this.button1.Text = "连接"; this.comboBox1.Enabled = true; this.textBox13.Enabled = true; this.textBox14.Enabled = true; this.textBox15.Enabled = true; SetTextBox(this.textBox2, "串口已断开"); try { this.timer1.Interval = 3000; this.timer1.Stop(); } catch { SetTextBox(this.textBox2, "停止定时器异常"); } } catch { SetTextBox(this.textBox2,"串口断开失败"); } } DialogResult result; result = MessageBox.Show("确定要删除历史储存数据吗?此操作将会删除所有后台历史储存数据以及当前测量数据,并且不可恢复!", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (result == DialogResult.OK) { try { DirectoryInfo di = new DirectoryInfo(@"savedata\"); FileInfo[] arrFi = di.GetFiles("*.*"); di.Delete(true); for (int i = 0; i < 11; i ) chart1.Series[i].Points.Clear(); UesrInit(); SetTextBox(textBox2, "历史储存数据已删除!"); } catch { SetTextBox(textBox2, "操作失败!"); } } else { SetTextBox(textBox2,"操作已取消!"); } } public void button1_Click(object sender, EventArgs e) { if (this.button1.Text == "连接") { try { this.serialPort1.PortName = this.comboBox1.Text; this.serialPort1.BaudRate = 9600; SwitchSerialPort(true); this.button1.Text = "断开连接"; this.comboBox1.Enabled = false; TextBox[] TextBoxName = new TextBox[13] { textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9, textBox10, textBox11, textBox12, textBox13, textBox14, textBox15 }; for (int i = 0; i < 13; i ) TextBoxName[i].Enabled = false; SetTextBox(this.textBox2,"串口已连接"); this.timer1.Start(); if(DataSaveFlag==0) { ChartStartTime = DateTime.Now.TimeOfDay.TotalDays; ChartTotalDays = 0; } ChartConfig(); ConfigDateSave(); } catch { while (ThreadreturnFlag == 1 || DataRecvFlag == 1) ;//等待串口中断及其子线程退出 SwitchSerialPort(false ); this.button1.Text = "连接"; this.comboBox1.Enabled = true; this.textBox13.Enabled = true; this.textBox14.Enabled = true; this.textBox15.Enabled = true; SetTextBox(this.textBox2, "请检查串口连接"); this.timer1.Stop(); } } else if (this.button1.Text == "断开连接") { try { while (ThreadreturnFlag == 1 || DataRecvFlag == 1) ;//等待串口中断及其子线程退出 SwitchSerialPort(false ); this.button1.Text = "连接"; this.comboBox1.Enabled = true; this.textBox13.Enabled = true; this.textBox14.Enabled = true; this.textBox15.Enabled = true; SetTextBox(this.textBox2, "串口已断开"); try { this.timer1.Stop(); } catch { SetTextBox(this.textBox2, "停止定时器异常"); } } catch { SetTextBox(this.textBox2,"串口断开失败"); } } } private void checkBox1_CheckedChanged(object sender, EventArgs e) { SeriesEnabled(0, this.checkBox1.Checked); ConfigDateSave(); } private void checkBox2_CheckedChanged(object sender, EventArgs e) { SeriesEnabled(1, this.checkBox2.Checked); ConfigDateSave(); } private void checkBox3_CheckedChanged(object sender, EventArgs e) { SeriesEnabled(2, this.checkBox3.Checked); ConfigDateSave(); } private void checkBox4_CheckedChanged(object sender, EventArgs e) { SeriesEnabled(3, this.checkBox4.Checked); ConfigDateSave(); } private void checkBox5_CheckedChanged(object sender, EventArgs e) { SeriesEnabled(4, this.checkBox5.Checked); ConfigDateSave(); } private void checkBox6_CheckedChanged(object sender, EventArgs e) { SeriesEnabled(5, this.checkBox6.Checked); ConfigDateSave(); } private void checkBox7_CheckedChanged(object sender, EventArgs e) { SeriesEnabled(6, this.checkBox7.Checked); ConfigDateSave(); } private void checkBox8_CheckedChanged(object sender, EventArgs e) { SeriesEnabled(7, this.checkBox8.Checked); ConfigDateSave(); } private void checkBox9_CheckedChanged(object sender, EventArgs e) { SeriesEnabled(8, this.checkBox9.Checked); ConfigDateSave(); } private void checkBox10_CheckedChanged(object sender, EventArgs e) { SeriesEnabled(9, this.checkBox10.Checked); ConfigDateSave(); } public void SeriesEnabled(int SeriesName, bool Enabled) { if (this.chart1.InvokeRequired) { SeriesEnabledCallback d = new SeriesEnabledCallback(SeriesEnabled); this.Invoke(d, new object[] { SeriesName, Enabled }); } else { this.chart1.Series[SeriesName].Enabled = Enabled; } } }}
评论