demo3/visa-scpi-example/File.cpp

154 lines
4.9 KiB
C++
Raw Normal View History

// C++ script created by FSW: 04:12:2024 14:23:36
#include <string>
#include <iostream>
#include <visa.h>
// forward declarations
static bool write_command(const ViSession& handle, const std::string& command);
static bool write_query(const ViSession& handle, const std::string& query, ViPBuf result_buffer, const ViUInt32 buffer_size, ViPUInt32 read_count = nullptr);
static bool process_system_error(const ViSession& handle);
static unsigned char get_esr(const ViSession& handle);
static ViSession g_analyzer;
// assign your own variables
static ViByte g_result[1000000];
static bool write_command( const ViSession& handle, const std::string& command )
{
ViStatus status = viWrite(handle, (ViBuf)command.c_str(), command.length(), VI_NULL);
// return true if successful
return ((status >= VI_SUCCESS) && process_system_error(handle));
}
static bool write_query( const ViSession& handle, const std::string& query, ViPBuf result_buffer, const ViUInt32 buffer_size, ViPUInt32 read_count )
{
ViStatus status = viWrite(handle, (ViBuf)query.c_str(), query.length(), VI_NULL);
if (status >= VI_SUCCESS)
{
status = viRead(handle, result_buffer, buffer_size, read_count);
}
// return true if successful
return ((status >= VI_SUCCESS) && process_system_error(handle));
}
static bool process_system_error(const ViSession& handle)
{
bool success = true;
const unsigned char EsrErrorMask = 0x3C;
if ((get_esr(handle) & EsrErrorMask) != 0)
{
char err_buf[256];
ViUInt32 readCount;
viWrite(handle, (ViBuf)":SYST:ERR?", 10, VI_NULL);
viRead(handle, (ViPBuf)err_buf, sizeof(err_buf), &readCount);
err_buf[readCount] = '\0';
std::cout << err_buf << std::endl;
viWrite(handle, (ViBuf)"*CLS", 4, VI_NULL);
success = false;
}
return success;
}
static unsigned char get_esr(const ViSession& handle)
{
char esr_buf[16];
ViUInt32 readCount;
viWrite(handle, (ViBuf)"*ESR?", 5, VI_NULL);
viRead(handle, (ViPBuf)esr_buf, sizeof(esr_buf), &readCount);
esr_buf[readCount] = '\0';
return (static_cast<unsigned char>(std::stoi(std::string(esr_buf))));
}
int main ()
{
ViSession defaultRM;
bool success = true;
// create a VISA session and return a handle to it
viOpenDefaultRM(&defaultRM);
// create a VISA session to the connected device and return a handle to it
viOpen(defaultRM, (ViRsrc)"TCPIP::169.254.177.77::inst0::INSTR", VI_NULL, VI_NULL, &g_analyzer);
success = write_command( g_analyzer, "*RST" );
success = write_command( g_analyzer, "*CLS" );
success = write_command( g_analyzer, ":SYST:DISP:UPD ON" );
success = write_command( g_analyzer, ":INIT:CONT OFF" );
success = write_command( g_analyzer, ":SENS:FREQ:STAR 1000000000" );
success = write_command( g_analyzer, ":SENS:FREQ:STOP 2000000000" );
success = write_command( g_analyzer, ":DISP:WIND:TRAC:Y:SCAL:RLEV -30" );
success = write_command( g_analyzer, ":DISP:WIND:TRAC:Y:SCAL:RLEV 30" );
success = write_command( g_analyzer, ":SENS:BAND:RES:AUTO OFF" );
success = write_command( g_analyzer, ":SENS:BAND:RES:AUTO ON" );
success = write_command( g_analyzer, ":SENS:BAND:VID:AUTO OFF" );
success = write_command( g_analyzer, ":SENS:BAND:VID:AUTO ON" );
success = write_command( g_analyzer, ":SENS:FREQ:MODE SWE" );
success = write_command( g_analyzer, ":DISP:WIND1:SIZE LARG" );
success = write_command( g_analyzer, ":CALC1:MARK1:FUNC:STR:STAT ON" );
success = write_command( g_analyzer, ":CALC1:MARK1:FUNC:STR:STAT OFF" );
success = write_command( g_analyzer, ":CALC:MARK:FUNC:POW:SEL CN" );
success = write_command( g_analyzer, ":SENS:POW:ACH:BAND:CHAN 300000" );
success = write_command( g_analyzer, ":SENS:POW:ACH:PRES CN" );
success = write_command( g_analyzer, ":CALC:MARK1:STAT ON" );
success = write_command( g_analyzer, ":CALC:MARK1:X 1200000000" );
success = write_command( g_analyzer, ":CALC:MARK:FUNC:POW:SEL CN" );
success = write_command( g_analyzer, ":SENS:POW:ACH:PRES CN" );
success = write_command( g_analyzer, ":SENS:POW:ACH:PRES CN" );
success = write_command( g_analyzer, ":CALC:MARK:FUNC:POW:SEL CN0" );
success = write_command( g_analyzer, ":CALC:MARK:FUNC:POW:SEL CN" );
success = write_command( g_analyzer, ":SENS:FREQ:SPAN 1000000" );
success = write_command( g_analyzer, ":SENS:FREQ:SPAN 1000000000" );
success = write_command( g_analyzer, ":CALC:MARK1:STAT ON" );
success = write_command( g_analyzer, ":CALC:MARK1:X 1200000000" );
success = write_command( g_analyzer, ":CALC:DELT2:STAT ON" );
success = write_command( g_analyzer, ":CALC:DELT2:X 1600000000" );
success = write_command( g_analyzer, ":INIT:SPUR" );
success = write_command( g_analyzer, ":MMEM:NAME 'C:\R_S\Instr\user\FSW_ScreenShot_2024-12-04_14-22-12.PNG'" );
success = write_command( g_analyzer, ":HCOP:IMM1" );
viClose(g_analyzer);
viClose(defaultRM);
return 0;
}