labwindowscvi测试应用程序编程
- 格式:ppt
- 大小:187.50 KB
- 文档页数:14
LabWindows/CVI多线程技术在多功能显示器测试软件中的应用发布时间:2023-03-07T02:22:58.947Z 来源:《科学与技术》2022年21期作者:杨柳暄陈涛沈晓雪[导读] 多功能显示器测试软件模拟任务管理计算机与显示器进行422通讯,实时接收显示器发送的按键杨柳暄陈涛沈晓雪(苏州长风航空电子有限公司江苏苏州215151)[摘要]多功能显示器测试软件模拟任务管理计算机与显示器进行422通讯,实时接收显示器发送的按键信息,并且向显示器发送画面信息完成画面显示。
本文针对LabWindows/CVI的多线程机制进行了详细的介绍,并给出了其在多功能显示器测试中的应用。
测试结果表明,利用多线程技术能够更好地开发并行任务,加快系统的响应速度,提高执行效率。
关键词:多线程;多功能显示器测试;LabWindows/CVI;Application of LabWindows/CVI multi-threadtechnology in multi-function display test softwareYang Liuxuan Chen Tao Shen Xiaoxue(Suzhou Changfeng Avionics co.,LTD., Suzhou Jiangsu 215151,China)Abstract: The multi-function display test software simulates the task management computer to perform 422 communication with the display, receives the key information sent by the display in real time, and sends the screen information to the display to complete the screen display. This paper introduces the multi-thread mechanism of LabWindows/CVI in detail, and gives its application in the multi-function display test. The test results show that using multi-threading technology can better develop parallel tasks, speed up the response speed of the system, and improve execution efficiency.Key words: Multithread;Multifunction Display Test;LabWindows/CVI;1.引言在多功能显示器测试过程中,需要设计相应的测试软件。
l a b w i n d o w s-C V I-教程本页仅作为文档封面,使用时可以删除This document is for reference only-rar21year.MarchLabWindows/CVI基础教程序言LabWindows/CVI是National Instruments公司推出的一套面向测控领域的软件开发平台。
它以ANSI C为核心,将功能强大,使用灵活的C语言平台与数据采集,分析和表达的测控专业工具有机地接和起来。
它的集成化开发平台,交互式编程方法,丰富的控件和库函数大大增强了C语言的功能,为熟悉C语言的开发人员建立检测系统,自动测量环境,数据采集系统,过程监控系统等提供了一个理想的软件开发环境。
本教程面向的是那些从未使用过LabWindows/CVI的读者,但是假设读者以有了C语言的基础并且熟悉Windows 2000/9x/NT操作系统。
在每一章节的学习中,作者都是通过一个具体的实例让读者迅速的掌握本章的知识点;而不是长篇大论,述及边枝细叶,反而使读者望而却步,只见树木,不见森林。
想信通过对本教程的学习,读者可迅速掌握LabWindows/CVI编程思想及步骤,为读者日后进一步学习打下基础。
本书约定:“File>>Page Setup>>Options”File 这一种黑色斜体字是指多级菜单名,按扭名,窗口名或者是关键性词汇。
“>>”是指打开一个多级菜单或对话框。
如:File>>Page Setup>>Options是指你首先打开File 菜单,然后选择Page Setup项,最后从弹出的对话框中选择Options 项。
“说明:通过这种方式你可以修改Panel顶端所显示的名字。
”该种字体所写的文字是值得读者注意并记忆的内容。
“该椭圆形框所框住的内容是读者需修改的地方。
第一部分 CVI快速入门本部分通过编制一个简单的LabWindows/CVI程序,使读者对CVI编程环境,思想及步骤有一初步的认识。
第一章 : LabWindows/CVI1.1 LabWindows/CVI1.1.1 LabWindows/CVI概述LabWindows/CVI是美国NI(National Instruments)公司开发的面向计算机测控领域的虚拟仪器软件开发平台,可以在多种操作系统(WindowsXP/Vista/7、Mac OS和Unix)下运行。
LabWindows/CVI 是为C 语言程序员提供的集成开发环境(IDE),在此开发环境中可以利用C语言及其提供的库函数来实现程序的设计、编辑、编译、链接、调试。
使用LabWindows/CVI 可以完成以下但不限于以下工作:·交互式的程序开发;·具有功能强大的函数库,用来创建数据采集和仪器控制的应用程序;·充分利用完备的软件工具进行数据采集、分析和显示;·利用向导开发IVI 仪器驱动程序和创建ActiveX 服务器;·为其它程序开发C 目标模块、动态连接库(DLL)、C 语言库。
图 1‐1 LabWindows/CVI界面LabWindows/CVI 的功能强大在于它提供了丰富的函数库。
利用这些库函数除可实现常规的程序设计外,还可实现更加复杂的数据采集和仪器控制系统的开发。
数据采集。
IVI库、GPIB/GPIB 488.2库、NI-DAQmx库、传统的NI-DAQ库、RS-232库、VISA库、VXI库以及NI-CAN库。
数据分析。
格式化IO库、分析库以及可选的高级分析库。
GUI库。
使用LabWindows/CVI 的用户界面编辑器可以创建并编辑图形用户界面(GUI),而使用LabWindows/CVI 的用户界面库函数可以在程序中创建并控制GUI。
此外,LabWindows/CVI为GUI 面板的设计,准备了许多专业控件,如:曲线图控件、带状图控件、表头、旋钮和指示灯等,以适应测控系统软件开发的需求,利用这些控件可以设计出专业的测控程序界面。
CVI驱动程序编写入门仪器驱动程序有一个VPP规范(VXI即插即用),这个VPP规范中介绍仪器驱动的架构,使用的接口及前面板规范,函数原形规范等等。
看过这个规范的朋友可能知道,VPP就是对CVI开发仪器驱动的规范。
在开始编写驱动之前,请先安装NI-Visa、labwindows8.0下面介绍如何用CVI编写仪器驱动•生成仪器驱动程序工程文件(.prj)•设计函数树和函数面板(.fp, .c, .h)•完成驱动程序每个函数的代码(.c)•编译调试,生成动态链接库1 新建工程文件双击labWindows/CVI程序文件,打开CVI。
在“File”下拉菜单中选择“New”,再选择“Project”创建工程,弹出如图1-1对话框图1-1 创建工程在对话框中有两个单选项,前一个指在当前工作区创建工程,后一个指新建一个工作区创建工程。
通常选则后一个,这样在一个工作区对应一个工程,简单明了。
其他保持默认。
单击确定,创建工程完成。
2 设计函数树和函数面板这是最关键的一步,创建.fp、.c、.h等文件是仪器驱动中必不可少的文件,其中.fp 是仪器驱动的核心。
先介绍.fp创建过程。
在“File”下拉菜单中选择“New”,再选“Function Tree”,如图1-2图1-2 创建fp文件在右边白色区域,单击右键并点击”create Instrument”,弹出如下对话框,如图1-3图1-3 仪器节点“Name”指当前函数树的名字,”prefix”指函数名的前趋,在后面创建的.h文件中每一个函数名以zyosc开头,后面再跟实际函数名。
填写完后单击ok,如图1-4图1-4 函数树创建蓝色阴影部分的有这样的提示“create class or function panel window”,意思是在此可以创建类和函数前面板。
这里的类指用户如何划分函数,根据用户的需要将某些函数规为一类。
通常情况我们将错误消息、错误查询、复位,自检和版本查询等函数规为通用函数,具体操作硬件的部分的函数规为功能函数。
Labwindows/CVI串口程序最近对自己这篇文章再次查看了一下,感觉写得比较粗略,因此在此对其进行一些修改和详细讲解下。
希望对大家有用!串口的相关函数:1、打开串口函数OpenComConfig,相关定义如下:int OpenComConfig (int portNumber, char deviceName[], long baudRate, int parity, int dataBits, int stopBits, int inputQueueSize, int outputQueueSize);int portNumber 需要开启的串口号char deviceName[] 将要打开的串口号的名称,比如串口号1,名称为”COM1”long baudRate 设置串口读数的速度,波特率:110, 150, 300, 600, 1,200, 2,400, 4,800, 9,600, 14,400, 19,200, 28,800, 38,400, 56,000, 57,600, 115,200, 128,000, and 256,000,默认值为9600。
int parity 选择串口的校验模式,Default Value: 0—no parityValid Values:0 = no parity,1 = odd parity,2 = even parity,3 = mark parity,4 = space parityint dataBits 数据位数Default Value: 7 data bitsValid Values: 5,6,7 or 8 data bitsint stopBits 停止位Default Value: 1 stop bit Valid Values: 1 or 2 stop bits int inputQueueSize 串口输入的数据量,一般选择的4,096int outputQueueSize 串口输出的数据量,一般选择的4,096函数的返回值来判定串口的状态。
第5卷 第4期信息与电子工程Vo1.5,No.4 2007年8月INFORMATION AND ELECTRONIC ENGINEERING Aug.,2007文章编号:1672-2892(2007)04-0253-04基于LabWindows/CVI的数据库与Matlab混合编程及实现蒋 薇,夏连胜(中国工程物理研究院 流体物理研究所,四川 绵阳 621900)摘要:为了在Windows/CVI环境下实现基于数据库的复杂算法,讨论了LabWindows/CVI与Matlab混合编程以及LabWindows/CVI对数据库进行访问的几种方法,并在多脉冲电子束测控系统中运用三者混合编程,实现了大量现场数据的计算、管理等功能。
关键词:LabWindows/CVI;Matlab;数据库;多脉冲电子束;测控系统中图分类号:TP206+.1 文献标识码:AMixed Programme and Implementation Based on LabWindows/CVI Databaseand MatlabJIANG Wei,XIA Lian-sheng(Institute of Fluid Physics,China Academy of Engineering Physics,Mianyang Sichuan 621900,China)Abstract: Several methods of mixed programme based on LabWindows/CVI and Matlab are discussed.Some kinds of solution about accessing between LabWindows/CVI and database are also described.The controllingand measuring system of multi-pulsed current beam is designed and implemented under the truss ofLabWindows/CVI+database+Matlab.It can provide a way to implement some complex algorithms basing ondatabase in LabWindows/CVI.Key words: LabWindows/CVI;Matlab;database;multi-pulsed current beam;controlling and measuring system1引言NI公司的LabWindows/CVI(以下简称CVI)开发平台集成了从一般用户应用程序开发所需的界面编程到测试应用开发所需的各种仪器控制、数据获取、信息处理以及对数据库和网络应用开发的软件包,适用于自动测试、自动控制、测试仪器通信、测试硬件控制以及信号分析处理的软件开发。
1.1 LabWindows/CVI1.1.1 LabWindows/CVI概述LabWindows/CVI是美国NI(National Instruments)公司开发的面向计算机测控领域的虚拟仪器软件开发平台,可以在多种操作系统(WindowsXP/Vista/7、Mac OS和Unix)下运行。
LabWindows/CVI 是为C 语言程序员提供的集成开发环境(IDE),在此开发环境中可以利用C语言及其提供的库函数来实现程序的设计、编辑、编译、链接、调试。
使用LabWindows/CVI 可以完成以下但不限于以下工作:·交互式的程序开发;·具有功能强大的函数库,用来创建数据采集和仪器控制的应用程序;·充分利用完备的软件工具进行数据采集、分析和显示;·利用向导开发IVI 仪器驱动程序和创建ActiveX 服务器;·为其它程序开发C 目标模块、动态连接库(DLL)、C 语言库。
图1-1 LabWindows/CVI界面LabWindows/CVI 的功能强大在于它提供了丰富的函数库。
利用这些库函数除可实现常规的程序设计外,还可实现更加复杂的数据采集和仪器控制系统的开发。
数据采集。
IVI库、GPIB/GPIB 488.2库、NI-DAQmx库、传统的NI-DAQ库、RS-232库、VISA库、VXI库以及NI-CAN库。
数据分析。
格式化IO库、分析库以及可选的高级分析库。
GUI库。
使用LabWindows/CVI 的用户界面编辑器可以创建并编辑图形用户界面(GUI),而使用LabWindows/CVI 的用户界面库函数可以在程序中创建并控制GUI。
此外,LabWindows/CVI为GUI 面板的设计,准备了许多专业控件,如:曲线图控件、带状图控件、表头、旋钮和指示灯等,以适应测控系统软件开发的需求,利用这些控件可以设计出专业的测控程序界面。
网络和进程间通信库。
基于LabWindows/CVI的数据库编程在测试系统的应用开发中,测试数据通常是以文件的方式进行组织管理的。
这样的数据存储方式有一定的局限性,特别是远程测试系统,对于大量的数据不能及时的反馈。
要实现方便灵活的管理和访问,数据库是一种必然的选择。
LabWindows/CVI 是一个完全的标准C 开发环境,用于开发虚拟仪器应用系统。
CVI5.5 及以后的版本提供了支持数据库的sQL TOOLKIT 工具包,该工具包提供了丰富的数据库操作函数,给开发带来了方便和快捷。
1 基于PCI 的数据采集对于非NI 公司的PCI 数据采集卡,一般的驱动程序都是以动态链接库的形式提供的。
对于CVI 而言,可以直接利用驱动程序的动态链接库文件实现对采集卡的控制。
本文采用中泰PCI-8333 数据采集卡进行信号采集、存储和输出。
PCI-8333 数据采集卡具备丰富的采集与控制方法,同时也提供了动态链接库文件和头文件。
1.1 缩写采集卡头文件虽然厂商提供了头文件,但是LabWindows/CVI 需要用c 语言对头文件进行改写,参照厂商提供的头文件,编写部分方法声明如下:注意,头文件中包含的方法一定要与动态链接库中的方法—致。
编写后,将该头文件添加到应用程序中。
在程序源文件中声明头文件,添加#include“PCI8KP.h”。
1.2 产生dll 导入库文件将采集卡厂商提供的动态链接库文件加入刭应用程序所在的文件夹中,选择菜单:Option-Generate DLL Import Library,在弹出的对话框中选择OK 按钮,会弹出动态链接库选择对话框,选择加入到程序中的动态链接库后,会在该文件夹下产生导入库。
将该导入库也加入到当前程序的工程中。
1.3 应用动态链接库上面的两个步骤完成后,便可在CVI 应用程序中使用该动态链接库,调用头文件中已声明的方法进行数据采集源程序的编写,实现对效据采集卡的控制。
2 ODBC 及SQL Toolkit 工具包ODBC(Open Database Connectivity,开放数据库互连)是Microsoft 公司开发的一套开放数据库系统应用程序接口规范,它提供了统一的数据库应用编程接口(API),为应用程序提供了一套高层调用接口规范和基于动态连接库的运行支持环境。
@+labwindows+cvi测试技术及工程应用第一篇基础篇本篇主要介绍虚拟仪器的基本概念、虚拟仪器开发语言LabWindows/CVI的编程环境和程序结构以及基本控件的使用方法,并通过讲解简单实例,使初学者可以迅速掌握利用LabWindows/ CVI开发平台构建一般应用程序的基本思想、方法和步骤.本书使用的是LabWindows/CVI 8.0版本。
奢侈品保养虚拟仪器虚拟仪器(virtual instrument,简称“vi")是计算机技术、仪器技术和通信技术相结合的产物奢侈品维护.虚拟仪器的目的是利用计算机强大资源使硬件技术软件化,分立元件模块化,降低程序开发的复杂程度,增强系统的功能和灵活性。
奢侈品清洗1.1.1虚拟仪器的基本概念虚拟仪器基于计算机的软硬件测试平台,它可代替传统的测量仪器,如示波器、逻辑分析仪、信号发生器、频谱分析仪等:可集成自动控制、工业控制系统:可自由构建专有仪器系统。
它由计算机、奢侈品保养应用软件和仪器硬件组成。
虚拟仪器系统是将仪器硬件搭载到计算机平台,并辅以相应软件而构成的.奢侈品护理加盟虚拟仪器通过软件将计算机硬件资源与仪器硬件有机地融合为一体,从而把计算机强大的计算处理能力和仪器硬件的测量、控制能力结合在一起,大大降低了仪器硬件的成本,并通过软件实现对数据的显示、奢侈品维护存储以及分析处理功能。
从发展史看,电子测量仪器经历了由模拟仪器、智能仪器到虚拟仪器三个阶段。
奢侈品清洗如果在计算机中插入数据采集卡,利用计算机高速计算能力完成仪器信号的分析与处理、结果的输出,就可以把传统仪器的所有功能模块集成在一台计算机上,软件成为仪器系统的关键.在此基础上,美国国家仪器公司(National Instrument,简称“NI")提出了“软件就是仪器”的观点。
奢侈品保养奢侈品护理加盟虚拟仪器技术的优势在于可由用户自己定义通用仪器系统,且功能灵活,很容易构建,所以应用面极为广泛,尤其在科研、开发、.测量、检测、计量、控制等领域,更是不可多得的优秀开发工具。
第28卷第8期2007年8月微 计 算 机 应 用M I CROCOMP UTER APP L I CATI O NS Vol .28No .8Aug .2007 本文于2006204203收到。
基于Lab W i n dows/CV I 的仪器测试软件设计刘 娅1,2 李孝辉1 张慧君1(1中国科学院国家授时中心 临潼 710600 2中国科学院研究生院 北京 100039)摘要:在自动化测控系统的软件开发过程中,经常会遇到对仪器数据的采集和处理问题。
文中介绍了一种采用通用的仪器编程语言及I/O 通信方式,快速构建仪器测试平台的方法。
对于开发自控软件具有进一步的借鉴意义。
关键词:Lab W i n dows/CV I 软件 测试D esi gn of Test of I n strum en t Software Ba sed on Lab W i n dows/CV IL I U Ya 1,2,L I Xiaohui 1,Z HANG Huijun 1(1Nati onal Ti m e Service Center,the Chinese Acade my of Sciences,L int ong,710600,China2Graduate School of Chinese Acade my of Sciences,Beijing,100039,China )Abstract:During the course of devel op ing the s oft w are of aut omatizati on measure ment and contr ol,the matter of the acquiring and dis 2posing the instru mental data often appears .This paper intr oduced a method that it can be conveniently use general instru ment p r ogra m 2m ing language and I/O communicati on mode t o construct instru ment test p latf or m.It is significant f or devel op ing the s oft w are of aut o 2matic contr ol .Keywords:Lab W indows/CV I,s oft w are,test 随着电子技术的高速发展,电子测量仪器广泛应用于各种行业,电子仪器和计算机结合形成强大的组合优势也是目前仪器发展的一大趋势。
基于Lab Windows-CVI平台的虚拟仪器的设计与开发共3篇基于Lab Windows/CVI平台的虚拟仪器的设计与开发1随着电子技术的不断发展, 虚拟仪器作为一种数量庞大、功能多样的软件应用程序, 逐渐成为了各行各业进行测量、控制和测试的必备工具。
这些虚拟仪器通过计算机上的物理实验平台, 将传感器和其他实际输入设备的测量数据传输给计算机并进行处理, 最后通过软件界面来呈现出来。
在虚拟仪器的设计和开发领域中, LabWindows/CVI (Laboratory Windows/C语言 Visual Interface)平台已成为一种主流的选择。
这是由于LabWindows/CVI能够提供大量的函数库, 在实现各种测量和分析任务时具有更好的灵活性、可扩展性和稳定性。
本文将介绍如何在LabWindows/CVI平台上进行虚拟仪器的设计和开发,包括以下几个主要方面。
一、LabWindows/CVI软件环境和数据传输方式要实现LabWindows/CVI平台上的虚拟仪器设计和开发, 需要在计算机上安装LabWindows/CVI软件,然后将传感器所得的数据传入计算机。
数据的传输方式可以通过串口通信、USB接口、网口等方式进行,并对数据进行预处理,例如校准、补偿,以确保获得最准确的数据。
二、虚拟仪器的界面设计虚拟仪器的界面设计是虚拟仪器开发的关键。
理智的界面设计能够使用户快速进行各种实验,迅速了解实验结果。
在LabWindows/CVI平台上, 用户可以通过库函数来设计操作面板并实现交互。
LabWindows/CVI提供了丰富的控件(例如按钮、复选框、滑块、列表框和编辑框等),用于构建、显示和操作虚拟仪器界面。
此外,LabWindows/CVI还支持定制控件,以实现更加复杂的界面效果。
三、数据处理和分析算法的实现设计虚拟仪器的另一个重要方面是数据处理和分析算法的实现。
在LabWindows/CVI平台上,用户可以基于C语言自定义函数库来实现数据处理和分析算法,因此可以更加灵活地对接采集数据的传感器类型、样本数、采样间隔等各种参数进行调整。
2.3.2主函数及回调函数框架分步生成前面一节所讲述的是一次性生成全部代码,但在程序设计时,经常需要进行添加或删除面龟控件操作,上述的操作方法可能不能完全满足程序设计的需要,基于此原因,引入了主函数及回沙盘模型/调函数框架分步生成方案。
(1)生成主函数框架首先,在用户界面窗口中点击菜单Code--Generate-Main Function,邀NA产牛卞函数对话框,如图2-8所示。
__.可以设置自动生成的主函数是WinMain还是main函数。
.程序初始化时载入和显示的面板。
必须选择至少一个面板。
本例中只存在一个面板,所以只能选择该面板。
建筑模型/·定义选中面板的句柄,如雄有特殊要求,即可使用默认的面板句柄名。
‘、上述属性设置完成后,点击OK按钮,即可生成屯函数的框架。
保存文件,命名为“温度..c"。
选择菜单Edit--Add Files to Project--Source(*.c),把该文件添加到该工程中来。
主函数源代码框架如下:#include<cvirte.h>#include<userint.h>#include“温度.h"沙盘制作/static int panelHandle;int main(int argc,char*argv[])长if(InitCVIRTE(0,argv,0)==0)return一1;/*out of memory*/if((panelHandle=LoadPanel(0,return一1;DisplayPanel(panelHandle);RunUserlnterface Q;DiscardPanel(panelHandle);模型/return0;(2)生成回调函数源代码框架选择控件响应的事件。
单击用户界面菜单中Code-Preferences--Default Control Events,弹出控件回调事件对话框,如图2-9所示。
LabWindowsCVI(四):基本控件⽂本框、列表框使⽤及编程实例1、Text⽂本框 Create ——> Text ——> String 、Text Message 、Text Box 控件2、列表框(List Box)、树控件(Tree)、表格控件(Table) Create ——> Lists&Tables ——> List BOX 、Tree 、Table控件功能函数: Library ——> User Interface Library ——> Controls/Grqaphs/Strip Charts ——> General FunctionsNew Control :NewCtrl 新建控件,穿件⼀个新控件,并返回该控件的ID值Duplicate Control :DuplicateCtrl 复制控件,Discard Control :DiscardCtrl 删除控件Get Active Control :GetActiveCtrl 获得当前激活控件Set Active Control :SetActiveCtrl 激活制定控件Default Control Value :DefaultCtrl 设置控件默认值,恢复指定⾯板控件的默认值Get Control Value :GetCtrlVal 获取控件当前值Set Control Value :SetCtrlVal 设置控件当前值Get Control Attribute :GetCtrlAttribute 获取控件属性值Set Control Attribute :SetCtrlAttribute 设置控件属性值Get Control Bounding Rectangle :GetCtrlBoundingRect 获取控件的位置坐标3、⽂本框使⽤编程实例#include <formatio.h>#include <cvirte.h>#include <userint.h>#include "控件2.h"static int panelHandle;int main (int argc, char *argv[]){if (InitCVIRTE (0, argv, 0) == 0)return -1; /* out of memory */if ((panelHandle = LoadPanel (0, "控件2.uir", PANEL)) < 0)return -1;DisplayPanel (panelHandle);RunUserInterface ();DiscardPanel (panelHandle);return0;}//“转移”按钮的回调函数int CVICALLBACK transfer (int panel, int control, int event,void *callbackData, int eventData1, int eventData2){int maxitems;int i;char label[10];char Tag[32];int value;int checked;static int j=0;switch (event){case EVENT_COMMIT://清除指定列表框ClearListCtrl(panelHandle,PANEL_TREE);//向⽂本框的末尾添加⼀⾏⽂本InsertTextBoxLine(panelHandle,PANEL_TEXTBOX,-1,"转移并显⽰⽬录");j=0;//获得列表框项⽬的数⽬GetNumListItems(panelHandle,PANEL_LISTBOX,&maxitems);for(i = 0 ; i < maxitems; i++){//获得列表框中指定项⽬的选中状态//IsListItemChecked(panelHandle,PANEL_LISTBOX,i,&checked);IsListItemChecked(panelHandle,PANEL_LISTBOX,i,&checked);//如果该项⽬被选中,则执⾏以下操作if(checked){j=j+1;//获得指定索引项⽬的value值GetValueFromIndex(panelHandle,PANEL_LISTBOX,i,&value);//获得指定索引项⽬的标题GetLabelFromIndex(panelHandle,PANEL_LISTBOX,i,label);//将选中的项⽬插⼊到指定的树控件InsertTreeItem(panelHandle,PANEL_TREE,VAL_SIBLING,j-2,VAL_NEXT,label,"",Tag,value); }}break;}return0;}//“清除已选⽬录”按钮的回调函数int CVICALLBACK clear (int panel, int control, int event,void *callbackData, int eventData1, int eventData2){switch (event){case EVENT_COMMIT://清除控件的内容ClearListCtrl(panelHandle,PANEL_TREE);//向⽂本框末尾插⼊⼀⾏⽂本InsertTextBoxLine(panelHandle,PANEL_TEXTBOX,-1,"清除已选⽬录");break;}return0;}//“插⼊”按钮的回调函数int CVICALLBACK insert (int panel, int control, int event,void *callbackData, int eventData1, int eventData2){char newitem[10];switch (event){case EVENT_COMMIT://向⽂本框末尾插⼊⼀⾏⽂本InsertTextBoxLine(panelHandle,PANEL_TEXTBOX,-1,"添加⽬录");//得到指定String控件的值GetCtrlVal(panel,PANEL_STRING_INSERT,newitem);//向列表框插⼊新的项⽬InsertListItem(panelHandle,PANEL_LISTBOX,-1,newitem,0);break;}return0;}//“移除”按钮的回调函数int CVICALLBACK delete (int panel, int control, int event,void *callbackData, int eventData1, int eventData2){int maxitems;char removeitem[10];int i;char label[10];switch (event){case EVENT_COMMIT:InsertTextBoxLine(panelHandle,PANEL_TEXTBOX,-1,"移除⽬录");GetCtrlVal(panelHandle,PANEL_STRING_DELETE,removeitem);GetNumListItems(panelHandle,PANEL_LISTBOX,&maxitems);for(i = 0 ; i < maxitems ; i++ ){//获得指定索引的项⽬标题GetLabelFromIndex(panelHandle,PANEL_LISTBOX,i,label);//判断该标题是否和指定的内容相⼀致if(CompareStrings(label,0,removeitem,0,1)==0){//删除列表框指定项⽬DeleteListItem(panelHandle,PANEL_LISTBOX,i--,1);maxitems--;}}break;}return0;}//“退出”按钮的回调函数int CVICALLBACK QuitCallback (int panel, int control, int event,void *callbackData, int eventData1, int eventData2) {switch (event){case EVENT_COMMIT:QuitUserInterface(0);break;}return0;}⽂本框使⽤编程实例。
IVI State Caching for Groups of AttributesbyIan DeesApplication Software EngineerAnritsu CompanyCategory:TelecomProducts Used:LabWindows™/CVIInstrument Driver Development WizardPCMCIA-GPIB cardThe Challenge:Write an instrument driver capable of caching the values of multiple attributes that are set by the same command. The driver should avoid sending redundant command strings to the instrument, but must also make sure the instrument always has the correct values.The Solution:Using the advanced features of LabWindows/CVI, create shared read and write callback functions, which decide when to send commands to the instrument based on a set of hidden state variables.IntroductionAnritsu is using LabWindows/CVI to write an Interchangeable Virtual Instruments (IVI) driver for computer-based control of Optical Spectrum Analyzers. One of IVI’s benefits is state caching, through which the driver optimizes performance by remembering the state of the instrument. This feature is difficult to implement for some instrument attributes, which can only be accessed by complex commands that set a group of attributes at once. Anritsu and National Instruments collaborated on an innovative solution, which is able to deliver the performance boost of state caching even in difficult cases when a single command sets many attributes.Many Attributes Sharing One CommandWhen a command sets two attributes at once, the Instrument Driver Developers Guide recommends considering one of them to be “dominant” (2-20). When the driver writes the dominant attribute, it picks a reasonable default value for the subordinate one. This method works well for attribute pairs, but larger commands complicate the picture. Imagine a command that sets five attributes at once. We would have to select a dominant attribute, a “second-place”attribute, a “third-place” attribute, and so forth. For each combination of attributes one through four, we would need to choose a default value for the fifth. The attributes would always have to be set in the same order, lest one default value overwrite another.National Instruments Driver Support engineer Edward Zhu recommends a new approach: create an integer attribute representing the state of the entire group. This “state attribute” is hidden: the user must not be able to put the driver into an incorrect state. The IVI engine cannot cache this variable, because the write callback must be called every time the state attribute is changed. Both properties are easy to set in LabWindows/CVI (Figure 1).Figure 1: Configuring the State AttributeThe state attribute can have one of four values: “1 means no updates (instrument I/O) required and defer all future updates, 2 means future updates required, 3 means update now, and 4 is the do nothing state.” (Zhu) Figure 2 shows how our driver will manage the attributes based on the state attribute.Figure 2: State DiagramTo manage the transitions between states, we’ll define three callback routines:•The shared read callback is called by the IVI engine whenever a user-level attribute is read. The driver simply has to “do an instrument I/O, parse the data string, and update the cached values of all the attributes accordingly” (Zhu).•The shared write callback is called when an attribute is changed. This function sets the state attribute to inform the driver that the values in memory no longer match those in the instrument (Zhu).•The state attribute write callback (Figure 3, parts omitted for brevity) is only invoked when our state-caching code changes the state attribute. If the driver is in state 3 (i.e., if some attributes have changed), this callback sends all the attribute values to the instrument (Zhu).Figure 3: State Attribute Write CallbackThe state-transition behavior is hidden inside a function the user can call to set all the attributes at once. At the beginning of this high-level function (Figure 4), we set the state attribute to 1 to hold communications until all attributes have been set. Next, we call the appropriate SetAttribute function on each attribute. At the end of the function, we set the state attribute to 3, so that all the attributes will be sent to the instrument (Zhu).Figure 4: High-Level Configuration FunctionFinishing TouchesWe can make writing high-level configuration functions easier by moving the state transition logic into the callback functions. To simplify the process, let’s reexamine the state definitions (Figure 5):State 1:None of the attributes have been changed (the attributes are “clean”). If any of them change in the near future, don’t send any I/O (the attributes are “locked”).State 2:At least one attribute has changed (the attributes are “dirty”). The attributes are still locked, so do not send any I/O.State 3:The attributes are dirty and have just been unlocked. Send the new values to the instrument.State 4:The attributes are clean and unlocked.Figure 5: Modified State DiagramWe have replaced the integer state attribute with a pair of Boolean state attributes, Locked and Dirty, both of which share the state attribute write callback. Our high-level function (Figure 6) is now simpler: we set Locked to True before changing our attributes. When we’re done, we set Locked to False, and the state attribute write callback(Figure 7) will check the value of the Dirty and Locked flags to decide whether or not to send any communication.Figure 6: New High-Level Configuration FunctionFigure 7: New State Attribute Write CallbackIn the shared read callback Figure 8), after we query the instrument, some of the retrieved values may be older than the cached values we’re holding. For each attribute, we call AttributeIsCached to see whether or not its in-memory value has changed; if the attribute has not changed, we call SetAttribute to update it with the new value from the instrument. At the end of the shared read callback, a quick check of the “attributeId” parameter tells us which attribute we must return.Figure 8: New Shared Read CallbackFurther ImprovementsIn most cases, the attributes in a group are more diverse than in our five-integer example. Instead of sharing one read callback and one write callback, the group might share one integer read callback, one integer write callback, one floating-point read callback, and so on. These shared callbacks could call the same implementation function. Developers who are interested in more complex behavior could easily extend this state-caching scheme. For example, if one attribute’s range depends on another attribute’s value, more sophisticated range checking could be added into the callbacks. Another potential improvement would be to turn off our state-caching scheme when the user wants to disable state caching for the entire driver.Works CitedNational Instruments. Instrument Driver Developers Guide. Austin: National Instruments Corporation, 1998. Zhu,Edward.[************************].“RE:IVIQuestion.”Privatee-mailmessagetoIanDees.[********************].20October1999.。