文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

如何使用C#实现串口示波器

2023-06-29 05:42

关注

这篇文章主要为大家展示了“如何使用C#实现串口示波器”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用C#实现串口示波器”这篇文章吧。

具体内容如下

开发工具

C#串口示波器,实时刷新端口号,动态绘制多条折线,获取串口数据并输出到文本框
之前用Java实现串口示波器对我来说还是比较困难的,而且实现的效果不尽如人意,就用C#开发了。
C#可以自由布置界面,在工具栏直接拖拽控件到窗体,然后双击控件就能添加事件了,很方便
最终效果是实现了,但是有个数据丢失的问题

数据处理到绘图分三个步骤:

获取串口发送的数据
2、把数据按需要提取到中间容器集合list1中
3、从集合中提取绘图的y值

测试得出的结果就是数据放进集合前不存在丢失,从集合中拿出数据这一过程也没问题。所以问题就出在第二步了,已解决(按需提取数据的规则要尽可能设置的详细一点就行)。

代码:

namespace CommPortsDesigner{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e) //窗体运行完成即为加载,加载完成会触发事件        {        }        //传入原始数据datas及需要匹配的字符串r,得到需要的数据列表list        private List<int> DealData(string datas, Regex r, string split)         {            if (string.IsNullOrEmpty(datas))            {                throw new ArgumentException($"“{nameof(datas)}”不能是 Null 或为空。", nameof(datas));            }                       List<int> list1=new List<int> { };            string s1="";            MatchCollection mc = r.Matches(datas);  //提取符合要求的字符串            for (int i = 0; i < mc.Count; i++)            {                textBox2.AppendText(mc[i].Value + "\r\n");    //测试数据是否正确输出                s1 = s1 + mc[i].Value;            }            string[] d = s1.Split( new string[]{split}, StringSplitOptions.RemoveEmptyEntries);            for (int i=0;i<d.Length;i++)            //将数字存入列表            {                list1.Add( int.Parse(d[i]));            }            //测试画图用的数据是否解析正确            if (split.Equals("LP4:"))            {                foreach (int m in list1)                {                    textBox3.AppendText(m + "\r\n");                }            }            return list1;        }         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)        {        }        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)        {                            serialPort1.BaudRate = int.Parse(comboBox2.SelectedItem.ToString());        }        private void searchPort()        {            string Buffer;            comboBox1.Items.Clear();            for (int i = 1; i < 20; i++)            {                try                {                    Buffer = "COM" + i.ToString();                    serialPort1.PortName = Buffer;                    serialPort1.Open();                    comboBox1.Items.Add(Buffer);                    comboBox1.SelectedItem = Buffer;                    serialPort1.Close();                }                catch                { }            }        }        private void getDatas()         {            System.Text.RegularExpressions.Regex r1 = new System.Text.RegularExpressions.Regex("LP1:(-?)\\d+");            System.Text.RegularExpressions.Regex r2 = new System.Text.RegularExpressions.Regex("LP2:(-?)\\d+");            System.Text.RegularExpressions.Regex r3 = new System.Text.RegularExpressions.Regex("LP3:(-?)\\d+");            System.Text.RegularExpressions.Regex r4 = new System.Text.RegularExpressions.Regex("LP4:(-?)\\d+");            System.Text.RegularExpressions.Regex r5 = new System.Text.RegularExpressions.Regex("LP5:(-?)\\d+");            System.Text.RegularExpressions.Regex r6 = new System.Text.RegularExpressions.Regex("CC1:(-?)\\d+");            System.Text.RegularExpressions.Regex r7 = new System.Text.RegularExpressions.Regex("CC2:(-?)\\d+");            System.Text.RegularExpressions.Regex r8 = new System.Text.RegularExpressions.Regex("CC3:(-?)\\d+");            System.Text.RegularExpressions.Regex r9 = new System.Text.RegularExpressions.Regex("CC4:(-?)\\d+");            System.Text.RegularExpressions.Regex r10 = new System.Text.RegularExpressions.Regex("CC5:(-?)\\d+");            byte[] inbuffer = null;            //Queue<int> q1 = new Queue<int>();            if (serialPort1.IsOpen && serialPort1.BytesToRead > 0)            {                chart1.ChartAreas[0].AxisX.LabelStyle.Format = "hh:mm:ss.ff";                chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Milliseconds;                chart1.ChartAreas[0].AxisX.MajorGrid.IntervalType = DateTimeIntervalType.Milliseconds;                try                {                    inbuffer = new byte[serialPort1.BytesToRead];                    serialPort1.Read(inbuffer, 0, serialPort1.BytesToRead);                    string strRaad = ASCIIEncoding.ASCII.GetString(inbuffer, 0, inbuffer.Length);                    textBox1.AppendText(strRaad);                    chart1.ChartAreas[0].AxisX.Minimum = DateTime.Now.AddSeconds(-5).ToOADate();                    chart1.ChartAreas[0].AxisX.Maximum = DateTime.Now.ToOADate();                    chart1.ChartAreas[0].AxisX.Interval = 500;                    if (btn_start.Text.Equals("停止"))                    {                        //LP1                        if (checkBox2.Checked)                        {                            foreach (int y in DealData(strRaad, r2, "LP2:"))                            {                                chart1.Series[1].Points.AddXY(DateTime.Now.ToOADate(), y);                                //count2 = count2 + 5;                            }                        }                        else                        {                            chart1.Series[1].Points.Clear();                        }                                                //LP2                        if (checkBox2.Checked)                        {                            foreach (int y in DealData(strRaad, r2, "LP2:"))                            {                                chart1.Series[1].Points.AddXY(DateTime.Now.ToOADate(), y);                                //count2 = count2 + 5;                            }                        }                        else                        {                            chart1.Series[1].Points.Clear();                        }                        //LP3                        if (checkBox3.Checked)                        {                            foreach (int y in DealData(strRaad, r3, "LP3:"))                            {                                chart1.Series[2].Points.AddXY(DateTime.Now.ToOADate(), y);                                //count3 = count3 + 5;                            }                        }                        else                        {                            chart1.Series[2].Points.Clear();                        }                        //LP4                        if (checkBox4.Checked)                        {                            foreach (int y in DealData(strRaad, r4, "LP4:"))                            {                                chart1.Series[3].Points.AddXY(DateTime.Now.ToOADate(), y);                               //count4 = count4 + 5;                            }                        }                        else                        {                            chart1.Series[3].Points.Clear();                        }                        //LP5                        if (checkBox5.Checked)                        {                            foreach (int y in DealData(strRaad, r5, "LP5:"))                            {                                chart1.Series[4].Points.AddXY(DateTime.Now.ToOADate(), y);                                //count5 = count5 + 5;                            }                        }                        else                        {                            chart1.Series[4].Points.Clear();                        }                        //CC1                        if (checkBox6.Checked)                        {                            foreach (int y in DealData(strRaad, r6, "CC1:"))                            {                                chart1.Series[5].Points.AddXY(DateTime.Now.ToOADate(), y);                                //count6 = count6 + 5;                            }                        }                        else                        {                            chart1.Series[5].Points.Clear();                        }                        //CC2                        if (checkBox7.Checked)                        {                            foreach (int y in DealData(strRaad, r7, "CC2:"))                            {                                chart1.Series[6].Points.AddXY(DateTime.Now.ToOADate(), y);                                //count7 = count7 + 5;                            }                        }                        else                        {                            chart1.Series[6].Points.Clear();                        }                        //CC3                        if (checkBox8.Checked)                        {                            foreach (int y in DealData(strRaad, r8, "CC3:"))                            {                                chart1.Series[7].Points.AddXY(DateTime.Now.ToOADate(), y);                                //count8 = count8 + 5;                            }                        }                        else                        {                            chart1.Series[8].Points.Clear();                        }                        //CC4                        if (checkBox9.Checked)                        {                            foreach (int y in DealData(strRaad, r9, "CC4:"))                            {                                chart1.Series[8].Points.AddXY(DateTime.Now.ToOADate(), y);                                //count9 = count9 + 5;}                        }                        else                        {                            chart1.Series[8].Points.Clear();                        }                        //CC5                        if (checkBox10.Checked)                        {                            foreach (int y in DealData(strRaad, r10, "CC5:"))                            {                                chart1.Series[9].Points.AddXY(DateTime.Now.ToOADate(), y);                                //count10 = count10 + 5;                            }                        }                        else                        {                            chart1.Series[9].Points.Clear();                        }                    }                }                catch                {                }            }        }               private void btn_open_Click(object sender, EventArgs e)        {            if (serialPort1.IsOpen||btn_open.Text.Equals("关闭"))            {                try                {                    serialPort1.Close();                    btn_open.Text = "打开";                }                catch                { }            }            else            {                try                {                    serialPort1.PortName = comboBox1.Text;                    serialPort1.Open();                    btn_open.Text = "关闭";                }                catch                {                    MessageBox.Show("串口打开失败!", "错误");                }            }                    }        private void timer1_Tick(object sender, EventArgs e)        {            if (!serialPort1.IsOpen)            {                searchPort();            }            else{}                        getDatas();        }        private void btn_clear_Click(object sender, EventArgs e)        {             textBox1.Clear();        }        private void btn_save_Click(object sender, EventArgs e)        {            if (this.textBox1.TextLength > 0)            {                string path = "D:\\log.txt";                using (StreamWriter sw = new StreamWriter(path, true))                {                    String time = DateTime.Now.ToLocalTime().ToString();                    sw.WriteLine("\n" + time);                    sw.Write(this.textBox1.Text);                }                MessageBox.Show("已保存到 D:\\log.txt!");            }        }        private void btn_start_Click(object sender, EventArgs e)        {            if (serialPort1.IsOpen)            {                if (btn_start.Text.Equals("开始"))                {                    try                    {                        btn_start.Text = "停止";                    }                    catch                    { }                }                else                {                    try                    {                        btn_start.Text = "开始";                    }                    catch                    {                    }                }            }            else             {                if (btn_start.Text.Equals("停止"))                {                    try                    {                        btn_start.Text = "开始";                    }                    catch                    { }                }                else                 {                    MessageBox.Show("串口未打开!");                }            }        }    }}

因为第一次开发,所以要实现的功能代码都放在一个类里了,大家将就着看吧

最终实现的效果

如何使用C#实现串口示波器

以上是“如何使用C#实现串口示波器”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯