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
}
}
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
}
}
相关文章推荐
- iOS学习之NSURLSessionConfiguration构造方法
- Android-SDK国内更新镜像及设置
- Java enum的用法详解
- 前端事件系统(四)
- Centos6.7 编译安装 MySQL教程
- saltstack 安装部署及ntp服务批量部署过程实例
- centos7安装配置nexus+maven
- 成功—失败法和0.618法结合求出局部极小值的C语言实现
- 让Android屏幕适配开发更简单-我就是传说中的Google百分比布局支持库
- MapReduce-输入数据整块处理
- oracle导数据
- 设计模式—观察者模式
- 杭电OJ 1004实现和一些注意的陷阱
- UIScrollView上面添加的控件偏移
- 敏捷开发之Scrum扫盲篇
- 动态数组ArrayList
- springsecurity中如何做到多个用户不能使用同一个账号同时登陆系统
- SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3:
- Android Studio使用过程中常见问题及解决方案
- Android String 资源 你所不知道的