LabWindows/CVI

Q: How many software engineers does it take to change a lightbulb ?
A: It can't be done; it's a hardware problem.
On this page:

Introduction

LabWindows/CVI is a great package from National Instruments. Its main purpose is to be a powerful tool for writing data acquisition programs, but it's really a multipurpose 32 bits C compiler, doubled by a code generator and some of the most powerful C libraries ever seen on a PC (easy to use and very complete User Interface, signal/maths and more with Advanced Analysis, Data Acquisition, VXI, GPIB 488, VISA, TCP, DDE, formatting and I/O, Utilities and finally ANSI C).

Note: I have recompiled all the .lib and .fp with CVI version 5.5 and the .fp are not backward compatible with previous versions of CVI. Sorry. You can still try to use them by linking directly to the .lib.

First things first, 2 headers that everybody should have, the iso646.h for meaningful connectors and some version of my def.h useful definitions file.

There are a few things missing from early (or even current) versions of LabWindows. Here is some code I wrote or adapted for getting the available disk size, doing on the fly statistics, doing color scaling, getting compiled Html Help files (*.chm) to display contextual help and doing extended time formatting. You can right click on a frame and select [Open frame in new window] to get a full size listing; or copy/paste the frame into your favorite editor...



Getting the available disk size
On the non-compatibility of Bill Gates with himself.

DOS, Windows 3.*

This is the source code for a small 16 bits DLL for compilation with Microsoft C v1.52. I don't know if this will compile for Win95.

Category:
Windows 3.11, sample C program

3 source files: DiskFree.c, DiskFree.h and DiskFree.def in DiskFree.zip.

Win95

Things are different here, playing around with interrupts is no longer a Good Thing, and it is better to use a call to a Windows SDK function (source sent by ). Note the #include to windows.h:

One source file: AvailableDiskSize.c in DiskFree.zip.

Category:
Windows 95, sample C program

Win95-OSR2 or Win98 with hard drive > 2Gb

Damn, this is turning into a real tutorial. If your HD is bigger than 2Gb, GetDiskFreeSpace will return only 2Gb. You have to use GetDiskFreeSpaceEx, but there's a catch... it uses 64 bits integer arithmetics, and CVI won't support them. The hack is to use a struct with 2 long and some conversions to double. Note that this function returns a volume free size in Mb, not bytes.

Category:
Windows 98, sample C program

LabWindows/CVI version 5.5

Starting with version 5.5, there is a GetDiskSpace function in CVI. But it still requires 64 bits integers which CVI does not support. Bouhhh...

LabWindows/CVI version 7

There's now full support of 64 bits integer with __int64. It works directly for instance with the fsetpos and fgetpos file functions for files above 2Gb. Example: __int64 Position; [...] fsetpos(File, (fpos_t*)&Position));


Getting HtmlHelp files to display from your application

The new HtmlHelp system is a great way to write you online documentation if you already know HTML. But then, how do you call it from your application ? I hacked this short module for LabWindows in order to display contextual help, the ChmHelp() function is similar to the SystemHelp() function and if defined in HtmlHelp.h. Note, you need to include HtmlHelp.lib and Advapi32.lib to you project. You need the Microsoft HtmlHelp workshop in order to get those libraries (and to compile chm files).

Two source files: ChmHelp.h and ChmHelp.c.

Category:
Windows 95 + IE4, sample C source code

LabWindows/CVI version 5.5

Starting with version 5.5, there is a ShowHtmlHelp function in CVI. You don't need to link anymore with HtmlHelp.h and HtmlHelp.lib. But you still need to prepend the mk:@MSITStore... stuff.


Showing all attributes

Sometimes there's something wrong with a control. It doesn't behave as expected. Maybe you changed an attribute with the wrong panel/control association. And the cause is hard to pinpoint since it can happen in a totally unrelated piece of code. Unless NI makes a SetBreakOnAttributeChange function, we are stuck with looking at the properties of the control. But we can't do that from the debugger, it needs to be done from the code itself. And to further complicate it, every type of control has different attributes.

So I wrote this little module to dump all the valid attributes off a panel or control. It just prints them all. Two source files: ShowAttributes.h and >ShowAttributes.c.

Category:
LabWindows/CVI user interface Panel, Debugging
2012/03/23, Original functions.

Have you ever noticed that in the user interface editor you can move between panels using Ctrl-shift and the left or right arrow ? Well, here's a little piece of code that allows you to do the same thing in your C programs. In addition it will print or center your panels on request. Note that on Linux the keymaps are a bit different so you may want to verify them.

Two source files: PanelManagement.h and PanelManagement.c.

Category:
LabWindows/CVI user interface Panel, Navigation
History:
1999/09/18, Original functions.
2008/11/28, Reshapped a bit.


Saving & reading PNG files
LabWindows/CVI as a cgi-script web generator.

OK, so LabWindows/CVI can convert a panel or control to a bitmap and then save it as as .bmp file. Hmmm, not very useful: you can't put it on the Web and it takes a huge amount of space on the disk. The JPG format is not very adapted to saving a typical panel (the text gets hard to read because of the lossy compression), and the GIF format is limited to 256 colors and is a proprietary format. So what gives ? The only other compressed format that is common on the Web is the Portable Network Graphics (PNG) format that can be viewed in IE4 or NS4 or above.

So I got the LIBPNG library and the ZLIB library from their respective web sites and managed to compile them for LabWindows/CVI. I wrote this Function Panel to save a bitmap, a panel or a control to a PNG file, and inversely that can read a PNG file into a bitmap or a picture control. LIBPNG and ZLIB are public domain, so this FP file is too.

PNG files have a lot of characteristics we don't need here: they can have RGB 3*8 or 3*16 bits (possibly with 8 or 16 bits alpha channel), greyscale 1, 2, 4, 8 or 16 bits (possibly with Alpha channel). Palettes of 1, 2, 4, 8 or 16 colors with multiple transparencies, comment text... Basically here we save in RGB 3*8, although I have put options for Alpha channel, greyscale conversion and black&white conversion.

Saving: If your graphic card is in 16/24-bit mode, it saves 24-bits RGB colors PNG files without alpha layer; if your card is in 32-bit mode, it saves a 24-bits RGB image with the option of having the current selection in the alpha layer. It won't work with monitors in paletted mode, I didn't want to bother with palettes, although if you pay me I could implement it in less time than it takes for a French trucker to go on strike. This 3*8 RGB should be OK for most applications, but you can save room by saving as greyscale (3 times smaller uncompressed) or black&white (24 times smaller uncompressed). PNG control options such as interlacing, gamma (default is set to 1, but you might get more accurate colors with gamma set to 0.5), resolution, compression level and comment texts are implemented.

Reading: It can read any PNG file I've cared to test: paletted 1/2/4/8/16 bits, 3*8/3*16 RGB, 8/16 greyscale, alpha layers or no, but it converts everything to 3*8 bits RGB. Image comment texts are read. No gamma conversion.

I don't give the source code here because it relies on LIBPNG and ZLIB and needs a lot of source files. The FP file contains extended information on use.

Note: Do you want to generate web pages to present those nice PNG files ? Create a cgi-script exe file with LabWindows ? Then you can easily compile and use the public domain cgic106 library in version 5.5 of LabWindows/CVI (you need the possibility to do console applications). And use my PanelToCgi code generator to convert one of your existing CVI application to the web !

Some sample files: TestReadSavePng.c and a function panel (ReadSavePng.fp/lib) in ReadSavePng.zip (111Kb). Example of use:

	// Saving
	Png_SetParameters (1, 1, 72, Z_BEST_SPEED, FALSE);
	Png_SetTextAuthor("Guillaume Dargaud");
	Png_SavePanelControlToFile (PanelHandle, 0, "PanelSpeedInterlaced.png");
	// Reading
	Png_ReadControlFromFile(PanelHandle, PictureControl, 0, "PanelSpeedInterlaced.png");
	printf(Png_GetTextAuthor());
Category:
ANSI C, LabWindows/CVI Function Panel, Graphics
History:
1999/09/18, Original SaveAsPng functions
1999/12/18, Added ReadFromPng functions, added color and 2/8/24/32 bits conversion routines
2000/24/14, there was a bug for 16/24 bits graphic cards.
2001/12/18, Partial compatibility for Unix (just remove the two panel related functions). Added function to read the file parameters.
2001/04/15, Explosive bug in reading greyscale and 32 bit images corrected
2001/05/10, Use of bitmaps when reading now optional
2001/08/15, added more secure check on reading illegally formatted text in text fields.
2002/09/24, corrected error in reading greyscale files. Removed some buggy Alpha support.
2002/09/27, corrected error in reading palleted files.
2002/11/30, Added a function to read the file properties without reading the bitmap.

Saving & reading JPEG files
LabWindows/CVI as a cgi-script web generator.

OK, so just above here you have downloaded my fp so that you can read and save images of panels/controls/bitmaps in LabWindows/CVI. PNG files are great but for pictures you might want to use the superior compression of the JPEG format. You have to be aware of the limitations: JPEG works well with RGB or greyscale pictures but not well at all with the typical panel containing text and graphics. JPEG is also the standard image file on the Web.

So I got the JPEG-6B library from the Independent JPEG Group and managed to compile it for LabWindows/CVI. I wrote this Function Panel to save a bitmap, a panel or a control to a JPEG file, and inversely that can read a JPEG file into a bitmap or a picture control. IJG's JPEG-6b is public domain, so this FP file is too.

JPEG files are either 24 bit RGB or 8 bit greyscale. This FP accepts 32 bit arrays but will convert them. On reading, greyscales are converted to RGB. JPG does not support palettes, alpha channels or transparencies.

Limitations: It won't work with monitors in paletted mode, I didn't want to bother with palettes, although if you pay me I could implement it in less time than it takes for a French trucker to go on strike. Also I haven't implemented interlaced on saving (it will read them, though).

I don't give the source code here because it relies on JPEG-6b and needs a lot of source files. The FP file contains extended information on use.

Note: Do you want to generate web pages to present those nice JPEG files ? Create a cgi-script exe file with LabWindows ? Then you can easily compile and use the public domain cgic106 library in version 5.5 of LabWindows/CVI (you need the possibility to do console applications, so it won't work with earlier versions). And use my PanelToCgi code generator to convert one of your existing CVI application to the web !

A function panel (ReadSaveJpeg.fp/lib) in ReadSaveJpeg.zip (95Kb). Example of use:

	// Saving
	Png_SavePanelControlToFile (PanelHandle, 0, "PanelSpeed.jpg", 75);
	// Reading
	Png_ReadControlFromFile(PanelHandle, PictureControl, 0, "PanelSpeed.jpg");
Category:
ANSI C, LabWindows/CVI Function Panel, Graphics
History:
2001/05/05, v1.0, original version.
2001/05/10, Use of bitmaps when reading now optional
2001/06/13, corrected bug when reading greyscale JPEG
2001/08/16, now returns in stead of exiting in case of error
2002/11/30, Added a function to read the file properties without reading the bitmap.


OK, so there are several predefined popups, like ConfirmPopup or GeneralPopup that you can call, and they will return the button clicked by the user. But how do I make a custom popup, for instance the classic [Yes|No|YesToAll|NoToAll|Cancel] popup ?

I mulled that one over for a while, until a suggestion from Martin J. Saxon: the trick is to call RunUserInterface again... Here's the sourcecode for the [Yes|No|YesToAll|NoToAll|Cancel] popup.

Category:
ANSI C, LabWindows/CVI Function Panel, Popup
History:
2001/08/16, v1.0, original version.
2009/03/04, Precision popup.

Here's another example which allows you to change the format, precision and padding of a numeric control:

Note: you may be thinking about doing the same for a graph properties popup, but this is largely done already if you use legends and enable [Interactive Legend].


[EditAxisSettingsPopup.png]
Edit axis settings popup

And yet another example which allows you to change the format, precision and a few other attributes to a graph axis:


[SlideRampPopup.png]
Slider ramp settings popup

And yet another example which allows you to change some cosmetics on a slider popup, including the ramp colors, needle width, etc:

And yet another example which allows you to change the ON and OFF colors of a simple LED or button:


More events

There are many default events in LabWindows, but some are missing. For instance you can drag and drop files from explorer onto a panel only if you call the EnableDragAndDrop function. Something else that is sometimes necessary is shift-click, ctrl-click and alt-click. The code is below. The thing I haven't been able to figure out yet is how to separate and get the coordinates of mouse-down and mouse-up on a canvas, which is necessary for drawing a box for instance.

Category:
ANSI C, Windows, LabWindows/CVI, User Interface
History:
2005/03/06, original version.
#include <windows.h>
...
int CVICALLBACK cb_View (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) {
	BOOL KeyShift, KeyCtrl, KeyAlt;
	
	// can be combined
	KeyShift=GetAsyncKeyState(VK_SHIFT)>>8;	// >>8 if the key is pressed right now,
	KeyCtrl =GetAsyncKeyState(VK_CONTROL)>>8;	// &0xFF if it's been pressed since the last call
	KeyAlt  =GetAsyncKeyState(VK_MENU)>>8;

	switch (event) {
		case EVENT_LEFT_CLICK:
			if (KeyShift) ...;
			if (KeyCtrl) ...;
			if (KeyAlt) ...;
			break;
		...

Another trick that has to do with user input is to regroup the keyboard events to the top panel. For instance if you have a child or separate panel that catches a few keyboard event but you want all other keyboard events to go to the main one (useful for instance if the menu bar is only on the main panel), do the following in the child panel callback function:

switch (event) {
	case EVENT_KEYPRESS:	
		switch (eventData1) {	// Keyboard events recognized by child panel
			case 'm': DoSomething(); return 1; // Act and swallow the event
			...
		}
		// Key not recognized, passed to Main window
		SetActivePanel(MainPanel);
		FakeKeystroke (eventData1);
		return 1;		// We swallow the event but fake it again
}


[TSL1.png]
Non-deterministic time labels on strip chart.

Arbitrary time axis in strip charts

TRecent versions of CVI have added a SetCtrlAttribute (panel, control, ATTR_XFORMAT, VAL_ABSOLUTE_TIME_FORMAT) and also a relative time format. The problem is that the data must be sent to the strip chart precisely 1s apart. Thus you cannot use a timer which lacks the precision. So I wrote this set of functions which writes the dates as text labels on the axis. It should work with an arbitrary number of strip charts at the same time.

It works quite simply: you call TSL_Auto with the expected update rate of your data, and then you call TSL_PlotStripChart to add points to the strip chart. That's about all there is to it, even if the data comes at non-deterministic intervals. It can use any resolution from one second to a year.

Here are 3 examples at various time scales:

Category:
Library, LabWindows/CVI, User Interface
History:
2012/02/14, Original version.
2012/02/15, Fixed bug when using multiple strip charts
2012/02/21, Finer-grained intervals
2012/03/05, Tiny improvements

2 source files (TimeStripLabel.c/h) and 3 test files (TimeStripLabelTest.uir/h/prj) in TimeStripLabel.zip. And here's the header file:



[GenericStripChart.png]
Generic strip chart interface.

Generic strip charts

Strip charts are a good way to offer real-time display of data but their programming can be a tad tricky. Here's a piece of code that takes a bunch of variable pointers and gives the user the possibility to display what he wants on how many strip charts as he wants. Basically.

It works quite simply: load the GSC panel from GenericStripChartUI.uir, then call GSC_SetProperties, then call GSC_AddVariable as many times as needed. Done.

If you compile the project with the GSC_DEBUG option, you get a full example executable. Also you can compile the file with TimeStripLabel.c or PanelManagement.h for added functionality.

Category:
Library, LabWindows/CVI, User Interface
History:
2012/02/21, first version.
2012/02/24, corrected fatal bug.
2012/02/29, added GenericXYplot (see below).
2012/03/05, added EditAxisSettingsPopup option.
2012/03/20, minor fixes.
2014/03/28, minor fixes.

2 source files (GenericStripChart.c/h) and 2 UIR files (GenericStripChartUI.uir/h) in GenericStripChart.zip. And here's the header file:



[GenericXYplot.png]
Generic XY plot interface.

Generic XY plots

X/Y plots are the main way to display one data versus another and here's a simple panel that will take an arbitrary list of double variables and lets the user chose a pair to display against each others. It will display data for a while and then silently remove the oldest to avoid overflow or overloaded display.

It works quite simply: load the GXY panel from GenericStripChartUI.uir (it's the same file as above, but does no require the use of strip charts), then call GXY_SetProperties, then call GXY_AddVariable as many times as needed. Done.

Category:
Library, LabWindows/CVI, User Interface
History:
2012/02/29, first version.
2012/03/05, added EditAxisSettingsPopup option.

2 source files (GenericXYplot.c/h) and 2 UIR files (GenericStripChartUI.uir/h) in the same zip file as above: GenericStripChart.zip. And here's the header file:


LabWindows Real-Time

As of 2007, LabWindows can run as real-time. The setup is fairly complex: you develop your code on a Windows machine, in the usual CVI IDE, and then you push it onto a machine running a real-time minimalist OS called PharLap ETS. Now I've had some trouble getting that thing running. The remote machine (also called real-time target) can be a PC, a PXI box or some other compbination, but for PCs the requirements are actually pretty tight:

Now NI gives you several ways to configure the target. I'll speak first about the OS installation, then about the network config:

USB Boot
If you can boot from USB, go in Measurement & Automation Explorer (MAX), in [Tools][RT Disk Utilities] and create a Desktop PC utility USB drive. You can do several different things when booting from this drive: run the real-time OS, install it on the drive, test to see if the hardware is compatible with the RTOS... Below I'll detail the phases of a floppy install, but it applies to a USB install as well.
Floppy test
In MAX, create a Desktop PC Boot disk. Boot your remote system with it. It should finish with a message similar to "Initializing network... Device 1 - MAC address:... - 0.0.0.0 (primary) System state: unconfigured". If you get an immediate lockup, the processor is probably not compatible. If you get an ETS Loader.ERR, make sure that you floppy drive is working properly (they are often full of dust or the floppies are so old as to be useless). You get another kind of message if your network card is not compatible.
Hard drive preparation
You need an empty disk, formated in FAT32, without MBR. Easier said than done. For instance departitionning an existing disk with [Start][Settings][Control Panel][Administrative Tools][Computer Management][Disk Management] won't work if you have grub or somesuch as a boot loader. Here's a clean way to do it:
Network configuration
NI documentation seems to imply that the network configuration happens by magic: the remote boots with the (non-possible) IP address 0.0.0.0, is seen from the master PC, and you configure and run it from there. Not quite. We have a tightly secured network and there was no way I could see an IP of 0.0.0.0. There's no information in the NI documentation as to what kind of broadcast was happening, so we could try to allow it at the firewall level or somesuch.
CVI specific config
Go back to MAX, [Remote Systems], expand your target, [Software][Add/Remove software] (the button is above), select [LabWindows/CVI Runtime Engine for RT] and [LabWindows/CVI Network Variable], as well as other requirements (VISA...)

Anyway, to make a long story short, I wasn't able to properly run the RTOS on my test laptop: incompatible network card. It's the proper chipset but it needs to be PCI and not MiniPCI. End of the story for now.


LabWindows on Linux

As of version 8.0 (2007), National Instruments provides a version of LabWindows/CVI for Linux. It was updated in 2010. Here are some notes about using it.

RPM
Chose an RPM-based distro: Red Hat, Suse and a few others are in. Gentoo, Ubuntu are out unless you want to play with rpm/deb converters. NI lists a limited set of Linux distribution, but I've used another one with success, still RPM-based (Scientific Linux).
Non RPM ?
If you'd rather use a modern Linux such as Ubuntu, you can try to copy the rpms to a writable directory, then convert them to .deb using: alien --to-deb --scripts *.rpm
CD
The original Linux CD I had contained a Linux runtime engine but no Linux compiler (cvicc). I have no idea what I can do with a runtime without compiler. Note that unlike the free distribution of the runtime on Windows, the distribution of the Linux runtime costs money. I have no idea why there's a difference but it's worth asking NI.
Motif
You need OpenMotif 2.2 for the user interface (quite archaic IMHO): sudo yum install openmotif22.
Visa
In my case I want CVI to run Visa USB drivers. Visa is on the install CD, but can also be downloaded from here.
Kernel
Visa and a few other libraries are in the form of kernel modules, so if you change kernel version, you need to reinstall some of the CVI components. Note that I wasted time on the kernel issue for nothing: on Red Hat, the kernel gets installed in specific ways, either with # up2date kernel-devel or # up2date redhat-rpm-config rpm-build [...]. I used the manual way where you download the tar file from kernel.org, put it in /usr/src, ln, make menuconfig, etc... This does not work with the NI kernel modules. The kernel must be installed in the approved Red Hat way.
Development
The main difference with CVI for Windows is that there's no development environment... So you have to develop on Windows, particularly you have no choice for the UIR. I use a shared SMB folder between a Windows and a linux machine, create the project and the UIR files in the CVI IDE, compile and test on Windows. Then all I have to do is open a shell in the same directory on the Linux side and type cvicc project.prj to compile it. It creates an executable with the same name, but for Linux. Very simple.
Note that the compiler sometimes confuses Windows and Linux intermediate files, so if your compilation isn't huge, it's better to do rm -rf cvibuild* between each compile.
The underlying compiler is actually gcc and you can change the default parameters in the file /usr/local/natinst/cvi2010/bin/compile.ecc, for instance here I do change -g to -ggdb and -c99 to -gnu99 (to get rid of the 'warning : implicit declaration of function ...' message when using POSIX functions):
CMPLRNAME = GCC
VALID_CFG = 1
DOPARSING = 0
OPT_LEVEL = 0
WARNLEVEL = 0
USERFLAGS =
COMPFLAGS = -c -x c -m32
C89_FLAGS = -std=gnu89
C99_FLAGS = -std=c99
LINKFLAGS = -rdynamic -m32
COMPLPATH = gcc
ENV_BATCH =
OBJ_TEMPL = -o $OBJECT
SRC_TEMPL = $SOURCE
DEF_TEMPL = -D$MACRO
DEFVTEMPL = -D$MACRO=$VALUE
INC_TEMPL = -I$PATH
LIB_TEMPL = -l$LIBRARY
LIBPTEMPL = -L$PATH
LIB_PATHS =
USE_LIBRY = nianlys;pthread;cvintwrk;ninetv
ABS_LIBRY =
OPT_FLAGS
Debug Opt = -g
Release Opt = -O2
END_FLAGS
WARNFLAGS
Warnings =
END_FLAGS
__CEND___
X-windows
The compilation step runs fine in a text shell, but in order to run the program on Linux from windows, you need X-Windows. The one integrated with cygwin keeps crashing, and I'm having more luck with Xming. But currently I simply develop on a virtualbox WinXP in a directory exported by VirtualBox, and compile on linux. There's a bug when working this way: if you remove some text from a text file, the CVI editor truncates it, but the junk content behind the truncation remains in the file, so the Windows compilation works fine while the Linux one gives errors on extraneous characters which you can't see in the CVI editor. If you close and reopen CVI or if you open the file with a Linux editor, you can see the problem. It's a pain.
Libraries
Some additional NI libraries are installed with the compiler, they are present in /usr/local/vxipnp/linux/bin/) and can be linked to with 'cvicc -lvisa project.prj'.
Standard Linux libs can be used as well, for instance 'cvicc -lusb project.prj' for libusb which I've used with success to develop a USB driver for a custom instrument for both Linux and Windows.
Debuggin'
Use cvicc -debug project.prj and then run it as kdbg ./project or kdbg ./project_dbg (depending on your target setting). Then do [File][Open source], type *.c and [OK] so that the debugger has all your source files ready for stepping into. Very usable if a little 'ancient' compared to the CVI debugger.
Updates
The linux version is a bit behind the Windows version. For instance I've had trouble compiling some functions from toolbox.h, such as GetDiskSize in version 8.0.
Predefined Macros
The only new macro that I can find is _NI_linux_. Of course, all the WIN something macros go away. Careful that the _CVI_ macro which normally gives the version number IS NOT DEFINED.
Bugs
Here are some bugs I've noticed under Linux:

Support my site: make a donation, buy images, use associate services

Conclusion

Download: Here's a summary of the available downloads:

Some tricks:

Other resources about LabWindows/CVI include: