c#上位机多通道ADC采样波形显示

啥也别说了,先看效果图吧!!!如下图:


1.主程序.cs文件

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Threading;  
  8. using System.Text;  
  9. using System.Windows.Forms;  
  10. using System.IO.Ports;  
  11. using System.Windows.Forms.DataVisualization.Charting;  
  12.   
  13. namespace demo  
  14. {  
  15.     public partial class demo: Form  
  16.     {  
  17.         #region 变量定义  
  18.   
  19.         public Int32 recFrameNum = 0; //接收到的数据帧数  
  20.         public Int32 recByteNum = 0;  //接收到的数据字节数  
  21.         private List<byte> recBuffer = new List<byte>();  
  22.         private bool handerListening = false;  
  23.         private bool comClosing = false;  
  24.         private int pointIndex = 0;  
  25.         private bool axisChange = false;  
  26.         public bool Isput = false;  
  27.         public byte[] buf;  
  28.         #endregion  
  29.   
  30.         public demo()  
  31.         {  
  32.             InitializeComponent();  
  33.   
  34.         }  
  35.   
  36.         #region 串口初始化  
  37.         private void buttonSwitch_Click(object sender, EventArgs e)  
  38.         {  
  39.             try  
  40.             {  
  41.                 if (serialPort1.IsOpen)  
  42.                 {  
  43.                     comClosing = true;//串口关闭动作正在进行  
  44.                    while (handerListening) Application.DoEvents();//关闭串口前处理完消息队列中的所有事件  
  45.   
  46.                     serialPort1.Close();  
  47.                     if (!serialPort1.IsOpen)  
  48.                     {  
  49.                         buttonSwitch.Text = ”打开串口”;  
  50.                         labelStatus.ForeColor = Color.Black;  
  51.                     }  
  52.                 }  
  53.                 else  
  54.                 {  
  55.                     comClosing = false;  
  56.                     serialPort1.PortName = comboBoxPort.Text;  
  57.                     serialPort1.BaudRate = Convert.ToInt32(comboBoxBaudRate.Text);  
  58.                     serialPort1.DataBits = Convert.ToInt32(comboBoxData.Text);  
  59.                     switch (comboBoxCheck.Text)  
  60.                     {  
  61.                         case “None”: serialPort1.Parity = System.IO.Ports.Parity.None; break;  
  62.                         case “Odd”: serialPort1.Parity = System.IO.Ports.Parity.Odd; break;  
  63.                         case “Even”: serialPort1.Parity = System.IO.Ports.Parity.Even; break;  
  64.                         case “Mark”: serialPort1.Parity = System.IO.Ports.Parity.Mark; break;  
  65.                         case “Space”: serialPort1.Parity = System.IO.Ports.Parity.Space; break;  
  66.                         defaultbreak;  
  67.                     }  
  68.                     serialPort1.StopBits = (System.IO.Ports.StopBits)Convert.ToInt32(comboBoxStopBit.Text);  
  69.   
  70.                     try  
  71.                     {  
  72.                         serialPort1.Open();  
  73.                     }  
  74.                     catch(Exception ex)  
  75.                     {  
  76.                         //现实异常信息给客户。  
  77.                         MessageBox.Show(ex.Message);  
  78.                         //捕获到异常信息,创建一个新的comm对象,之前的不能用了。  
  79.                         //serialPort1 = new SerialPort();  
  80.                     }  
  81.   
  82.                     if (serialPort1.IsOpen)  
  83.                     {  
  84.                         buttonSwitch.Text = ”关闭串口”;  
  85.                         labelStatus.ForeColor = Color.Red;  
  86.                     }  
  87.                 }  
  88.             }  
  89.             catch (Exception err)  
  90.             {  
  91.                 MessageBox.Show(err.Message);  
  92.             }  
  93.         }  
  94.         #endregion  
  95.   
  96.   
  97.         #region 串口接收    
  98.   
  99.         private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)  
  100.         {  
  101.   
  102.                 double xValue;  
  103.                 double yValue = 0;  
  104.                 double zValue = 0;  
  105.                 byte[] xVarry = new byte[2];  
  106.                 byte[] yVarry = new byte[2];  
  107.                 byte[] zVarry = new byte[2];  
  108.   
  109.       
  110.               
  111.             if (comClosing == truereturn//如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环  
  112.             try  
  113.             {  
  114.                 handerListening = true;//设置标记,说明已经开始处理数据,一会儿要使用系统UI的。  
  115.   
  116.                 int n = serialPort1.BytesToRead;  
  117.                 byte[] buf = new byte[n];  
  118.   
  119.                 recFrameNum++;  
  120.                 recByteNum += n;  
  121.                 serialPort1.Read(buf, 0, n);  
  122.                 recBuffer.AddRange(buf);  
  123.   
  124.                 //对接收到的数据进行显示           
  125.                 this.BeginInvoke((EventHandler)(delegate  
  126.                 {  
  127.                     labelRecByteCount.Text = Convert.ToString(recByteNum);  
  128.                     labelRecFrameCount.Text = Convert.ToString(recFrameNum);  
  129.                     //if (checkBoxRecDisplay.Checked == true)  
  130.                     //{  
  131.                     //    foreach (var temp in buf) { richTextBox1.Text += temp.ToString(“X2”) + ” ”; }  
  132.                     //    richTextBox1.AppendText(“\r\n”);  
  133.                     //    this.richTextBox1.ScrollToCaret();  
  134.                     //}  
  135.                 }));  
  136.                   
  137.                 if(buf.Length >= 8)  
  138.                 {  
  139.                     if (buf[0] == ConfigPrama.FrameHeader && buf[7] == ConfigPrama.FrameTail)  
  140.                     {  
  141.                         //通道一  
  142.                         xVarry[1] = buf[1];  
  143.                         xVarry[0] = buf[2];  
  144.                         xValue = Convert.ToDouble(BitConverter.ToInt16( xVarry,0));  
  145.                         xValue = xValue * 3.6 / 1024;  
  146.                         //通道二  
  147.                         yVarry[1] = buf[3];  
  148.                         yVarry[0] = buf[4];  
  149.                         yValue = Convert.ToDouble(BitConverter.ToInt16(yVarry, 0));  
  150.                         yValue = yValue * 3.6 / 1024;  
  151.                         //通道三  
  152.                         zVarry[1] = buf[5];  
  153.                         zVarry[0] = buf[6];  
  154.                         zValue = Convert.ToDouble(BitConverter.ToInt16(zVarry, 0));  
  155.                         zValue = zValue * 3.6 / 1024;  
  156.                         this.BeginInvoke((EventHandler)(delegate { paint_ax(xValue, yValue, zValue); }));  
  157.                     }  
  158.                 }  
  159.                 else  
  160.                 {  
  161.   
  162.                       
  163.                 }  
  164.             }  
  165.             catch(Exception ex)  
  166.             {  
  167.                 MessageBox.Show(ex.Message);  
  168.             }  
  169.             finally  
  170.             {  
  171.                 handerListening = false;  
  172.             }  
  173.         }  
  174.         #endregion  
  175.   
  176.         #region 数据缓冲清理  
  177.         public void Updatetextbox()  
  178.         {  
  179.   
  180.         }  
  181.         #endregion  
  182.   
  183.         #region 清空接收区和接收记录  
  184.         private void buttonClear_Click(object sender, EventArgs e)  
  185.         {  
  186.   
  187.             recFrameNum = 0;  
  188.             recByteNum = 0;  
  189.             labelRecByteCount.Text = ”0”;  
  190.             labelRecFrameCount.Text = ”0”;  
  191.         }  
  192.         #endregion  
  193.   
  194.         private void buttonSave_Click(object sender, EventArgs e)  
  195.         {  
  196.   
  197.         }  
  198.   
  199.         #region 绘图接口  
  200.         void paint_ax(double x,double y,double z)  
  201.         {  
  202.             // Define some variables  
  203.             int numberOfPointsInChart = 100;  
  204.   
  205.             try  
  206.             {  
  207.                 chart1.ChartAreas[”ChartArea1”].AxisX.Minimum = 0;  
  208.                 chart1.ChartAreas[”ChartArea1”].AxisX.Maximum = 100;  
  209.                 chart1.ChartAreas[”ChartArea1”].AxisY.Minimum = -1;  
  210.                 chart1.ChartAreas[”ChartArea1”].AxisY.Maximum = 4;  
  211.                 chart1.ResetAutoValues();  
  212.   
  213.                 if (checkBox1.Checked == true)  
  214.                 {  
  215.                               
  216.                     chart1.Series[”通道一”].Points.AddXY(pointIndex, x);  
  217.                 }  
  218.                 if (checkBox2.Checked == true)  
  219.                 {  
  220.                     chart1.Series[”通道二”].Points.AddXY(pointIndex, y);  
  221.                 }  
  222.                 if (checkBox3.Checked == true)  
  223.                 {  
  224.                     chart1.Series[”通道三”].Points.AddXY(pointIndex, z);  
  225.                 }  
  226.                 pointIndex++;  
  227.   
  228.   
  229.                 if(chart1.Series[0].Points.Count > numberOfPointsInChart)  
  230.                 {  
  231.                     axisChange = true;  
  232.   
  233.                 }  
  234.                    // Adjust X axis scale  
  235.                 if(axisChange)  
  236.                 {  
  237.                     chart1.ChartAreas[”ChartArea1”].AxisX.Minimum = pointIndex + 1 - numberOfPointsInChart;  
  238.                     chart1.ChartAreas[”ChartArea1”].AxisX.Maximum = chart1.ChartAreas[“ChartArea1”].AxisX.Minimum + numberOfPointsInChart;  
  239.                 }  
  240.   
  241.   
  242.                 // Invalidate chart  
  243.                 chart1.Invalidate();  
  244.   
  245.             }  
  246.             catch (Exception ex)  
  247.             {  
  248.                 MessageBox.Show(ex.Message);  
  249.             }  
  250.   
  251.   
  252.         }  
  253.         #endregion  
  254.   
  255.         #region 清除绘图  
  256.         private void buttonGclear_Click(object sender, EventArgs e)  
  257.         {  
  258.             chart1.Series[”通道一”].Points.Clear();  
  259.             chart1.Series[”通道二”].Points.Clear();  
  260.             chart1.Series[”通道三”].Points.Clear();  
  261.             pointIndex = 0;  
  262.             axisChange = false;  
  263.         }  
  264.         #endregion  
  265.   
  266.    
  267.   
  268.   
  269.     }  
  270. }  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Windows.Forms.DataVisualization.Charting;

namespace demo
{
    public partial class demo: Form
    {
        #region 变量定义

        public Int32 recFrameNum = 0; //接收到的数据帧数
        public Int32 recByteNum = 0;  //接收到的数据字节数
        private List<byte> recBuffer = new List<byte>();
        private bool handerListening = false;
        private bool comClosing = false;
        private int pointIndex = 0;
        private bool axisChange = false;
        public bool Isput = false;
        public byte[] buf;
        #endregion

        public demo()
        {
            InitializeComponent();

        }

        #region 串口初始化
        private void buttonSwitch_Click(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen)
                {
                    comClosing = true;//串口关闭动作正在进行
                   while (handerListening) Application.DoEvents();//关闭串口前处理完消息队列中的所有事件

                    serialPort1.Close();
                    if (!serialPort1.IsOpen)
                    {
                        buttonSwitch.Text = "打开串口";
                        labelStatus.ForeColor = Color.Black;
                    }
                }
                else
                {
                    comClosing = false;
                    serialPort1.PortName = comboBoxPort.Text;
                    serialPort1.BaudRate = Convert.ToInt32(comboBoxBaudRate.Text);
                    serialPort1.DataBits = Convert.ToInt32(comboBoxData.Text);
                    switch (comboBoxCheck.Text)
                    {
                        case "None": serialPort1.Parity = System.IO.Ports.Parity.None; break;
                        case "Odd": serialPort1.Parity = System.IO.Ports.Parity.Odd; break;
                        case "Even": serialPort1.Parity = System.IO.Ports.Parity.Even; break;
                        case "Mark": serialPort1.Parity = System.IO.Ports.Parity.Mark; break;
                        case "Space": serialPort1.Parity = System.IO.Ports.Parity.Space; break;
                        default: break;
                    }
                    serialPort1.StopBits = (System.IO.Ports.StopBits)Convert.ToInt32(comboBoxStopBit.Text);

                    try
                    {
                        serialPort1.Open();
                    }
                    catch(Exception ex)
                    {
                        //现实异常信息给客户。
                        MessageBox.Show(ex.Message);
                        //捕获到异常信息,创建一个新的comm对象,之前的不能用了。
                        //serialPort1 = new SerialPort();
                    }

                    if (serialPort1.IsOpen)
                    {
                        buttonSwitch.Text = "关闭串口";
                        labelStatus.ForeColor = Color.Red;
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
        #endregion


        #region 串口接收  

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

                double xValue;
                double yValue = 0;
                double zValue = 0;
                byte[] xVarry = new byte[2];
                byte[] yVarry = new byte[2];
                byte[] zVarry = new byte[2];



            if (comClosing == true) return; //如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环
            try
            {
                handerListening = true;//设置标记,说明已经开始处理数据,一会儿要使用系统UI的。

                int n = serialPort1.BytesToRead;
                byte[] buf = new byte[n];

                recFrameNum++;
                recByteNum += n;
                serialPort1.Read(buf, 0, n);
                recBuffer.AddRange(buf);

                //对接收到的数据进行显示         
                this.BeginInvoke((EventHandler)(delegate
                {
                    labelRecByteCount.Text = Convert.ToString(recByteNum);
                    labelRecFrameCount.Text = Convert.ToString(recFrameNum);
                    //if (checkBoxRecDisplay.Checked == true)
                    //{
                    //    foreach (var temp in buf) { richTextBox1.Text += temp.ToString("X2") + " "; }
                    //    richTextBox1.AppendText("\r\n");
                    //    this.richTextBox1.ScrollToCaret();
                    //}
                }));

                if(buf.Length >= 8)
                {
                    if (buf[0] == ConfigPrama.FrameHeader && buf[7] == ConfigPrama.FrameTail)
                    {
                        //通道一
                        xVarry[1] = buf[1];
                        xVarry[0] = buf[2];
                        xValue = Convert.ToDouble(BitConverter.ToInt16( xVarry,0));
                        xValue = xValue * 3.6 / 1024;
                        //通道二
                        yVarry[1] = buf[3];
                        yVarry[0] = buf[4];
                        yValue = Convert.ToDouble(BitConverter.ToInt16(yVarry, 0));
                        yValue = yValue * 3.6 / 1024;
                        //通道三
                        zVarry[1] = buf[5];
                        zVarry[0] = buf[6];
                        zValue = Convert.ToDouble(BitConverter.ToInt16(zVarry, 0));
                        zValue = zValue * 3.6 / 1024;
                        this.BeginInvoke((EventHandler)(delegate { paint_ax(xValue, yValue, zValue); }));
                    }
                }
                else
                {


                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                handerListening = false;
            }
        }
        #endregion

        #region 数据缓冲清理
        public void Updatetextbox()
        {

        }
        #endregion

        #region 清空接收区和接收记录
        private void buttonClear_Click(object sender, EventArgs e)
        {

            recFrameNum = 0;
            recByteNum = 0;
            labelRecByteCount.Text = "0";
            labelRecFrameCount.Text = "0";
        }
        #endregion

        private void buttonSave_Click(object sender, EventArgs e)
        {

        }

        #region 绘图接口
        void paint_ax(double x,double y,double z)
        {
            // Define some variables
            int numberOfPointsInChart = 100;

            try
            {
                chart1.ChartAreas["ChartArea1"].AxisX.Minimum = 0;
                chart1.ChartAreas["ChartArea1"].AxisX.Maximum = 100;
                chart1.ChartAreas["ChartArea1"].AxisY.Minimum = -1;
                chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 4;
                chart1.ResetAutoValues();

                if (checkBox1.Checked == true)
                {

                    chart1.Series["通道一"].Points.AddXY(pointIndex, x);
                }
                if (checkBox2.Checked == true)
                {
                    chart1.Series["通道二"].Points.AddXY(pointIndex, y);
                }
                if (checkBox3.Checked == true)
                {
                    chart1.Series["通道三"].Points.AddXY(pointIndex, z);
                }
                pointIndex++;


                if(chart1.Series[0].Points.Count > numberOfPointsInChart)
                {
                    axisChange = true;

                }
                   // Adjust X axis scale
                if(axisChange)
                {
                    chart1.ChartAreas["ChartArea1"].AxisX.Minimum = pointIndex + 1 - numberOfPointsInChart;
                    chart1.ChartAreas["ChartArea1"].AxisX.Maximum = chart1.ChartAreas["ChartArea1"].AxisX.Minimum + numberOfPointsInChart;
                }


                // Invalidate chart
                chart1.Invalidate();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
        #endregion

        #region 清除绘图
        private void buttonGclear_Click(object sender, EventArgs e)
        {
            chart1.Series["通道一"].Points.Clear();
            chart1.Series["通道二"].Points.Clear();
            chart1.Series["通道三"].Points.Clear();
            pointIndex = 0;
            axisChange = false;
        }
        #endregion




    }
}


2.初始化的一个数据协议格式.cs文件

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace demo  
  7. {  
  8.     public class ConfigPrama  
  9.     {  
  10.         public const byte FrameHeader = 0x5A; //帧头  
  11.         public const byte FrameTail = 0x5B; //帧尾  
  12.         public const int FrameLength = 8; //帧长  
  13.   
  14.     }  
  15. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace demo
{
    public class ConfigPrama
    {
        public const byte FrameHeader = 0x5A; //帧头
        public const byte FrameTail = 0x5B; //帧尾
        public const int FrameLength = 8; //帧长

    }
}

        最近项目需要自学c#上位机的学习成果。与大家分享一下,可以互相学习交流,还有很多不足接下来继续改进。


  • 4
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值