Spectrum Master Programming Manual : IQ Waveform Capture : C++ Code (IQ_SCPI.cpp)
 
C++ Code (IQ_SCPI.cpp)
This script initiates a capture but does not allow for the data to be saved on the instrument. Instead, data is saved directly to the PC.
 
/*
Anritsu Company
Example SCPI-based program to initiate IQ capture and receive data on PC
 
Data captured using this program will be written to binary output, data.bin.
Data points are packed in an alternating fashion: I0, Q0, I1, Q1...
Each data point is 3 bytes in size (I0 = 3 bytes, Q0 = 3 bytes).
*/
 
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "visa.h"
#include <windows.h>
 
#define BUFFER_SIZE 0x80000 /* maximum size of scpi response buffer */
 
int _tmain(int argc, _TCHAR* argv[])
{
ViStatus status; /* For checking errors */
ViSession defaultRM, instr; /* Communication channels */
ViUInt32 retCount; /* Return count from string I/O */
char *pAddress;
char *pFileName;
FILE * fd;
 
int i;
ViChar cmdBuf[256];
ViChar statStr[256];
char dataBuf[BUFFER_SIZE];
 
// the VISA address of the instrument
pAddress = "TCPIP::10.0.0.10::inst0::INSTR";
// the output filename of the data
pFileName = "data.bin";
 
/* Begin by initializing the system*/
status = viOpenDefaultRM(&defaultRM);
if (status != VI_SUCCESS)
{
/* Error Initializing VISA...exiting*/
printf("Can't initialize VISA\n");
return -1;
}
 
/* Open communication with TCP/IP device at xxx.xxx.xxx.xxx*/
/* NOTE: For simplicity, we will not show error checking*/
/* TODO: Add error handling. */
status = viOpen(defaultRM, pAddress, VI_NULL, VI_NULL, &instr);
if (status != VI_SUCCESS)
{
printf("viOpen failed %d\n",status);
return -1;
}
 
/* Set the timeout for message-based communication*/
/* TODO: Add error handling. */
status = viSetAttribute(instr, VI_ATTR_TMO_VALUE,30000);
 
fd = fopen(pFileName,"wb");
if (fd == NULL)
{
printf("Can't open the file\n");
status = viClose(instr);
status = viClose(defaultRM);
return -1;
}
 
/* initiate capture */
sprintf(&cmdBuf[0],"MEASure:IQ:CAPture REMote\n");
status = viWrite(instr, (unsigned char *)&cmdBuf[0],(ViUInt32)strlen(cmdBuf), &retCount);
/* printf("%d, %d, %d\n",i,(numBytes - bytesLeft),retCount); */
if (status != VI_SUCCESS)
{
printf("viWrite 1 failed 0x%x\n",status);
viStatusDesc(instr,status,statStr);
printf("statStr 1 = %s\n",statStr);
fclose(fd);
/* Close down the system */
status = viClose(instr);
status = viClose(defaultRM);
return -1;
}
 
i = 0;
while (1)
{
/* initiate capture */
sprintf(&cmdBuf[0],"MEASure:IQ:GET?\n");
status = viWrite(instr, (unsigned char *)&cmdBuf[0],(ViUInt32)strlen(cmdBuf), &retCount);
if (status != VI_SUCCESS)
{
printf("viWrite 1 failed 0x%x\n",status);
viStatusDesc(instr,status,statStr);
printf("statStr 1 = %s\n",statStr);
break;
}
status = viRead(instr,(unsigned char *)dataBuf,BUFFER_SIZE,&retCount);
if (status != VI_SUCCESS)
{
printf("viRead 1 failed 0x%x\n",status);
viStatusDesc(instr,status,statStr);
printf("statStr 1 = %s\n",statStr);
break;
}
 
if(!strncmp(dataBuf, "#10", strlen("#10")))
{
break;
}
else
{
/* append data to end of file */
/* data comes back in form #xyyyyyyy
where yyyyyy indicates the size of the buffer and x indicates the number of digits in the buffer size
the data immediately follows the buffer size */
char tempBuf[16];
int numDigits,size;
strncpy(tempBuf,&dataBuf[1],1);
tempBuf[1] = '\0';
sscanf(tempBuf,"%d\n",&numDigits);
strncpy(tempBuf,&dataBuf[2],numDigits);
tempBuf[numDigits] = '\0';
sscanf(tempBuf,"%d\n",&size);
fwrite(&dataBuf[2+numDigits],size,1,fd);
}
printf("i=%d,retCount=%d\n",i++,retCount);
}
 
fclose(fd);
/* Close down the system */
/* TODO: Add error handling. */
status = viClose(instr);
status = viClose(defaultRM);
 
return 0;
}