// ScpiExample.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "visa.h"
#include <conio.h>
#include <iostream>
#include <stdio.h>
#include <locale>

///
///	PLG SCPI programming sample
///
int _tmain(int argc, _TCHAR* argv[])
{	
	char address[128];
	char buff[64];
	int status;

	ViSession rm, rsrc;
	ViRsrc deviceAddress;

	viOpenDefaultRM(&rm);	        //	Open default resource manager
	setlocale(LC_ALL, "Russian");	//	Cyrillic char support
	
	std::cout << "Enter serial number: ";
	scanf("%s",buff);

	sprintf(address, "USB0::0x2b70::0x0040::%s::0::INSTR", buff);
	deviceAddress = address;

	// Open connection
	if ( (status = viOpen(rm, deviceAddress, VI_EXCLUSIVE_LOCK, 5000, &rsrc)) == VI_SUCCESS )//Открытие соединения с прибором. Первичная инициализация.
	{
		std::cout << "Connect to " << (char*)deviceAddress << std::endl;

		viPrintf(rsrc, "*RST\n");

		std::cout << "IDN: ";
		viQueryf(rsrc, "*IDN?\n", "%T", buff);
		std::cout << buff;

		std::cout << "setting frequency to 1 GHz" << std::endl;
		viPrintf(rsrc, "FREQ 1 GHz\n");
		
		std::cout << "setting power to 1 dBm" << std::endl;
		viPrintf(rsrc, "POW 1\n");

		std::cout << "power on" << std::endl;
		viPrintf(rsrc, "OUTPut:STATe ON\n");

		viQueryf(rsrc,"SYSTem:ERRor?\n", "%T", buff);
		std::cout << buff;

		std::cout << "press any key to continue..." << std::endl;
		getch();	//	Wait for user action

		std::cout << "switch LF generator and setting shape SINE" << std::endl;
		viPrintf(rsrc, "LFO:FUNC:SHAP SIN\n");
	
		std::cout << "setting frequency to 9 kHz" << std::endl;
		viPrintf(rsrc, "LFO:FUNC:FREQ 9 kHz\n");
		
		std::cout << "setting amplitude 3 V" << std::endl;
		viPrintf(rsrc, "LFO:AMP 3\n");
		
		viPrintf(rsrc, "*RST\n");
		viClose(rsrc);	// Close session
	}
	else
	{
		std::cout << "Connection error!" << std::endl;
	}
	
	viClose(rm);	//	// Close resource manager
	
	std::cout << "press any key to exit..." << std::endl;
	getch();	//	Wait for user action
	
	return 0;
}

