C#之windows桌面软件第十课:电脑ADC值显示(上位机)(单通道显示)
本站寻求有缘人接手,详细了解请联系站长QQ1493399855
C#之windows桌面软件第十课:电脑ADC值显示(上位机) (单通道显示)
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;
using System.IO.Ports;//using System.IO.Ports;
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) {byte Data = (byte)serialPort1.ReadByte(); //把接受到的数据赋给变量DataprogressBar1.Value = Data; //把Data值给进度条}/*下面语句用来调整窗口的大小和是否可见groupBox2.Visible = false; //第二个框不可见this.Size = new Size(620, 117); //缩小窗口groupBox2.Visible = true; //第二个框框可见this.Size = new Size(620, 172); //放大窗口*///打开串口按钮private void button1_Click(object sender, EventArgs e){if (serialPort1.IsOpen) //判断{groupBox2.Visible = false; //第二个框不可见this.Size = new Size(620, 117); //缩小窗口serialPort1.Close(); //此处可不加try,catchbutton1.Text = "打开串口";}else{try{serialPort1.PortName = comboBox1.Text; //串口号serialPort1.Open(); //打开groupBox2.Visible = true; //第二个框框可见this.Size = new Size(620, 172); //放大窗口button1.Text = "关闭串口"; //按键标题}catch{MessageBox.Show("串口打开错误", "错误");}}}}
}
www.DoYoung.net(部分代码来至杜洋工作室)