Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

NI 488.2 from VS2013 C++. How?

This has been very frustrating.

I'm trying to use a NI GPIB board to talk to an instrument. This needs to happen in C++ (not .NET) because it needs to run as a compiled executable in a tester platform.

I'm running VS Professional 2013. I have installed the latest GPIB software, version 17.0.0. 

 

In NIMax, interactively everything works fine. We just need to initialize, send a command and receive an answer. ibfind, ibwrt, ibrd is ALL I NEED. 

 

The latest version of the GPIB User Manual (2015), has this to say on p. 4-12: Microsoft Visual C/C++ (Version 8.0 or later): 

With Microsoft Visual C++ (Version 8.0 or later), to compile and link a Win64 console 

application named cprog in the Visual C++ x64 Command Prompt using the environment 

variable, "NIEXTCCOMPILERSUPP", type in the following on the command line:

cl /I"%NIEXTCCOMPILERSUPP%\include" cprog.c 

"%NIEXTCCOMPILERSUPP%\lib64\msvc\ni4882.obj" /MD

 

Frankly, this is B.S. 

 

I use:

#include "ni4882.h"

 

In order to have the code compile, I copied ni4882.h and ni4882.obj into a local folder. The code compiles, but none of the commands works. 

 

ibdev(0,6,0,T3s,1,0)

 

fails with -1. AND I DO NOT SEE ANYTHING IN GPIB TRACE in NIMax (whereas I see it when I run commands interactively). 

 

Can somebody please help:

- what do I have to do in VS 2013 to correctly link the functions so that they actually work?

- would linking to DLL work better?

 

I think NI documentation is really lacking here...

 

Thank you for your help!

0 Kudos
Message 1 of 10
(5,162 Views)

Hey davide_guarisco,

Which NI GPIB board and which 3rd Party Device are you using within your system? 

0 Kudos
Message 2 of 10
(5,124 Views)

@A_hint_from_trent: I am using a standard National Instruments GPIB PCIe board. Don't have the exact model with me right now, but the board functions perfectly in NI-MAX. The instrument is a Kepco BOP power supply (although this doesn't matter).

 

I am now also looking at a VISA solution as well. For me, all solutions (488.1 "ib" functions, 488.2 functions, or VISA functions are equivalent as long as they get the job done). Same questions apply. I am puzzled by the lack of documentation from NI on this topic. The examples are for very old versions of VS and they do not really explain the steps needed to add the dependencies. 

0 Kudos
Message 3 of 10
(5,119 Views)

We have made significant progress, but it is still not working. 

 

First, a general overview of how to include a third-party library in Visual Studio:

https://stackoverflow.com/questions/20058864/how-to-include-libraries-in-visual-studio-2012#20059167

 

Second, a case specific to NI-VISA:

https://forums.ni.com/t5/Instrument-Control-GPIB-Serial/Missing-dependencies-in-Visual-C-for-VISA/m-...

 

So, what we did:

1. Install the latest versions of NI-488.2 (17.0.0) and NI-VISA (17.5)

 

2. Add: 

#include "visa.h"

 

3. Add the following path to the project "include directories" (> Configuration Properties/VC++ Directories/Include Directories (click and edit, add a new entry)):

C:\Program Files\IVI Foundation\VISA\Win64\Include

Note that this path is different from the link above. 

 

4. Add the lib files. First step is to add a library directory--> project(on top bar)/properties/Configuration Properties/VC++ Directories/Library Directories (click and edit, add a new entry):

C:\Program Files\IVI Foundation\VISA\Win64\Lib_x64\msc

Second step is to add the following in the source code:

 

#ifdef WIN64
 #pragma comment( lib, "visa64")
#else
 #pragma comment( lib, "visa32")
#endif

Note that this is also different from the link above. 

 

But it still isn't working. The program compiles just fine, but we get link errors for each of the functions we want to use, such as:

1>gpibcode.obj : error LNK2019: unresolved external symbol _viOpenDefaultRM@4 referenced in function "int __cdecl __TestBlock_tb_kepco_Visasetup_body(void)" (?__TestBlock_tb_kepco_Visasetup_body@@YAHXZ)

Anyone knows how to resolve this?

0 Kudos
Message 4 of 10
(5,107 Views)

Update: The same link errors happen if we try to compile one of the VS2005 Example files in 

C:\Users\Public\Documents\Naional Instruments\NI-VISA\Examples\C\Gpib
0 Kudos
Message 5 of 10
(5,101 Views)

Some further progress. We are able to build a console app with the given IEEE488.2 example code and the command line above (which I called B.S.--my apologies). This works as a C as well as C++ project. The commands work and show up in the Trace app. 

 

However, when we try to integrate the same code into our tester environment, it still does not work...

0 Kudos
Message 6 of 10
(5,072 Views)

OK, we now understand why the program does not run in our tester environment, and that has nothing to do with NI, the drivers, or the compiler. 

 

Basically we learned that:

- there are different ways to send commands via GPIB (IEEE488.1, IEEE488.2, and VISA). Each way needs different interface files, include directories, and library files. This is very confusing, and, IMHO, poorly documented by NI

- a lot of the code examples are outdated and/or do not work

 

0 Kudos
Message 7 of 10
(5,062 Views)

To clarify: We still cannot link successfully an example with the VISA library from C++. We still get the link errors, in spite of having the appropriate reference directories and library files. 

 

A version using ieee488.2 works just fine, but not VISA. 

 

Still looking for a solution...

0 Kudos
Message 8 of 10
(5,045 Views)

OK, we have found a solution with the help of NI support. Kudos to Brian P. for making the effort to resolve this issue. 

 

It turns out that there are three places in Project>Properties where the appropriate directories/libraries need to be supplied to the compiler:

 

1. VC++ Directories>Include Directories: add the /include directory

2. VC++ Directories>Library Directories: add the /msc directory

3. Linker>Input>Additional Dependencies: add the .lib file in the /msc directory

 

The include and msc directories are found in different hierarchies depending on whether the project is 32 or 64 bit.

 

For 64 bit: C:/Program files/IVI Foundation/...

For 32 bit: C:/Program files (x86)/IVI Foundation/...

 

I hope this helps somebody.

 

I also hope that NI will updated their documentation and especially their programming examples to more up-to-date versions of Visual Studio projects. 

0 Kudos
Message 9 of 10
(4,976 Views)

For all of your C/C++ to GPIB needs:

 

Please examine www.ansi-c.com.

 

Web page has customer support contact. 

0 Kudos
Message 10 of 10
(293 Views)