/****************************************************************************
    '    Copyright © 2005, 2006 Agilent Technologies Inc. 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 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 support engineers can help explain the
    ' functionality of Agilent software components and associated
    ' commands, but they will not modify these samples to provide added
    ' functionality or construct procedures to meet your specific needs.
'****************************************************************************/

//' This program uses the Agilent 34980A IVI-COM driver.
//' The IVI-COM driver is loaded as a reference from the Project/References menu
//' When loading the references, select:
//'           IVI Agilent34980A 1.0 Type Library
//'           IVI Agilent34952A 1.0 Type Library
//'
//' This program requires either NI Visa or
//' Visa from Agilent I/O library version M or later to be installed
//' on the computer.
//'
//' 34952A
//' This program demonstrates the use of the 34952A to perform digital IO, output a voltage
//' level on a DAC channel, and read the totalizer (you must supply an external signal to the
//' totalizer input to obtain readings other than 0 with the totalizer).


#include "stdafx.h"
#include <atlstr.h>
#include <atlsafe.h>

#import "C:/Program Files/IVI/Bin/Agilent34952A.dll"	no_namespace


int _tmain(int argc, _TCHAR* argv[])
{
	::CoInitialize(NULL);


	try
	{

		IAgilent34980A2Ptr host(__uuidof(Agilent34980A));

		// Setup IVI-defined initialization options
		CString strStandardInitOptions = 
			"Cache=false, InterchangeCheck=false, QueryInstrStatus=true, RangeCheck=false, RecordCoercions=false, Simulate=false";
			
		// Initialize Switch and clear status system/errors
		host->Initialize("TCPIP0::156.140.92.11::INSTR", VARIANT_FALSE, VARIANT_TRUE, LPCTSTR(strStandardInitOptions));	
		host->Status->Clear();

		IAgilent34952APtr multiFunc = (IAgilent34952APtr) host->Personality->Select( _bstr_t("Agilent34952A") );

	//	Configure channels 1 and 2 as output, and channel 3 as Input
		multiFunc->Digital->Configure(_bstr_t("1001"), Agilent34952ADigitalModeWord,	Agilent34952ADirectionInput);
		multiFunc->Digital->Configure(_bstr_t("1003"),Agilent34952ADigitalModeByte,Agilent34952ADirectionOutput);
				
	//	Write 6255 as Word to channels 1 and 2
		multiFunc->Digital->WriteWord(_bstr_t("1001"),6255);
			
	//	Write Byte of 85 to channel 2 
		multiFunc->Digital->WriteByte(_bstr_t("1002"),85);
	//	Write Byte of 70 to channel 1 
		multiFunc->Digital->WriteByte(_bstr_t("1001"),70);

	//	Read byte on channel 3 (input chan) and print it
		BYTE byteResp = multiFunc->Digital->ReadByte(_bstr_t("1003"));
		printf("Byte response from channel 1003 was: %d \n" + byteResp);

	//	Output 4.0 Volts on DAC (channel 6)
	//	In our example, we wired this channel to the Gate input of the totalizer to enable it
		multiFunc->DAC->put_Voltage(_bstr_t("1006"), 4.0);
				
                                            
	//	Now lets configure and operate the totalizer (note that we enabled the gate via the DAC above)
		multiFunc->Totalize->Configure(_bstr_t("1005"),Agilent34952ATotalizeTypeRead,
					Agilent34952ATotalizeThresholdModeAC, Agilent34952ATotalizeSlopePositive);

	//	clear any counts that may be on the totalizer
				multiFunc->Totalize->ClearTotal(_bstr_t("1005"));
				
	//	Read totalizer, if you have nothing connected it will read 0
		int index;
		double real64Resp;
		for (index=0; index<=3; index++)
		{
			real64Resp = multiFunc->Totalize->ReadData(_bstr_t("1005"));
			printf("Pass %d   Reading = %lf \n", index, real64Resp);
		}

		// Close instrument session
		host->Close();
	}
	catch (_com_error& e)
	{
		::MessageBox(NULL, e.Description(), e.ErrorMessage(), MB_ICONERROR);
	}

	::CoUninitialize();

	printf("\nDone - Press Enter to Exit");
    getchar();

	return 0;
}

