LabWindows_CVI测试技术及工程应用学习笔记1(构建一个简单的程序)
1、创建工程文件
或File——〉New——〉Project(*.prj),默认名Untitled.prj,存储在Unititled.cws的工作空间中
File——〉Save Untitled Project As...保存新建工程文件为“温度.prj”
2、创建用户界面文件
或File——〉New——〉User Interface(*.uir)
面板属性编辑
panel——面板
constant name——常量名
callback function——回掉函数名
panel title——面板标题
menu bar——菜单栏
close control——关闭面板的控件
top——顶
left——左
height——高
width——宽
scroll bars——滚动条
auto-center vertically——垂直居中
auto-cecnter horizontally——水平居中
title bar style——标题栏风格
other attributes——其他属性
sizable——程序运行时是否可以改变窗口大小
movable——程序运行时是否可以通过拖拽移动窗口
can maximize——程序运行时是否可以最大化
can minimize——程序运行时是否可以最小化
title bar visible——标题栏可见
has taskbar button——有任务栏按钮
conform to system colors——符合系统颜色
scale contents on resize——调整大小时缩放内容
floating style——浮动风格(最前端显示,never从不;when app is active程序激活时;always总是)
create——〉graph——〉strip chart
graph是整屏显示一组静态数据,strip chart滚动显示数据并能动态添加数据点
create——〉numeric——〉thermometer
数值 温度计
create——〉numeric——〉numeric(最大值 最小值)
create——〉BinarySwitch——〉vertical toggle switch
垂直摇杆开关
create——〉led——〉round led
create——〉command button——〉square command button
方形命令按钮
arrange——〉alignment
布置 对齐
按钮控件属性
control mode——控件显示类型(indication指示类型,不响应用户操作事件;hot响应控件所有事件normal在程序执行时修改控件属性,不能对控件操作产生事件;validate与hot基本相同,若range-checking范围检查设置为notify通知,属性值合法才产生EVENT_COMMIT)
initially dimmed——程序运行时控件是否可用
initially hidden——程序运行时控件是否可见
modifier key——组合快捷键中的修饰键shift或ctrl键
key——其他键
label——标题
控件按照下表配置:
File——〉Save 用户界面文件保存为“温度.uir”
3、生成源代码文件
Code——〉Generate——〉All Code弹出产生所有代码对话框
select panels to load and display at startup确定程序启动时要显示的面板,相应面板名前打勾
program termination选择回调函数用来实现结束程序的功能
若选择Generate WinMain() instead of main()选项,则会产生一个名为WinMain的主函数
源代码框架
#include <cvirte.h>
#include <userint.h>
#include "温度.h"
static int panelHandle;
/* 主函数 */
int main (int argc, char *argv[])
{
/*
函数原型: int InitCVIRTE(void HInstance, char *Argv[], void *Reserved)
*HInstance:若main为主函数,则输入值为0
*Argv[]:指向调试时生成的可执行文件的文件名
*Reserved:保留参数,一般为0
返回值:1表示运行成功;0表示运行失败,可能内存溢出
*/
if (InitCVIRTE (0, argv, 0) == 0)//初始化CVI运行时库
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "温度.uir", PANEL)) < 0)//姜用户界面文件载入到内存中
return -1;
DisplayPanel (panelHandle);//在屏幕上显示面板
RunUserInterface ();//运行用户界面
DiscardPanel (panelHandle);//从内存中删除面板及其子面板,并在屏幕中清除
return 0;
}
//Acquire 回调函数
int CVICALLBACK Acquire (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
break;
}
return 0;
}
//Quit回调函数
int CVICALLBACK Quit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);//在运行用户界面时,回退出用户界面
break;
}
return 0;
}
向源代码框架中添加回调函数(Quit Acquire)
Quit回调函数——按下按钮时退出用户界面
Acquire回调函数——开关控件处于ON时使LED点亮,且LED控件标题变为“开”。在Strip CHart上滚动显示100个随机产生的温度数值。在温度计Thermometer控件中显示即时温度。当温度数值产生完毕后,在最大值和最小值数值控件中,显示所产生的100个随机数值中的最大值和最小值;当开关处于OFF状态时,将LED熄灭,该控件标题变为“关”,温度计为零。
利用函数面板向框架中添函数
回调函数Acquire中首先要判断的是开关控件Binary Switch的状态是ON还是OFF。利用GetCtrlVal和SetCtrlVal函数来获得和设置控件的值,打开Library——〉User Interface Library——〉Controls/Graphs/Strip Charts——〉General Functions弹出选择函数面板,双击所需函数会弹出相应函数面板。
Acquire回掉函数源码
int CVICALLBACK Acquire (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
static double max,min;
static int max_index,min_index;
int i, j ,value;
double datapoints[100];//定义信号采集数据点数组
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal (panelHandle, PANEL_BINARYSWITCH, &value); //获取开关控件的状态
if(1 == value)//开关为开
{
SetCtrlVal (panelHandle, PANEL_LED, 1); //点亮LED
SetCtrlAttribute (panelHandle, PANEL_LED, ATTR_LABEL_TEXT, "开");//设置LED的标题变为“开”
for(i = 0; i < 100; i++)
{
datapoints[i] = 100*rand()/32767.0;//编写随机函数
Delay(0.1); //掩饰0.01s
SetCtrlVal (panelHandle, PANEL_NUMERICTHERM, datapoints[i]);//设置温度计控件的值
PlotStripChartPoint (panelHandle, PANEL_STRIPCHART, datapoints[i]);//逐点绘图在图表中滚动显示数据
}
MaxMin1D (datapoints, 100, &max, &max_index, &min, &min_index);//找出100个随机数中最大值和最小值
SetCtrlVal (panelHandle, PANEL_NUMERIC, min); //将最小值设置到控件中
SetCtrlVal (panelHandle, PANEL_NUMERIC_2, max); //将最大值设置到控件中
SetCtrlVal (panelHandle, PANEL_BINARYSWITCH, 0); //数据产生完毕后关闭“开始采集”开关
SetCtrlVal (panelHandle, PANEL_LED, 0); //关闭LED
SetCtrlVal (panelHandle, PANEL_NUMERICTHERM, 0.0);//设置温度计控件的值归零
SetCtrlAttribute (panelHandle, PANEL_LED, ATTR_LABEL_TEXT, "关");//设置LED的标题变为“关”
}
else
{
SetCtrlVal (panelHandle, PANEL_LED, 0); //关闭LED
SetCtrlVal (panelHandle, PANEL_NUMERICTHERM, 0,0);//设置温度计控件的值归零
SetCtrlAttribute (panelHandle, PANEL_LED, ATTR_LABEL_TEXT, "关");//设置LED的标题变为“关”
}
break;
}
return 0;
}
运行和调试程序
Run——〉Debug
Windows——>Variables即可浏览变量显示窗口
选择所要查看变量,右击选择View——〉Memory可浏览变量的内存显示窗口
生成可执行文件和发布文件
生成可执行文件Build——〉Target Type——〉Executable
目标设置对话框Build——〉Target Setting
生成可执行文件Build——〉Create Debuggable Executable
生成发布文件(MSI格式)
生成cds文件——〉Build——〉Distributions——〉Manage Distributions
生成“cvidistkit.温度”的MSI发布程序目录Build——〉Distributions——〉Build