Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Tutorial
NI Supported: Yes
Publish Date: Dec 31, 2007


Feedback


Yes No

Related Categories

Related Links - Developer Zone

How to Make a Win32 GPIB Application Using Direct Entry

6 ratings | 2.33 out of 5
Print

The following sections illustrate how to make a Direct Entry application. The code fragments are written in C, but the same techniques shown using C, can also be applied to other programming languages. For information about making GPIB Applications using Direct Entry, please refer to the following sections:


Overview Information
The use of direct entry means that the programs take the responsibility of accessing the 32-bit GPIB DLL. Direct access to the 32-bit GPIB DLL is accomplished by loading the DLL when the program is ready to use it (using LoadLibrary), obtaining addresses of the global variables and functions that the program needs to use (using GetProcAddress), and finally, unloading the DLL (using FreeLibrary) before exiting.

You can directly access the gpib-32.dll from any programming environment that allows you to request addresses of variables and functions that a DLL exports. The gpib-32.dll exports pointers to each of the global variables and all of the NI-488 and NI-488.2 calls.

Back to top
Items to Include in Your Direct Entry GPIB Application
Your application needs to include the header files, windows.h and decl-32.h. The 32-bit GPIB DLL exports pointers to the global variables and all of the NI-488.2 functions and subroutines. Pointers to the global variables (ibsta, iberr, ibcnt, and ibcntl) are accessible through these exported variables:
  • int *user_ibsta;
  • int *user_iberr;
  • int *user_ibcnt;
  • long *user_ibcntl;

At the beginning of your application, make sure that the following lines are included:

/* With C++ compiler, this prevents name-mangling */
#ifdef __cplusplus
extern "C" {
#endif


/* Necessary Header Files */
#include <windows.h>
#include "decl-32.h"


#ifdef __cplusplus
}
#endif


/* Pointers to NI-488.2 global status variables */
int *Pibsta;
int *Piberr;
int *Pibcnt;
long *Pibcntl;


/* Global variable for the handle to the loaded GPIB-32.DLL */
HINSTANCE Gpib32Lib = NULL;

Back to top


Casting the Function Pointers
Except for the functions explicitly listed below, all of the NI-488.2 function and subroutine names are exported by the 32-bit GPIB DLL. What this means is that to use direct entry to access a particular function (for example, ibwrt) all you need to do to get a pointer to the exported function is to call GetProcAddress passing the name of the function (for example, ibwrt) as a parameter. The parameters that you use when you invoke the function are identical to those described in the Format C section for the ibwrt command in the GPIB Function Reference Manual for Win32. To access the Function Reference Manual, please refer to:
  • On-line help
  • View Documentation option on the CD-ROM
  • Printed manual included with the GPIB Interface

There are a few functions that the 32-bit GPIB DLL exports with slightly different names. Here is a list of those functions:

  • ibbna
  • ibfind
  • ibrdf
  • ibwrtf
These functions all require an argument that is a name. ibbna requires a board name (for example, "gpib0"), ibfind requires a board or device name, and ibrdf and ibwrtf take a file name. Since Windows 2000/NT supports both normal, 8-bit characters and Unicode, 16-bit wide characters, gpib-32.dll exports two versions of each of these functions. An ASCII version is for 8-bit characters (ibbnaA, ibfindA, ibrdA, ibwrtA) and a "wide" version for 16-bit characters (ibbnaW, ibfindW, ibrdW, ibwrtW). Neither Windows 98 nor Windows 95 supports wide characters. So the only valid one to use is the 8-bit ASCII version.

To access any of these four functions in your application, you must use the correct function name when calling GetProcAddress. For Windows 98/95 applications, use the 8-bit ASCII versions named ibbnaA, ibfindA, ibrdfA, and ibwrtfA. For Unicode applications, use the 16-bit wide versions named ibbnaW, ibfindW, ibrdfW, and ibwrtfW.

The prototypes for each function can be found in the Function Reference Manual. For functions that return an integer value, like ibdev, the pointer to the function needs to be cast as:

int (__stdcall *Pname)

where *Pname is the name of the pointer to the function. For functions (i.e., the 488.2 calls) that do not return a value, the pointer to the function needs to be cast as:

void (__stdcall *Pname)

where *Pname is the name of the pointer to the function. They are followed by the function's list of parameters as described in the Function Reference Manual. Below is an example of how to cast the function pointer and how the parameter list is set up for ibdev and ibonl functions:

int (__stdcall *Pibdev)(int ud, int pad, int sad, int tmo, int eot, int eos);
int (__stdcall *Pibonl)(int ud, int v);

Back to top
Using LoadLibrary Function to Load gpib-32.dll
Within your application, you need to load the gpib-32.dll library. The example below shows you how to call the LoadLibrary function along with a way to handle an error:

Gpib32Lib = LoadLibrary("gpib-32.dll");

if (!Gpib32Lib) return FALSE;
Back to top
Using GetProcAddress Function to Get Pointers to Functions and Global Variables
Next, your Win32 application needs to use GetProcAddress to get the addresses of the global status variables and functions you need to use. The following code fragment illustrates how to get the addresses of the pointers to the status variables and any functions your application needs to use:

Pibsta = (int *) GetProcAddress(Gpib32Lib, (LPSTR)"user_ibsta");

Piberr = (int *) GetProcAddress(Gpib32Lib, (LPSTR)"user_iberr");

Pibcntl = (long *) GetProcAddress(Gpib32Lib, (LPSTR)"user_ibcntl");

Pibdev = (int (__stdcall *) (int, int, int, int, int, int)) GetProcAddress(Gpib32Lib, (LPCSTR)"ibdev");

Pibonl = (int (__stdcall *) (int, int))GetProcAddress(Gpib32Lib, (LPCSTR)"ibonl");


If GetProcAddress fails, it returns a NULL pointer. The following code fragment illustrates how to verify that none of the calls to GetProcAddress failed:
if ((Pibsta == NULL) || (Piberr == NULL) || (Pibcntl == NULL) || (Pibdev == NULL) || (Pibonl == NULL)) { FreeLibrary(Gpib32Lib); Gpib32Lib = NULL; return FALSE; } else return TRUE; 
Back to top
Dereferencing the Pointers to Functions and Global Variables
Your Win32 application needs to dereference the pointer to access either the status variables or functions. The following code illustrates how to call a function and access the status variable from within your application:
dvm = (*Pibdev) (0, 1, 0, T10s, 1, 0); if (*Pibsta & ERR) Then printf("Call failed"); (*Pibonl) (dvm, 0);
Back to top
Using FreeLibrary Function to Unload gpib-32.dll
Upon completion of your program, free the library with the following call:

FreeLibrary(Gpib32Lib);

Back to top
Compiling Your Application Back to top
Information about the GPIB Sample Programs
The Direct Entry C sample programs are located in <gpibpath>\LangInt\Direct (where <gpibpath> is where the GPIB Software for Windows is installed). Or you can download the
samples from our FTP site in WinZip format. The sample programs are Console Applications.

Back to top
Console Application
All the GPIB sample programs are of the Win32 console application type. They illustrate how to use the function calls exported by the GPIB driver. Since most of the popular Win32 compilers support console applications as well as Graphical User Interface (GUI) applications, it is simple to create Windows applications without knowledge of Windows Graphical User Interface (GUI) programming. A Win32 console application is a Windows program which uses text-based input and output, not a graphical interface. This allows you to quickly create a Windows application by using simple input and output functions like printf and scanf.

Back to top


6 ratings | 2.33 out of 5
Print

Reader Comments | Submit a comment »

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).