c#串口通信之adc数据采集

来源:互联网 发布:微信拼车源码 编辑:程序博客网 时间:2024/06/06 04:42

使用c#直接拖串口控件serialPort1 设置属性即可。

</pre><p></p><p></p><pre name="code" class="cpp">using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;<span style="color:#ff0000;">using System.IO.Ports;</span>namespace Adc_Display{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            serialPort1.DataReceived += new SerialDataReceivedEventHandler(PortDataReceivedEvent);                  //添加串口数据接受事件            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;                                   //        }        private void PortDataReceivedEvent(object sender, SerialDataReceivedEventArgs args)                         //串口数据接收        {<span style="color:#ff0000;">            byte Data = (byte)serialPort1.ReadByte();                                                               //读串口接收字节,            progressBar1.Value = Data;                                                                              //进度条赋值,显示进度条刻度</span>        }        private void button1_Click(object sender, EventArgs e)        {            if (serialPort1.IsOpen)                                                                                 //判断            {                groupBox2.Visible = false;                                                                          //第二个框框可见                this.Size = new Size(620, 117);                                                                     //缩小                serialPort1.Close();                                                                                //此处可不加try,catch                button1.Text = "打开串口";            }            else            {                try                {                    serialPort1.PortName = comboBox1.Text;                                                         //串口号                    serialPort1.Open();                                                                            //打开                    groupBox2.Visible = true;                                                                      //第二个框框可见                    this.Size = new Size(620, 172);                                                                //放大                    button1.Text = "关闭串口";                                                                     //按键标题                }                catch                {                    MessageBox.Show("串口打开错误", "错误");                }            }        }    }}


效果如图:



0 0
原创粉丝点击