Instrument Control (GPIB, Serial, VISA, IVI)

Showing results for 
Search instead for 
Did you mean: 

Trying to read data using VISA viScanf()

I am using VISA to control a network analyzer through Visual Studio and am running into issues with getting the data using the viScanf(). 


The code below is a snippet of my source code.  The programmers reference guide for the network analyzer sates that the "CALC1:MARK:REF:X?" command will return the frequency of the marker in NR3 format (IEEE floating point standard). 


I figure that since it is returning the info in floating point format that I should read it in as a float.  Am I using the viScanf() correctly in this situation? 


I initialize the "data" variable to 6 just to see if anything changes, which it doesn't.  All I want to do is get the reading of 260 MHz.


Any help is appreciated.



float data = 6;


viPrintf(instr, "FORM:BORD NORM\n");
viPrintf(instr, "FORM:DATA REAL,32\n");
viPrintf(instr, "CALC1:MARK1 ON\n");

viPrintf(instr, "CALC1:MARK1:X 260 MHz\n");
viPrintf(instr, "CALC1:MARK:REF:X?\n");
viScanf(instr, "%f", &data);







0 Kudos
Message 1 of 4

Hi Jeff

The way your trying to access the VISA gpib driver is a very old style method there
are much new methods to access VISA gpib devices.The IVI foundation , NI & others
have updated the VISA GPIB driver to make things easier but not left much of real
programming example documentation trail .

If you look here at this post it shows the names of classes inside the driver

This example is for visual basic 6 , the methodology will be different in visual C or C++ but the classes names will be similar.
In C/C++, you must tell the compiler where to find the include and library files are and make sure these paths include the
“VISA COM 3.0 Type Library”. Using the C++ statement either / or

#include "stdafx.h"
#import "C:/Program Files/IVI Foundation/VISA/VisaCom/GlobMgr.DLL" no_namespace named_guids

I have have not used visual C yet . These two link may help you also but its is mainly about using IVI drivers.

Another place to look is on your own machine if you are using a NI VISA driver. Look in this location on your machine

C:\Documents and Settings\All Users\Documents\National Instruments\NI-VISA\Examples\C

This shows you examples of the more old school way your trying to do things which may help you. This is similar
to the way your trying to write your program at the moment.

The latest NI VISA drivers are located here.
Make sure you read the help file to get the correct versions. In some of the newer NI VISA versions
they are retiring some of the older NI VISA/GPIB functions and this may create an issue for you.

If you want some good examples of programming using Microsoft Visual C from VS6 to VS2005.NET
look here . This is for measuring phase noise with a spectrum analyser . But there is a very good
example of how to program in C. This is only for ni488, which is the fore number to NI VISAGPIB

Install it and look here C:\Program Files\KE5FX\GPIB\gpiblib

According to the author of this code. The code compilable with any version of MSVC from VS6 to VS2005.NET,
including the free Microsoft Visual Studio Express package. But you use it at your own risk.

Agilent also have a lot of good stuff. See here as examples. But remember aligent also have a lot
customer drivers stuff. Which may be a good idea to stay away from if possible. It can give
code transportablity issues when using NI drivers. But its still useful to read.

I know this is a bit rambling but I have tried to give you a dump of all the resource I found in my quest to learn
instrument control using visual basic . Along the way I cam across many Visual C links also.

Good luck with your quest.

Best wishes


0 Kudos
Message 2 of 4

Hi Nathan,


It looks like you are using the viScanf() function correctly.  Are you being thrown any errors, or is the function just not returning the correct values from the instrument?  Have you tried communicating with your instrument through Measurement and Automation Explorer, in order to ensure that it is communicating properly? 




Stephanie R.

National Instruments

Stephanie R.
National Instruments
0 Kudos
Message 3 of 4



Thanks for the replies. It turns out that I was misunderstanding the format in which the data is being returned.  It actually returns an array of ASCII characters that represent a floating point number.



0 Kudos
Message 4 of 4