// 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>

#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream
#include <fstream>
#include <windows.h>
///
///	G7M SCPI programming sample
///
int _tmain(int argc, _TCHAR* argv[])
{
	char address[128], buff[128];
	int status;
	ViSession rm, rsrc;
	ViRsrc deviceAddress;
	char buffer[255];
	ViUInt32 retCnt;

	using namespace std;

	// Create resource manager
	viOpenDefaultRM(&rm);
	// Set locale for multibyte charsets
	setlocale(LC_ALL, "Russian");
	// Set device address
	std::cout << "Enter device address: "; scanf("%s", buff);
	// Create address in SCPI-specific format
	sprintf(address, "TCPIP::%s::5025::SOCKET", buff);
	deviceAddress = address;

	// Open connection
	if ( (status = viOpen(rm, deviceAddress, VI_EXCLUSIVE_LOCK, 5000, &rsrc)) == VI_SUCCESS )
	{
		std::cout << "Connected to " << (char*)deviceAddress << std::endl;
		// Get identification string of device
		viQueryf(rsrc, "*IDN?\n", "%T", buff);
		std::cout << "IDN: " << buff;

		viPrintf(rsrc, "*RST\n");
		Sleep(500);
		// Waiting for device answer
		viQueryf(rsrc, "*OPC?\n", "%T", buff);

		// Set the device settings
		viPrintf(rsrc, "SOURce:FREQuency:MODE LIST\n");
		viPrintf(rsrc, "SOURce:POWer:MODE LIST\n");

		// Loading a frequency list
		strcpy(buffer, "SOURce:LIST:FREQuency 5500000000, 5600000000, 5700000000, 5800000000, 5900000000\n");
		status = viWrite(rsrc, (ViBuf)&buffer[0], strlen(buffer), &retCnt);
		// Waiting for device answer
		viQueryf(rsrc, "*OPC?\n", "%T", buff);

		// Loading a list sweep file to device
		strcpy(buffer, "SOURce:LIST:POWer 0, 5, 10, 5, 0\n");
		status = viWrite(rsrc, (ViBuf)&buffer[0], strlen(buffer), &retCnt);
		// Waiting for end of download
		viQueryf(rsrc, "*OPC?\n", "%T", buff);

		// Set the List mode settings
		strcpy(buffer, "SOURce:LIST:DWELl 0.01, 0.5, 1, 0.5, 0.01\n");
		// Loading dwell value
		status = viWrite(rsrc, (ViBuf)&buffer[0], strlen(buffer), &retCnt);
		// Waiting for end of download
		viQueryf(rsrc, "*OPC?\n", "%T", buff);

		// Loading enable value
		strcpy(buffer, "SOURce:LIST:SWITch 1,1,1,1,1\n");
		status = viWrite(rsrc, (ViBuf)&buffer[0], strlen(buffer), &retCnt);
		viQueryf(rsrc, "*OPC?\n", "%T", buff);
		// Loading marker value
		strcpy(buffer, "SOURce:LIST:MARKer 1,1,1,1,1\n");
		status = viWrite(rsrc, (ViBuf)&buffer[0], strlen(buffer), &retCnt);
		viQueryf(rsrc, "*OPC?\n", "%T", buff);


		viPrintf(rsrc, "SOURce:LIST:MODE AUTO\n");
		viPrintf(rsrc, "TRIGger:SEQuence:Source IMMediate\n");
		viPrintf(rsrc, "INITiate:CONTinuous ON\n");
		viPrintf(rsrc, "INITiate:IMMediate\n");

		// Turn ON the device output power
		viPrintf(rsrc, "OUTPut:STATe ON\n");

		viQueryf(rsrc, "SYSTem:ERRor?\n", "%T", buff);
		std::cout << "SYSTem:ERRor? " << buff;
		// Waiting for user action
		std::cout << "Press any key to close connection..." << std::endl;
		getch();
		viPrintf(rsrc, "ABORt\n");
		// Close session
		viClose(rsrc);
	}
	else
	{
		viStatusDesc(rsrc, status, buff);
		std::cout << "Connection error: " << buff << std::endl;
	}
	// Close resource manager
	viClose(rm);

	// Waiting for user action
	std::cout << "Press any key to exit..." << std::endl;
	getch();
	
	return 0;
}
