/****************************************************************************
    '    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.
//' This program requires either NI Visa or
//' Visa from Agilent I/O library version M or later to be installed
//' on the computer.
//'
// This program configures some Multiplexer channels for the Dmm to scan, initiates the
// scan, retrieves the data, and prints the results.

#include "stdafx.h"
#include <atlstr.h>
#include <atlsafe.h>
#include <comutil.h>   // used to convert SafeArray BSTR to String for printing

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 instrument and clear status system/errors
		host->Initialize("TCPIP0::156.140.92.11::INSTR", VARIANT_FALSE, VARIANT_TRUE, LPCTSTR(strStandardInitOptions));	
		host->Status->Clear();

		// Configure Channel 1001   Temp. Thermist. 5k  °C
		host->Temperature->Thermistor->Configure(_bstr_t("1001"), Agilent34980AResolutionDefault,
			Agilent34980AThermistor5000);

		// Configure Channels 1004:1005  Volts DC    10 V    5 ½ digits
		host->Voltage->DCVoltage->Configure(_bstr_t("1004,1005"),10.0, Agilent34980AResolutionDefault);

		// Channels 1006   Resistance  10 Mohm 5 ½ digits
		// Channels 1007   Resistance  10 Mohm 5 ½ digits
		host->Resistance->Configure(_bstr_t("1006,1007"),10000000.0,Agilent34980AResolutionDefault);
		
		// Channel 1014   Resistance  100 ohm 5 ½ digits
		host->Resistance->Configure(_bstr_t("1014"),100.0,Agilent34980AResolutionDefault);
		
		// Channels 1015   4w Resistance   100 ohm 4 ½ digits
		host->Resistance->Configure(_bstr_t("1015"),100.0,Agilent34980AResolutionLeast);
					
		// Specify the scan list, 1 sweep per trigger, and have the scan order be ascending by channel number
		host->Scan->ConfigureScan(_bstr_t("1001,1002:1005,1006,1007,1014,1015"), 1, true);

		// Configure the trigger source, set number of triggers 2
		// Set trigger source timer, and time between triggers as 2.2 seconds
        host->Trigger->Configure(Agilent34980ATriggerSourceTimer, 2, 2.2, 1);

		// Read the number of channels in scan list
        int numberChannels = host->Scan->ScanSize;
		
		// Arm the trigger with initiate, this starts the measurement process
		host->Measurement->Initiate();

		// Wait up to 30 seconds for the scan to complete
		host->System->WaitForOperationComplete(30000);

		host->Measurement->DataFormat->Configure(false,true,false,
			Agilent34980ATimeOffsetAbsolute,true);

		//Retrieve the formatted data from the instrument
        SAFEARRAY* psaData = host->Measurement->Fetch();
        CComSafeArray<BSTR> saData;
		saData.Attach(psaData);
		char* lpszText2;
   
        for (int i = 0; i < (int)saData.GetCount(); i++)
        {
			lpszText2 = _com_util::ConvertBSTRToString(saData.GetAt(i));
            printf("%s\n", lpszText2); 
        }

		// NOTE!!!!  destroy SAFEARRAY after use to avoid memory leaks
        ::SafeArrayDestroy(psaData);

		host->Close();

	}
	catch (_com_error& e)
	{
		::MessageBox(NULL, e.Description(), e.ErrorMessage(), MB_ICONERROR);
	}

	::CoUninitialize();

	printf("Press Enter/Return to end program\n");
    getchar();

	return 0;
}
