Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Basic SCPI and VISA question

Solved!
Go to solution

Hi, i'm very new to SCPI and VISA and am trying to do a simple program on C++. The code is below:

 

/*
* File: main.cpp
* Author: greg
*
* Created on October 17, 2012, 9:56 AM
*/

#include "visa.h"
#include "rsspecan.h"
#include <stdio.h>
int main (void) {
ViSession defaultRM, vi;
char buf [256] = {0};
/* Open session to GPIB device at address 22 */
viOpenDefaultRM(&defaultRM);
viOpen(defaultRM, "GPIB0::22::INSTR",VI_NULL,VI_NULL,&vi);
/* Initialize device */
viPrintf(vi, "*RST\n");
/* Send an *IDN? string to the device */
viPrintf(vi, "*IDN?\n");
return 0;
}

 

and here are the error messages I receive:

 

main.cpp:16:57: warning: deprecated conversion from string constant to ‘ViRsrc {aka char*}’ [-Wwrite-strings]

main.cpp:18:22: warning: deprecated conversion from string constant to ‘ViString {aka char*}’ [-Wwrite-strings]

main.cpp:20:23: warning: deprecated conversion from string constant to ‘ViString {aka char*}’ [-Wwrite-strings]

/tmp/ccOCaxYK.o: In function `main': main.cpp:(.text 0x39):

undefined reference to `viOpenDefaultRM' main.cpp:(.text 0x65):

undefined reference to `viOpen' main.cpp:(.text 0x79):

undefined reference to `viPrintf' main.cpp:(.text 0x8d):

undefined reference to `viPrintf'

collect2: ld returned 1 exit status

make: ***[SCPI] Error 1 

 

If someone could explain what I am doing incorrectly, it would be very helpful. I'm compiling this on linux in C++ using the g++ compiler and a makefile.

 

Thanks

0 Kudos
Message 1 of 17
(14,530 Views)

Hi Greg,

 

After looking through your code it seems that you are doing everything correctly there. Based off your error it seems it is looking for functions that are not included, but it also looks like you have included everything you need to. So I was hoping you could let me know what version of VISA you have installed and also try modifying the code below and getting it to run. This is one of the example programs included in CVI. 

 

/********************************************************************/
/*              Read and Write to an Instrument Example             */
/*                                                                  */
/* This code demonstrates synchronous read and write commands to a  */
/* GPIB, serial or message-based VXI 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 2.
     * You can use any address for your instrument. In this example we are 
     * using GPIB Primary Address 2.
     *
     * 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.
     *
     * This example will also work for serial or VXI instruments by changing 
     * the instrument descriptor from GPIB0::2::INSTR to ASRL1::INSTR or
     * VXI0::2::INSTR depending on the necessary descriptor for your 
     * instrument.
     */
   status = viOpen (defaultRM,  "GPIB0::2::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 at
     * Primary Address 2.  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;
}

 

Patrick H | National Instruments | Software Engineer
0 Kudos
Message 2 of 17
(14,516 Views)

VISA version 5.1.2. I also tried the code I used on windows using the Dev-C++ compiler and received the same errors. I'll let you know how the code you sent runs as soon as I can.

0 Kudos
Message 3 of 17
(14,500 Views)

Hi Patrick,

 

When I ran it on windows, the deprecated conversion warnings actually stopped appearring, but i'm still getting undefined reference messages. I tried running the code you sent out and received this back

 

C:\DOCUME~1\User\LOCALS~1\Temp\cc6xbaaa.o(.text+0x13c):readwr~1.cpp: undefined reference to `viOpenDefaultRM@4'
C:\DOCUME~1\User\LOCALS~1\Temp\cc6xbaaa.o(.text+0x188):readwr~1.cpp: undefined reference to `viOpen@20'
C:\DOCUME~1\User\LOCALS~1\Temp\cc6xbaaa.o(.text+0x1c8):readwr~1.cpp: undefined reference to `viSetAttribute@12'
C:\DOCUME~1\User\LOCALS~1\Temp\cc6xbaaa.o(.text+0x20f):readwr~1.cpp: undefined reference to `viWrite@16'
C:\DOCUME~1\User\LOCALS~1\Temp\cc6xbaaa.o(.text+0x248):readwr~1.cpp: undefined reference to `viRead@16'
C:\DOCUME~1\User\LOCALS~1\Temp\cc6xbaaa.o(.text+0x2bb):readwr~1.cpp: undefined reference to `viClose@4'
C:\DOCUME~1\User\LOCALS~1\Temp\cc6xbaaa.o(.text+0x2d3):readwr~1.cpp: undefined reference to `viClose@4'

0 Kudos
Message 4 of 17
(14,495 Views)

Hi Greg,

 

From the errors you have listed it sounds like something may have gone wrong with the VISA install. All the functions listed in the error log are references included in VISA. Below is a link to our website where you can download the newest version of VISA. If you are still working on the Windows machine try VISA 5.2 and let me know if you are still having trouble.

 

http://www.ni.com/nisearch/app/main/p/bot/no/ap/tech/lang/en/pg/1/sn/catnav:du,n8:3.25.123.1640,ssna...

 

 

Patrick H | National Instruments | Software Engineer
0 Kudos
Message 5 of 17
(14,485 Views)

Hi Patrick,

 

I downloaded version 5.2 and still receive the same errors. Is there some step after download that i'm missing that causes this?

 

Thanks

0 Kudos
Message 6 of 17
(14,480 Views)

Hi Greg,

 

After downloading the files and running setup.exe there should be no additional setup necessary. A good way to verify they were installed correctly is to open Measurement and Automation Explorer and check under the software drop down to see if VISA was installed. Measurement and Automation Explorer is only available on Windows computers.

 

 

VISA.png

Patrick H | National Instruments | Software Engineer
0 Kudos
Message 7 of 17
(14,477 Views)

I checked the explorer and visa 5.2 has been downloaded in the explorer in the same as the picture shown. Could this have something to do with my visa.h file?

0 Kudos
Message 8 of 17
(14,462 Views)

Hi Greg,

 

Take a look at the knowledgebase I have linked here let me know if you see the visa.h header file. If you can see it, try adjusting the #include and direct it to this file.

 

http://digital.ni.com/public.nsf/allkb/6AE70070CF3B2ED786256B210059C4E1?OpenDocument

 

 

Patrick H | National Instruments | Software Engineer
0 Kudos
Message 9 of 17
(14,455 Views)

Hi Patrick,

 

I see the visa file in the location directed and included by bringing it into the folder with the code I was using and by including it using its path. Both times I received the same errors. I looked through the visa.h files and the functions that it says aren't defined seem pretty clearly defined in the visa.h file. Also, the measurement and automation explorer picture is below

0 Kudos
Message 10 of 17
(14,441 Views)