WinForm serial port + 4 temperature acquisition from 0 (below)

      Well, after so long, the next article is finally ready. In the next article, we will use the last serial port program to make a few code modifications to make a 4-channel temperature acquisition. Of course, I will also give the source code at the end of the article. For your reference.

This program has undergone several iterations since last year. The original code was written on the MFC platform, and later on the WinFrom platform (one of the opportunities for me to learn C# to solve the problems encountered in MFC at that time)

Although the same idea is used, it can be felt that .Net has made obvious progress compared with MFC. The amount of code required for C# construction of the same code is much less than that of C++, which is more conducive to understanding (it does not rule out that I was too naive at the time)

The top is MFC, the bottom is WinFrom

 

Comparison of the two generations of window interfaces:

The top is MFC, the bottom is WinFrom

(The read cycle interval is a bug, it has been fixed)

The MFC code is so badly written that I'm embarrassed to put it out. I won't say much nonsense, let's get started

-------------------------------------------------- -Dividing line----------------------------------------------- ----

1. Change the window layout, delete the send box, receive box and send button, and add the following controls

Related controls:

1. "--" is the Label control, named as: lbl_Line_1, lbl_Line_2, lbl_Line_3, lbl_Line_4. Used to display the data of each channel

2. The cycle input box of "5 seconds" is a textbox control, named: txt_Cycle. Used to get the acquisition cycle set by the user

3. Bar chart control Chart, named: Ct_Temp

Add lines to the plot (each series corresponds to a plot line, if this step is not done, an "array exceeds the index range" error will occur):

Add a table control named: listView_Temp

And change the View in his properties to Details

Edit header: Click the triangle in the upper right corner to select "Edit Column"

There are two properties that need to be modified, namely Text (display content) and Width (display width)

Add a timer Timer , named: TimerCollect

The commonly used attributes of timer controls are: Interval (period), which can be assigned int data in milliseconds (the previous bug was that *1000 was forgotten here)

Commonly used methods are: start(); <start> and Stop(); <stop>

Add the class "TempData" and the class "DataServer" to the project (attached file)

Declare global variables and initialize the object DataStr as the data received by the serial port

string DataStr;//全局变量—串口接收值
        TempData Data = new TempData();
        DataServer ObjData = new DataServer();
        int X = 0;//图标的X轴
        int Index = 0;//数据的序列号

Assign the following code in the main program (the drawing code of the chart control)

#region UI相关服务
        private void InitChart()//初始化条形图
        {
            ChartArea chartArea = Ct_Temp.ChartAreas[0];
            chartArea.AxisX.Minimum = 0;
            chartArea.AxisX.Maximum = 10;
            chartArea.AxisY.Minimum = 0d;
            chartArea.AxisY.Maximum = 40d;
        }

        private void ShowTemp(TempData data)//显示图像
        {
            #region 添加图线数据
            Series series1 = Ct_Temp.Series[0];
            series1.ChartType = SeriesChartType.Spline;
            series1.BorderWidth = 2;
            series1.Color = System.Drawing.Color.Red;
            series1.LegendText = "线路1";
            series1.Points.AddXY(X, data.Line_1);

            Series series2 = Ct_Temp.Series[1];
            series2.ChartType = SeriesChartType.Spline;
            series2.BorderWidth = 2;
            series2.Color = System.Drawing.Color.Black;
            series2.LegendText = "线路2";
            series2.Points.AddXY(X, data.Line_2);

            Series series3 = Ct_Temp.Series[2];
            series3.ChartType = SeriesChartType.Spline;
            series3.BorderWidth = 2;
            series3.Color = System.Drawing.Color.Blue;
            series3.LegendText = "线路3";
            series3.Points.AddXY(X, data.Line_3);

            Series series4 = Ct_Temp.Series[3];
            series4.ChartType = SeriesChartType.Spline;
            series4.BorderWidth = 2;
            series4.Color = System.Drawing.Color.Yellow;
            series4.LegendText = "线路4";
            series4.Points.AddXY(X, data.Line_4);
            #endregion

            X++;
            ChartArea chartArea = Ct_Temp.ChartAreas[0];
            if (X > 10)
            {
                chartArea.AxisX.Minimum = X - 10;
                chartArea.AxisX.Maximum = X;
            }
            if (data.Line_1 > chartArea.AxisY.Maximum || data.Line_2 > chartArea.AxisY.Maximum || data.Line_3 > chartArea.AxisY.Maximum || data.Line_4 > chartArea.AxisY.Maximum)
            {
                chartArea.AxisY.Maximum += 20d;
            }
        }

        private void AddDate(TempData data, string Model)//数据表添加
        {
            ListViewItem List = listView_Temp.Items.Add(Index.ToString());
            List.SubItems.Add(Model);
            List.SubItems.Add(data.Line_1.ToString() + "℃");
            List.SubItems.Add(data.Line_2.ToString() + "℃");
            List.SubItems.Add(data.Line_3.ToString() + "℃");
            List.SubItems.Add(data.Line_4.ToString() + "℃");
            List.SubItems.Add(DateTime.Now.ToLocalTime().ToString());
            listView_Temp.Items[listView_Temp.Items.Count - 1].EnsureVisible();//滚动到最后  
            Index++;
        }

        private void LineShow(TempData data)//通道数据实时显示
        {
            lbl_Line_1.Text = data.Line_1.ToString();
            lbl_Line_2.Text = data.Line_2.ToString();
            lbl_Line_3.Text = data.Line_3.ToString();
            lbl_Line_4.Text = data.Line_4.ToString();
        }

        #endregion

Add the following code to the start acquisition button

if (btn_Engage.Text == "开始采集")
            {
                TimerCollect.Interval = Convert.ToInt16(txt_Cycle.Text)*1000;
                TimerCollect.Start();
                btn_Engage.Text = "停止采集";
            }
            else if (btn_Engage.Text == "停止采集")
            {
                TimerCollect.Stop();
                btn_Engage.Text = "开始采集";
            }

Go back to the designer and double-click the timer to add a response service to it:

Data = ObjData.DataAnaly(DataStr);
            LineShow(Data);
            ShowTemp(Data);
            AddDate(Data, "自动");
            ObjData.SaveDate(Index, Data, "自动");

Change the serial port receive event:

Press F5 to run

----------------------------------------------Dividing line-- -------------------------------------------------- ----

Source code: Link: https://pan.baidu.com/s/1wZuHVKUPqMBkr8XcKu2mpg Password: 43hq

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325072889&siteId=291194637