ADC数据显示
实现过程十分简单
- 初始化
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(); //读字节
progressBar1.Value = Data; //进度条赋值
}
- 单机事件函数
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("串口打开错误", "错误");
}
}
}