//Example of using the DAC card 34951A

// """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
//  © Agilent Technologies Inc. 2004, 2005, 2006
//  All rights reserved.
//
// You have a royalty-free right to use, modify, reproduce and distribute
// the Sample Application Files (and/or any modified version) in any way
// you find useful, provided that you agree that Agilent Technologies has no
// warranty, obligations or liability for any Sample Application Files.
//
// Agilent Technologies provides programming examples for illustration only,
// This sample program assumes that you are familiar with the programming
// language being demonstrated and the tools used to create and debug
// procedures. Agilent Technologies support engineers can help explain the
// functionality of Agilent Technologies software components and associated
// commands, but they will not modify these samples to provide added
// functionality or construct procedures to meet your specific needs.
// """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


// NOTE: This include MUST be first, else will not compile cleanly
#include "stdafx.h"
//#include <iostream>
//#include <tchar.h>
#include "ag34980a.h"


// NOTE: The above include (ag34980A.h) needs some IVI shared component includes.  The path to these
// is often something like "C:\Program Files\IVI\Include".  You need to add this to your includes path at
// either the project level or the system level.  For project level, select Tools\Options, then click
// on "Projects" file tab, select "VC++ Directories".  This brings up a dialog box.  In the upper
// right of this dialog, there is a "Show Directories for" drop down -- select Include files. Then 
// input the include path desired in the window beneath.  You may also need to add an include path
// for Visa.h (which is often in C:\Program Files\Visa\winnt\include).
//

#define TEST_STD_BUFFER_SIZE 1024
#define TEST_STD_RESPONSE_SIZE 256
#define TEST_STD_DATA_SIZE	100

ViSession vi;  // needs to be global so can print errors out

void printErrs(ViStatus s)
{
	char errMssg[300];
	ViInt32 errCode = -1;
	ViStatus s2;

	s2 = ag34980a_GetError(vi, &errCode, (ViInt32)300, errMssg);
	printf("Got Visa error: %ld, %s\n", errCode, errMssg);
	if (errCode = -1074135039)  // instr has errors in its queue
	{
		// print out the instr specific errors
		while ((s2 >= 0) && (errCode != 0))
		{
			s2 = ag34980a_error_query(vi, &errCode, errMssg);
			if ( (s2 > -1) && (errCode != 0) )
				printf("Instr reported err: %ld, %s\n", errCode, errMssg);
		}
	}
}

#define	CHECK(func)	\
do {\
    ViStatus _s = (func);\
    if (_s < 0)\
    {\
	printf("Error calling function: %s\n", #func);\
	printErrs(_s);\
    }\
} while (0)
 // Uncomment and add the following two lines before the "while" above if you wish to exit when
 // you detect an error.
 //	_s = ag34980a_close(vi);\
 //   exit(0);\


int _tmain(int argc, _TCHAR* argv[])
{
//Open a session to the mainframe using the IVI-C host driver, Reset the instrument
	ViSession vi;
	ViStatus viStatus;
	char respStr[TEST_STD_RESPONSE_SIZE];
	
	viStatus = ag34980a_init( "TCPIP0::156.140.92.93::INSTR", VI_FALSE, VI_TRUE, &vi );
	
	if (viStatus < 0)
	{
		fprintf(stderr,"Failed to open the instrument.");
		exit(0);
	}

	//Create buffer to hold error responses
	ViInt32 nCount = 0;    // holds return count

	// Get card type of slot 1
	CHECK(ag34980a_GetCardType(vi, 1, TEST_STD_RESPONSE_SIZE,respStr,&nCount));
	printf("Card type was: %s\n", respStr);

	// NOTE: In the next section we could set the level and close the output relay all in one
	// function call:  ag34980a_ApplyLevel (vi, "1003", 3.0);
	// But instead we will do it in two parts by first setting the level, and then closing the output relay
                                
	// Set voltage level of 3.0 on channel 1 
	CHECK(ag34980a_ConfigureSourceVoltage(vi, "1001", 3.0));

	// This is the way to send a SCPI command directly to the instrument
	// This particular string closes the output relay so the voltage level set above can be output
	CHECK(ag34980a_SystemWrite(vi, "OUTP:STAT ON,(@1001)"));

	// Create a Sinewave named ExampleTrace, trace is 200 points in size
	CHECK(ag34980a_CreateStandardTrace34951A(vi, 1, "ExampleTrace", 200,
                                      AG34980A_VAL_TRACE_WAVEFORM_SINUSOID));

	// Output the waveform continuously at frequency 500 Hz, amplitude 5.0 V, offset 0.5 V
	CHECK(ag34980a_ApplyTrace(vi, "1003","ExampleTrace",
		AG34980A_VAL_FUNCTION_TYPE_VOLTAGE, 5.0, 0.5, 500));
 
	printf("Channel 3 should be outputting a 500 Hz Sine wave.\n");
    printf("Press Enter to exit the program\n");
    getchar();

	// close the instrument session
	viStatus = ag34980a_close(vi);
	

}
