LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Problelm with multi times use of viQueryf?

Solved!
Go to solution

My project  is used to continuously save three GIF pictures from a spectrum analyzer. I can't save the sencond pictures and I am confused. Can anybody help me to figure it out? The relavant files are in the accessory.

 

my codes are as follows:

 

#include <windows.h>
#include <utility.h>
#include <ansi_c.h>
#include <visa.h>
#include <formatio.h>
#include <cvirte.h>

int hDefaultRM;
int hSpectrumInstr;
int status;
char *ptr;

int SaveSpectrumAnalyzerScreen(int hInstru, char * fileName)    
{
 ViInt32 totalPoints;
 ViInt8  picArray[50000] = {0}; 
 int  hFile;
 
 viPrintf(hInstru, "*OPC\n");   
 viSetAttribute (hInstru, VI_ATTR_TMO_VALUE, 6*1000000);       //设置VISA的属性(延迟时间)        
 viPrintf(hInstru, ":MMEM:STOR:SCR 'C:PICTURE.GIF'\n");
 viQueryf(hInstru, ":MMEM:DATA? 'C:PICTURE.GIF'\n", "%#b", &totalPoints, picArray);
 viQueryf(hInstru, ":MMEM:DATA? 'C:PICTURE.GIF'\n", "%#y", &totalPoints, picArray); 

// viPrintf(hInstru, ":MMEM:DATA? 'C:PICTURE.GIF'\n"); 
// viScanf (hInstru, "%#b", picArray);
 viPrintf(hInstru, ":MMEM:DEL 'C:PICTURE.GIF'\n"); 
 
 hFile = OpenFile (fileName, VAL_READ_WRITE, VAL_TRUNCATE, VAL_BINARY);
 WriteFile (hFile, picArray, 50000);
 CloseFile (hFile);
 Sleep(1000);
 
 return 0;
}

int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1;    /* out of memory */
 
 status = viOpenDefaultRM(&hDefaultRM);
 status = viOpen (hDefaultRM, "GPIB0::11::INSTR", VI_NULL, VI_NULL, &hSpectrumInstr);

 SaveSpectrumAnalyzerScreen(hSpectrumInstr, "C:\\Users\\Administrator.LC-PC\\Desktop\\pic\\01.gif");
 SaveSpectrumAnalyzerScreen(hSpectrumInstr, "C:\\Users\\Administrator.LC-PC\\Desktop\\pic\\02.gif");   
 SaveSpectrumAnalyzerScreen(hSpectrumInstr, "C:\\Users\\Administrator.LC-PC\\Desktop\\pic\\03.gif");

 viClose(hDefaultRM);
 CloseCVIRTE (); 
 return 0;
}

 

Error1:

The first time to save picture from the spectrum analyzer it surely saved the correct picture in the specified folder. But the second time to save picture the program stoped at the line “viQueryf(hInstru, ":MMEM:DATA? 'C:PICTURE.GIF'\n", "%#b", &totalPoints, picArray); “ and for a long time it did not response. When NI I/O Trace was applied to the program, the second viQueryf was interpreted directly to viRead without viWrite. It must be wrong but how this error happene?

 

Error 2:

When I replace viQueryf with viPrintf and viScanf it can get the first picture correctly, but the second time to save picture viScanf got wrong content, the content in viScanf buffer is not like the correct one. but NI I/O Trace didn’t have any wrong.

0 Kudos
Message 1 of 3
(4,565 Views)

I think you are not using the OPC (operation complete) query correctly.

It should be *OPC? (with a question mark at the end) and you need to read the response from the instrument and then continue with the operation. Otherwise you cannot be sure the instrument is ready to continue.

S. Eren BALCI
IMESTEK
0 Kudos
Message 2 of 3
(4,552 Views)
Solution
Accepted by topic author 中科院电子所

I am not sure that if the error is relevant with the incorrecct use of "OPC\n". But the error disappeared when I flush the read buffer every time I viQuery the instrument.  The reason must be query the instrument in format of  "%#b" can not read out all of datas, the remain data lead to the second query to the instrument. Thanks for your reply!

 

Message 3 of 3
(4,549 Views)