////////////////////////////////////////////////////////////////////////////
// AO "NPF Micran" 04.10.2016
// This is an example for PLS06
// Example 2.
// PLS SCPI programming sample mode
////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"

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

///
///	PLS SCPI programming sample mode
///
int _tmain(int argc, _TCHAR* argv[])
{	
	char address[128], buff[128];
	int status;
	ViSession rm, rsrc;
	ViRsrc deviceAddress;

	viOpenDefaultRM(&rm);	        //	Create of resourse manager
	setlocale(LC_ALL, "Russian");	//	Correction Russian charset

	std::cout << "Enter PLS-06 serial number: ";
	scanf("%s", buff);
	sprintf(address, "USB0::0x2b70::0x0020::%s::0::INSTR", buff);
	deviceAddress = address;

	// Open conection
	status = viOpen(rm, deviceAddress, VI_EXCLUSIVE_LOCK, 5000, &rsrc);
	if (status != VI_SUCCESS)
	{
		viStatusDesc(rm, status, buff); // Get status description
		std::cout << "Connection error: " << buff << std::endl;
		// Close resource manager
		viClose(rm);	
		std::cout << "Press any key to exit..." << std::endl;
		getch();	// Waiting for user response
		return 0;
	}

	// Set timeout
	viSetAttribute(rsrc, VI_ATTR_TMO_VALUE, 5000);

		// Send command "Clear status"
	status = viPrintf(rsrc, "*CLS\n");

	// Send command "Reset"  (all settings to «default»)
	viPrintf(rsrc, "*RST\n");

	// Read ID device information
	std::cout << "IDN:";
	viQueryf(rsrc, "*IDN?\n", "%T", buff);
	std::cout << buff;
	
	// Set to Sample mode
	viPrintf(rsrc, "SENSe:DETector:FUNCtion SAMPLe\n");

	// Set frequency
	viPrintf(rsrc, "SENSe:FREQuency 50 MHz\n");

	// Set high resolution
	viPrintf(rsrc, "SENSe:TRACe:RESolution HRESolution\n");
	
	// Set internal trigger
	viPrintf(rsrc, "TRIGger:SEQuence:SOURce INTernal\n");

	// Set trigger level
	viPrintf(rsrc, "TRIGger:SEQuence:LEVel -62.4\n");
	
	// Set continuous measurements
	viPrintf(rsrc, "INIT:CONTinuous ON\n");

	// Set measurement time
	viPrintf(rsrc, "SENSe:TRACe:TIME 0.001\n");	
	
	// Read data from device
	viPrintf(rsrc, "TRACe:DATA? HRES\n");
	
	// Parse IEEE 488.2 binary block	
	ViUInt32 retCnt = 0;
	int bitSize = 0;
	char head[32];
	// Read head (#n - number of bytes of size record)
	if (viRead(rsrc, (ViPBuf)buff, 2, &retCnt) < 0) 
		return 0;	
	memcpy(&head[0], &buff, retCnt);
	bitSize = retCnt;
	buff[retCnt] = '\0';
	int numBytes = atoi(buff+1);
	if (buff[0]!='#' || numBytes == 0)
		return 0;
	// Read data length
	if (viRead(rsrc, (ViPBuf)buff, numBytes, &retCnt) < 0)
		return 0;
	memcpy(&head[0] + bitSize, &buff, retCnt);
	bitSize += retCnt;
	head[bitSize] = '\0';
	buff[retCnt]='\0';
	int dataSize = atoi(buff);
	if (dataSize == 0)
		return 0;

	float fdata[1000];
	char data[4000];
	// Read data to array of char 
	if (viRead(rsrc, (ViPBuf)data, dataSize + 1, &retCnt) < 0)
		return 0;
	for (int i = 0; i < dataSize/4; i++)
	{	
		char point[4];
		// The first byte is most significant, need to revert every 4 bytes
		for (int j = 0; j < 4; j++)
			point[3-j] = data[4*i+j];
		// Conversion to float
		memcpy(fdata + i, point, sizeof(float));
		// Show element of array
		printf("%f ", fdata[i]);
	}
						
	// Sample result
	std::cout << std::endl << "Result: ";
	viQueryf(rsrc, "FETCh?\n", "%T", buff);
	std::cout << buff;

	// Read error
	status = viQueryf(rsrc, "SYSTem:ERRor?\n", "%T", buff);
	std::cout << "Error: "<< buff;

	// Close session
	viClose(rsrc);

	// Close resource manager
	viClose(rm);
	
	std::cout << "to exit press any key!" << std::endl;
	getch();	// Waiting for user response
	
	return 0;
}

