C#上位机串口通信带曲线显示和历史曲线查看

1:应用程序界面

2:实现的功能

(1)常规的串口收发操作

(2)私有通信协议的格式转化

(3)数据的定时刷新显示和实时存储(txt)

(4)数据的历史曲线查看

3:实现的方式

串口设置的核心代码

 private void SearchAndAddSerialToComboBox(SerialPort MyPort, ComboBox MyBox)
        {                                                               //将可用端口号添加到ComboBox
            string[] MyString = new string[20];                         //最多容纳20个,太多会影响调试效率
            string Buffer;                                              //缓存
            MyBox.Items.Clear();                                        //清空ComboBox内容
            for (int i = 1; i < 20; i++)                                //循环
            {
                try                                                     //核心原理是依靠try和catch完成遍历
                {
                    Buffer = "COM" + i.ToString();
                    MyPort.PortName = Buffer;
                    MyPort.Open();                                      //如果失败,后面的代码不会执行
                    MyString[i - 1] = Buffer;
                    MyBox.Items.Add(Buffer);                            //打开成功,添加至下拉列表
                    MyPort.Close();                                     //关闭
                }
                catch
                {

                }
            }
            MyBox.Text = MyString[0];                                   //初始化
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)                                     //串口打开就关闭
            {
                try
                {
                    serialPort1.Close();
                }
                catch { }                                               //确保万无一失
                button1.Text = "打开串口";
                Button1Statue = false;                                  //按钮状态                
            }
            else
            {
                try
                {
                    serialPort1.PortName = comboBox1.Text;              //端口号
                    serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);//十进制数据转换
                    serialPort1.DataBits = Convert.ToInt32(comboBox3.Text, 10);//十进制数据转换
                    switch (comboBox4.Text)
                    {
                        case "0":
                            serialPort1.StopBits = StopBits.None;
                            break;
                        case "1":
                            serialPort1.StopBits = StopBits.One;
                            break;
                        case "1.5":
                            serialPort1.StopBits = StopBits.OnePointFive;
                            break;
                        case "2":
                            serialPort1.StopBits = StopBits.Two;
                            break;
                        default:
                            MessageBox.Show("Error:参数不正确", "Error");
                            break;
                    }
                    serialPort1.Open();                                 //打开端口
                    button1.Text = "关闭串口";
                    Button1Statue = true;                                //按钮状态
                }
                catch
                {
                    MessageBox.Show("串口打开失败", "错误");
                }
            }
        }

定时刷新显示的核心代码:

 private void refresh_curve()
        {
            while (true)
            {
                string test_time = DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss");
                string Path1 = "";

                if (starttime == "")
                {
                    Path1 = get_latest_file(Datapath);
                }
                else
                {
                    Path1 = @".\data\" + starttime + ".txt";
                }
                /*调试代码
                lstReceiveMsg.Invoke(new EventHandler(delegate
                {
                    lstReceiveMsg.AppendText(test_time + "-->" + "\r\n");
                    lstReceiveMsg.AppendText(Path1 + "-->" + "\r\n");                                        
                }));
                 */
                // 从文件中读取并显示每行
                string line = "";
                try
                {
                    using (StreamReader sr = new StreamReader(Path1))
                    {
                        SortedList sl = new SortedList();
                        while ((line = sr.ReadLine()) != null)
                        {
                            string[] array = line.Split('+');
                            sl.Add(array[0], array[1]);
                        }
                        // 获取键的集合
                        ICollection key = sl.Keys;
                        foreach (string k in key)
                        {
                            //  Console.WriteLine(k + ": " + sl[k]);
                            Time_axis.Add(k);
                            Curve.Add(sl[k].ToString().Split(',')[type_curve]);
                        }
                    }

                }
                catch
                {
                    using (StreamReader sr = new StreamReader(get_latest_file(Datapath)))
                    {
                        SortedList sl = new SortedList();
                        while ((line = sr.ReadLine()) != null)
                        {
                            string[] array = line.Split('+');
                            sl.Add(array[0], array[1]);
                        }
                        // 获取键的集合
                        ICollection key = sl.Keys;
                        foreach (string k in key)
                        {
                            //  Console.WriteLine(k + ": " + sl[k]);
                            Time_axis.Add(k);
                            Curve.Add(sl[k].ToString().Split(',')[type_curve]);
                        }
                    }
                    Console.WriteLine("没有当前时间段数据:" + Path1);
                }
                
                //建立委托机制,防止线程间异常
                chart1.Invoke(new EventHandler(delegate
                {
                    chart1.Series[0].Points.DataBindXY(Time_axis, Curve);
                    chart1.Series[0].Name = chart_series_name[type_curve];
                }));
             

                Thread.Sleep(3000);
                Time_axis.Clear();
                Curve.Clear();
            }
        }

六、有兴趣接电子设计相关小型项目的请加下群,每个项目一般在1000元以内,非诚勿扰

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页