Rubidium™ MG362x1A SeriesLow-Noise RF/Microwave SignalGenerators : General GPIB, Ethernet Information : Sending SCPI Commands with NI-VISA
 
Sending SCPI Commands with NI-VISA
SCPI commands can be sent to the instrument through any Virtual Instrument Software Architecture (VISA) controller. VISA is a commonly used API in the Test and Measurement industry for communicating with instruments from a PC. The physical connection between the PC and the instrument can be Ethernet or GPIB. NI‑VISA is the National Instruments implementation of the VISA I/O standard. Information and downloads are available at http://www.ni.com/visa/.
The following example describes the verification that a VISA controller can interact with the instrument. The images shown and the instructions for your instrument and software may differ from the example shown here, so refer to the NI user instructions for more details.
1. On the PC, run VISA Interactive Control and double‑click on the instrument listed in the GPIB Instrument Resources section as shown in Figure: VISA Interactive Control.
VISA Interactive Control
2. Select the Write tab and execute the default *IDN? Write by clicking the Write button.
3. Click the Read tab. If the PC is connected to the instrument, the command returns the following information from the Buffer: manufacturer name (“Anritsu”), model number, serial number, and firmware package number as shown in Figure: VISA Interactive Line Control Tab.
VISA Interactive Line Control Tab
Communicate Over GPIB Example
This code demonstrates synchronous read and write commands to a GPIB instrument using VISA.
/********************************************************************/
/* Read and Write to an Instrument Example */
/* */
/* This code demonstrates synchronous read and write commands to a */
/* GPIB instrument using VISA. */
/* */
/* The general flow of the code is */
/* Open Resource Manager */
/* Open VISA Session to an Instrument */
/* Write the Identification Query Using viWrite */
/* Try to Read a Response With viRead */
/* Close the VISA Session */
/********************************************************************/
 
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
/* Functions like strcpy are technically not secure because they do */
/* not contain a 'length'. But we disable this warning for the VISA */
/* examples since we never copy more than the actual buffer size. */
#define _CRT_SECURE_NO_DEPRECATE
#endif
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
#include "visa.h"
 
static ViSession defaultRM;
static ViSession instr;
static ViStatus status;
static ViUInt32 retCount;
static ViUInt32 writeCount;
static unsigned char buffer[100];
static char stringinput[512];
 
/*
* In every source code or header file that you use it is necessary to prototype your VISA variables at the beginning of the file. You need to declare the VISA session, VISA integers, VISA strings, VISA pointers, and VISA floating variables.
* Remember that if you are prototyping variables that are to be used as part of the VISA session that need this prototyping. As an example, above retCount has been prototyped as a static variable to this particular module. It is an integer of bit length 32. If you are uncertain how to declare your VISA prototypes refer to the VISA help under the Section titled Type Assignments Table. The VISA help is located in your NI-VISA directory or folder.
*/
 
int main(void)
{
/*
* First we must call viOpenDefaultRM to get the resource manager
* handle. We will store this handle in defaultRM.
*/
status=viOpenDefaultRM (&defaultRM);
if (status < VI_SUCCESS)
{
printf("Could not open a session to the VISA Resource Manager!\n");
exit (EXIT_FAILURE);
}
/*
* Now we will open a VISA session to a device at Primary Address 5.
* You can use any address for your instrument. In this example we are
* using GPIB Primary Address 5.
*
* We must use the handle from viOpenDefaultRM and we must
* also use a string that indicates which instrument to open. This
* is called the instrument descriptor. The format for this string
* can be found in the NI-VISA User Manual.
* After opening a session to the device, we will get a handle to
* the instrument which we will use in later VISA functions.
* The two parameters in this function which are left blank are
* reserved for future functionality. These two parameters are
* given the value VI_NULL.
*
*/
status = viOpen (defaultRM, "GPIB0::5::INSTR", VI_NULL, VI_NULL, &instr);
if (status < VI_SUCCESS)
{
printf ("Cannot open a session to the device.\n");
goto Close;
}
 
/*
* Set timeout value to 5000 milliseconds (5 seconds).
*/
status = viSetAttribute (instr, VI_ATTR_TMO_VALUE, 5000);
 
/*
* At this point we now have a session open to the instrument
* We can use this session handle to write
* an ASCII command to the instrument. We will use the viWrite function
* to send the string "*IDN?", asking for the device's identification.
*/
strcpy(stringinput,"*IDN?");
status = viWrite (instr, (ViBuf)stringinput, (ViUInt32)strlen(stringinput), &writeCount);
if (status < VI_SUCCESS)
{
printf("Error writing to the device\n");
goto Close;
}
/*
* Now we will attempt to read back a response from the device to
* the identification query that was sent. We will use the viRead
* function to acquire the data. We will try to read back 100 bytes.
* After the data has been read the response is displayed.
*/
status = viRead (instr, buffer, 100, &retCount);
if (status < VI_SUCCESS)
{
printf("Error reading a response from the device\n");
}
else
{
printf("Data read: %*s\n",retCount,buffer);
}
 
 
/*
* Now we will close the session to the instrument using
* viClose. This operation frees all system resources.
*/
Close:
printf("Closing Sessions\nHit enter to continue.");
fflush(stdin);
getchar();
status = viClose(instr);
status = viClose(defaultRM);
 
return 0;
}
 
Communicate Over Sockets Example
This code demonstrates synchronous read and write commands to a Socket instrument using VISA.
 
/********************************************************************/
/* Read and Write to an Instrument Example */
/* */
/* This code demonstrates synchronous read and write commands to a */
/* Socket instrument using VISA. */
/* */
/* The general flow of the code is */
/* Open Resource Manager */
/* Open VISA Session to an Instrument */
/* Write the Identification Query Using viWrite */
/* Try to Read a Response With viRead */
/* Close the VISA Session */
/********************************************************************/
 
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
/* Functions like strcpy are technically not secure because they do */
/* not contain a 'length'. But we disable this warning for the VISA */
/* examples since we never copy more than the actual buffer size. */
#define _CRT_SECURE_NO_DEPRECATE
#endif
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
#include "visa.h"
 
static ViSession defaultRM;
static ViSession instr;
static ViStatus status;
static ViUInt32 retCount;
static ViUInt32 writeCount;
static unsigned char buffer[100];
static char stringinput[512];
 
/*
* In every source code or header file that you use it is necessary to prototype
* your VISA variables at the beginning of the file. You need to declare the VISA
* session, VISA integers, VISA strings, VISA pointers, and VISA floating variables.
* Remember that if you are prototyping variables that are to be used as part of the
* VISA session that need this prototyping. As an example, above retCount has been
* prototyped as a static variable to this particular module. It is an integer of
* bit length 32. If you are uncertain how to declare your VISA prototypes refer
* to the VISA help under the Section titled Type Assignments Table. The VISA
* help is located in your NI-VISA directory or folder.
*/
 
int main(void)
{
/*
* First we must call viOpenDefaultRM to get the resource manager
* handle. We will store this handle in defaultRM.
*/
status=viOpenDefaultRM (&defaultRM);
if (status < VI_SUCCESS)
{
printf("Could not open a session to the VISA Resource Manager!\n");
exit (EXIT_FAILURE);
}
/*
* Now we will open a VISA session to a Socket device.
* There are two examples, one using a TCP-IP address (commented out),
* and one using an mDNS hostname. Your address and hostname will be different.
*
* We must use the handle from viOpenDefaultRM and we must
* also use a string that indicates which instrument to open. This
* is called the instrument descriptor. The format for this string
* can be found in the NI-VISA User Manual.
* After opening a session to the device, we will get a handle to
* the instrument which we will use in later VISA functions.
* The two parameters in this function which are left blank are
* reserved for future functionality. These two parameters are
* given the value VI_NULL.
*
*/
//status = viOpen(defaultRM, "TCPIP0::172.26.201.114::9001::SOCKET", VI_NULL, VI_NULL, &instr);
status = viOpen(defaultRM, "TCPIP0::MG36241A-2133003.local::9001::SOCKET", VI_NULL, VI_NULL, &instr);
if (status < VI_SUCCESS)
{
printf ("Cannot open a session to the device.\n");
goto Close;
}
/*
* Set timeout value to 5000 milliseconds (5 seconds).
*/
status = viSetAttribute (instr, VI_ATTR_TMO_VALUE, 5000);
 
/*
* Set the termination character to '\n' for reads, and enable the termination character.
* This is needed for raw socket devices as there is no other way to determine termination.
*/
status = viSetAttribute(instr, VI_ATTR_TERMCHAR, '\n');
status = viSetAttribute(instr, VI_ATTR_TERMCHAR_EN, VI_STATE_ASSERTED);
 
/*
* At this point we now have a session open to the instrument
* We can use this session handle to write
* an ASCII command to the instrument. We will use the viWrite function
* to send the string "*IDN?\n", asking for the device's identification.
* The termination character '\n' is needed for raw socket devices.
*/
strcpy(stringinput,"*IDN?\n");
status = viWrite (instr, (ViBuf)stringinput, (ViUInt32)strlen(stringinput), &writeCount);
if (status < VI_SUCCESS)
{
printf("Error writing to the device\n");
goto Close;
}
/*
* Now we will attempt to read back a response from the device to
* the identification query that was sent. We will use the viRead
* function to acquire the data. We will try to read back 100 bytes or
* until the termination character is received.
* After the data has been read the response is displayed.
*/
status = viRead (instr, buffer, 100, &retCount);
if (status < VI_SUCCESS)
{
printf("Error reading a response from the device\n");
}
else
{
printf("Data read: %*s\n",retCount,buffer);
}
 
 
/*
* Now we will close the session to the instrument using
* viClose. This operation frees all system resources.
*/
Close:
printf("Closing Sessions\nHit enter to continue.");
fflush(stdin);
getchar();
status = viClose(instr);
status = viClose(defaultRM);
 
return 0;
}