Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

use of the libairy visa

good morning.
 
Does anybody have an exemple of programme using the librairy visa.h . It is only to help to know how to use simply the function of the librairy.
I have to communicate with a ANRITSU spectrum analyser via RJ45. For example, if I want to change the frequency, the document provided by ANRITSU tells me to send  " [:SENSE]:FREQUENCY:START 1MHZ " . But i don't know if I write directly this command on C++, or if I have to save it into a buffer with " sprintf(buffer,"[:SENSE]:FREQUENCY:START 1MHZ ") and use the function of the visa32.lib : viWrite(.buffer....).
 
Do you have an idea. Excuse me if it is a basic question
 
thank you very much
0 Kudos
Message 1 of 33
(12,134 Views)
The simplest way to send SCPI commands from VC++ is use viPrintf().
 
By the way, I believe your ANRITSU instrument is on the Ethernet bus as you mentioned RJ45 connector.  There are two different TCPIP architecture on VISA --- VXI11 INSTR and RAW SOCKET.  I don't know which type your instrument is supporting, but the only difference is VISA address syntax when you call viOpen().  Plus, do not forget the LF termination (\n) if the instrument is RAW SOCKET, as many instruments often require an explicit termination on each given command string.
 
The following is a fragment using VISA API from VC++.
 
 
ViSession viRM;
ViSession vi;
ViStatus vs;

vs = viOpenDefaultRM( &viRM);
vs = viOpen( viRM, "TCPIP0::192.168.1.1::10001::SOCKET", VI_NO_LOCK, 0, &vi); // RAW SOCKET
//vs = viOpen( viRM, "TCPIP0::192.168.1.1::INSTR", VI_NO_LOCK, 0, &vi); //VXI11 INSTR
 
vs = viSetAttribute( vi, VI_ATTR_TERMCHAR_EN, VI_TRUE); 
vs = viSetAttribute( vi, VI_ATTR_TMO_VALUE, 3000); 
vs = viSetAttribute( vi, VI_ATTR_WR_BUF_OPER_MODE, VI_FLUSH_ON_ACCESS);
vs = viSetAttribute( vi, VI_ATTR_RD_BUF_OPER_MODE, VI_FLUSH_ON_ACCESS);

vs = viPrintf( vi, "FREQ:STAR %dMHZ\n", 1); // set frequency start at 1MHz
vs = viPrintf( vi, "MEAS:VOLT?\n"); // send a measurement query (if any applicable)
double dMeas;
vs = viScanf( vi, "%lf", &dMeas); // read the response converting to double
 
...
...
vs = viClose( vi);
vs = viClose( viRM);
 
 

このメッセージは 03-26-2007 11:54 AMに Makoto が編集しています。

Message 2 of 33
(12,110 Views)

thank you very much for your quick and detailled answer 🙂 .

The function viPrintf() is the same thing as viWrite ? What is the best function for you ?

Ah one more thing : how can I easily get the TCP IP address.

Thank you veru much for your help 

0 Kudos
Message 3 of 33
(12,106 Views)
ah sorry. I use BORLAND C++, is there any difference with VC++ , I don't think so but I would like to be sure.
Thank you
0 Kudos
Message 4 of 33
(12,104 Views)

> The function viPrintf() is the same thing as viWrite ? What is the best function for you ?

Basically they are the same, but viPrintf() is buffered IO that the VISA internally buffers the contents before sending them out.  Plus, viPrintf() is more flexible for ASCII based IOs and you can use it as if using print() with "%" identifiers, taking veriable numbers of parameters.  This is also true for viScanf() and viRead(). 

> Ah one more thing : how can I easily get the TCP

Normally, the instrument IP address can be shown on the instrument's front panel including DHCP enabling or fixed IP address.  (See the instrument manual.)  If the instrument is VXI11 based, you can also search for it from NI-MAX. 

As for Borland environment, you can use VISA in the same manner of VC++.  Mind that the visa32.lib to link is different for MSVC and Borland.  The correct visa32.lib for Borland is found in the C:\VXIpnp\WinNT\lib\bc directory.

 

このメッセージは 03-26-2007 01:04 PMに Makoto が編集しています。

Message 5 of 33
(12,096 Views)
arigatou gozaimasu. Thank you very much
I would like to know one more thing, if you have time of course.
 
 I would like to receive data from the spectrum analyser. I have already developp a programm with an old spectrum analyser, that didn't use VISA, and with a port COM. With the function "readfile(..)" I could easily receive information from the spectrum, and put it into a buffer, that I sent into a text file ".txt".
 
But with the new spectrum analyser via Ethernet RJ45 and the visa librairy, I have to use a specific function :
 
:MMEMory:STORe:TRACe <integer>,<file name>
 
But they tell me that the file name should not contain a file extension. So I don't know what kind of format it will be saved. I would like to use the data sent by the spectrum analyser, and to trace some specific curves with a programm in Matlab.
Do you know if there is a possibility to receive data into a buffer ,that a could send into a text file ? 
 
thank you very much
0 Kudos
Message 6 of 33
(12,091 Views)
> :MMEMory:STORe:TRACe <integer>,<file name>
 
I believe the <file name> parameter specifies a file name for instrument's internal storage, such as Compact Flash card or internal HDD etc..., because the instrument firmware does not know about anything about the "C" drive of your host Windows PC.  Then, the file format is up to the instrument design, therefore you should read the instrument operation manual about it. Likely you may be able or unable to specify the file format such as .TXT, .CSV, .XML, etc... Anyway, you may also be able to access the generated file inside the instrument via FTP because your instrument is connected to ethernet.
 
If your app program wants to store the data into your Windows PC as a specific format you want (such as .TXT), the app must acquire the instrument trace data via VISA viRead() for binary data or viScanf() for ascii data, then writes the text file using Win32 CreateFile/WriteFile/CloseHandle API functions.  The :MMEMory:STORe:TRACe <integer>,<file name> command apparently requires a file name to store into instrument internal storage, but I think there must be other similar instrument command that allows you to read the trace data without storing into instrument's storage.
 
Message 7 of 33
(12,083 Views)
thank you very, thanks to you, I understand better : ) .
I have an other problem. Now I know to receive data from the spectrum analyser and to put it into a buffer. I use the function : viRead(vi, (unsigned char *)buffer, 255, &retCount);
But my problem is that I have to receive 550 numbers ( the points measurement) from the spectrum analyser.
So I have to order the spectrum analyser to send me data with the format "int"  : vs = viPrintf( vi, "FORMat:DATA INTeger 32 ");
 
But what I would like to know is, if I use the function "viRead(vi, (unsigned char *)buffer, 255, &retCount);" , will it be ok ???
Is it ok if I do that way :
 
 - vs = viPrintf( vi, "FORMat:DATA INTeger 32 ");
 - I order the spectrum to send me data
 - viRead(vi, (unsigned char *)buffer, 255, &retCount);
0 Kudos
Message 8 of 33
(12,056 Views)
sorry for the smiley : 😄 = : D without the space
0 Kudos
Message 9 of 33
(12,053 Views)
viRead(vi, (unsigned char *)buffer, 255, &retCount);
 
The function is correct, but the 3rd parameter is not enough.  I think the response trace data you will receive is 550pt data block as int32 type each because you specified so.  Therefore, assuming the data format is BINARY, the response data will contain at least 2200 bytes (= 550 x sizeof(LONG)).  Therefore the viRead() 3rd param "255" is not enough, because this parameter specifies the maximum length to read.  (Mind that this parameter does not enforce this reading size, but just specifies the "maximum" length for reading.  If any termination condition is encountered, such as receiving a linefeed 0x0A character, the viRead will terminate before completing the specified length read.  )
 
Assuming the response data is BINARY (not in ASCII) formatted, there are two important issues for receiving it.
 
(1)IEEE488.2/SCPI header for <arbitrary block data>
Your instrument apparently looks like IEEE488.2 / SCPI compliant.  The IEEE488.2 std defines that binary formatted data shall have a prefix that begins with #xyyyyyyyy format.  The x is always 1 digit ascii numeric that specifies "number of digit" for the y part.  The y has multiple digit (specified by x) of decimal expression that specifies the following byte-size.  Therefore the actual read size specified for viRead() must be 10 or more bytes larger than 2200.
 
(2)Termination Character
Normally when you receive an ASCII response, it is suitable to enable the Termination Character (default is linefeed 0x0A) so that viRead() can automatically terminates whenever a termination character code was sent from the instrument.  However, when receiving a binary block, the 0x0A character is only one of data contents having no meaning of termination. Therefore you should disable Termination Character feature on viRead() for binary read.  Plus, when the instrument interface is TCPIP SOCKET (not VXI11), viRead() may have no condition that will automatically terminate.  In this case, you have to specify "exact length" of read.  To know the entire length to read, try to first 10 byes containing #xyyyyyyy... part.  You can calculate the entire length.
 
 
 

このメッセージは 03-27-2007 02:06 PMに Makoto が編集しています。

0 Kudos
Message 10 of 33
(12,050 Views)