您的位置:首页 > 其它

measurement studio测控与单片机通信上位机

2015-11-12 16:47 176 查看
using NationalInstruments.Analysis;

using NationalInstruments.Analysis.Conversion;

using NationalInstruments.Analysis.Dsp;

using NationalInstruments.Analysis.Dsp.Filters;

using NationalInstruments.Analysis.Math;

using NationalInstruments.Analysis.Monitoring;

using NationalInstruments.Analysis.SignalGeneration;

using NationalInstruments.Analysis.SpectralMeasurements;

using NationalInstruments;

using NationalInstruments.UI;

using NationalInstruments.NetworkVariable;

using NationalInstruments.NetworkVariable.WindowsForms;

using NationalInstruments.Tdms;

using NationalInstruments.UI.WindowsForms;

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;

namespace WindowsApplication2

{

public partial class Form1 : Form

{

//初始化公共数据

int j ;

List<double> list = new List<double> ();

char[] temperature = { '0','0','0','0','0','0','0'};

private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。

private bool Listening = false;//是否没有执行完invoke相关操作

private bool Closing = false;//是否正在关闭串口,执行Application.DoEvents,并阻止再次invoke

public Form1()

{

InitializeComponent();

}

private void switch1_StateChanged(object sender, ActionEventArgs e)

{

led1.Value = switch1.Value;

}

private void knob1_AfterChangeValue(object sender, AfterChangeNumericValueEventArgs e)

{

thermometer1.Value = knob1.Value;

}

private void button1_Click(object sender, EventArgs e)

{

}

//串口初始化

private void Form1_Load(object sender, EventArgs e)

{

serialPort1.PortName = "COM4";

serialPort1.BaudRate = 9600;//波特率设置

serialPort1.DataBits = 8;

serialPort1.StopBits = System.IO.Ports.StopBits.One;

serialPort1.Parity = System.IO.Ports.Parity.None;

serialPort1.Open();

timer1.Start();

}

#region 触发事件,刷新数据

//触发事件,刷新数据

private void timer1_Tick(object sender, EventArgs e)

{

double y = double.Parse(textBox1.Text);

textBox2.Text = Convert.ToString(y);

list.Add(y);

double[] yy = new double[j];

yy = list.ToArray();

waveformPlot1.PlotY(yy);

}

#endregion

#region 关闭串口,退出程序

//关闭串口,退出程序

private void button2_Click(object sender, EventArgs e)

{

if (serialPort1.IsOpen)

{

Closing = true;

while (Listening) Application.DoEvents();

//打开时点击,则关闭串口

serialPort1.Close();

Closing = false;

Close();

}

else

{

try

{

serialPort1.Open();

}

catch (Exception ex)

{

//捕获到异常信息,创建一个新的comm对象,之前的不能用了。

serialPort1 = new SerialPort();

//现实异常信息给客户。

MessageBox.Show(ex.Message);

}

}

//serialPort1.Close();

//Close();

}

#endregion

#region 窗体关闭按钮

//窗体关闭按钮

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

if (serialPort1.IsOpen)

{

Closing = true;

while (Listening) Application.DoEvents();

//打开时点击,则关闭串口

serialPort1.Close();

Closing = false;

Close();

}

else

{

try

{

serialPort1.Open();

}

catch (Exception ex)

{

//捕获到异常信息,创建一个新的comm对象,之前的不能用了。

serialPort1 = new SerialPort();

//现实异常信息给客户。

MessageBox.Show(ex.Message);

}

}

}

#endregion

#region 接受数据

//接受数据

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

{

if (Closing) return;//如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环

try

{

Listening = true;//设置标记,说明我已经开始处理数据,一会儿要使用系统UI的。

temperature[0] = Convert.ToChar(serialPort1.ReadChar());

if (temperature[0] == '-' || temperature[0] == '+')

{

for (int i = 1; i < 7; i++)

{

temperature[i] = Convert.ToChar(serialPort1.ReadChar());

}

this.Invoke(new EventHandler(DisplayText));

j++;

}

else

{

return;

}

}

finally

{

Listening = false;//我用完了,ui可以关闭串口了。

}

}

#endregion

#region 显示数据

//显示数据

private void DisplayText(object sender, EventArgs e)

{

for (int i = 0; i < 7; i++)

{

builder.Append(temperature[i]);

}

textBox1.Text = Convert.ToString(builder);

builder.Clear();

}

#endregion

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: