How to Make a Win32 GPIB Application Using Direct Entry
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
- Items to Include in Your Direct Entry GPIB Application
- Casting the Function Pointers
- Using LoadLibrary Function to Load gpib-32.dll
- Using GetProcAddress Function to Get Pointers to Functions and Global Variables
- Dereferencing the Pointers to Functions and Global Variables
- Using FreeLibrary Function to Unload gpib-32.dll
- Compiling Your Application
- Information about the GPIB Sample Programs
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 topItems 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
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
- Compiling a Borland C/C++ Direct Entry Application from within the Borland C/C++ Environment
Within the environment, you can compile, link, and run your application.
To compile the GPIB sample applications, when you create a new project, you need to make the following selections:- Target Type: Application (.exe)
- Platform: Win32
- Target Model: Console
Note: The required key strokes and menu bar selections needed in order to create, compile, and link a project vary with different versions of that particular compiler. For information about how to add files, compile, link, and run a project, please refer to the manuals and online help that came with your version of the compiler.
- Compiling a Borland C/C++ Direct Entry Application from the DOS Command Line
To compile a Console Application, called yourprog, from the DOS command line, type in the following:
bcc32 -w32 yourprog.c
To run the newly created executable, type in the name of your application on the DOS command line, like so:
yourprog - Compiling a Microsoft Visual C++ Direct Entry Application from within the Microsoft Visual C++ Environment
Within the environment, you can compile, link, and run your application.
To compile the GPIB sample applications, when you create a new project, select Console Application from the Type: list in the New Project Workspace dialog box.
Note: The required key strokes and menu bar selections needed in order to create, compile, and link a project vary with different versions of that particular compiler. For information about how to add files, compile, link, and run a project, please refer to the manuals and online help that came with your version of the compiler.
- Compiling a Microsoft Visual C++ Direct Entry Application from the DOS Command Line
To compile a Console Application, called yourprog, from the DOS command line, type in the following:
cl yourprog.c
To run the newly created executable, type in the name of your application on the DOS command line, like so:
yourprog
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
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/).
