/****************************************************************************
      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 demonstrates how to transfer digital data from 
// the 34950A memory to a receiving module or computer.  The program 
// first creates the data in memory that is to be sent by using one
// of the "patterns" supplied by the 34950A (a sequential count
// pattern).
//

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

#import "C:/Program Files/IVI/Bin/Agilent34950A.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 instrument and clear status system/errors
		host->Initialize("TCPIP0::156.140.92.131::INSTR", VARIANT_FALSE, VARIANT_TRUE, LPCTSTR(strStandardInitOptions));	
		host->Status->Clear();

//		Configure Channel 1001   Temp. Thermist. 5k  °C
		IAgilent34950APtr digIO = (IAgilent34950APtr) host->Personality->Select( _bstr_t("Agilent34950A") );

//		Configure sender for Output, width = byte
		digIO->Digital->Output->Configure(_bstr_t("1101"),Agilent34950AChannelWidthByte, 5.0);

		
//		Create a waveform to output, size = 3000
		digIO->Digital->Trace->CreateStandard(_bstr_t("1101"),Agilent34950ATraceTypeCount,"Count3000",3000);

//		Set up handshaking
		digIO->Digital->Handshake->Configure(_bstr_t("1101"),Agilent34950AHandshakeModeSynchronous,2.5,5.0);

		// set up receiver, rate is 500K
		// NOTE the sender will output on the rising edge of the clock.  The receiver will
		// need to make certain the data is stable before reading it, so the receiver 
		// should not use the same edge of the same clock to "read" the data.

//		Set up sender
//		Disable memory and set sync strobe source INTernal
		digIO->Digital->Output->Memory->put_Enable(_bstr_t("1101"),false);
		digIO->Digital->Handshake->put_SyncStrobe(_bstr_t("1101"),Agilent34950AHandshakeStrobeSourceInternal);

//		Select the name of the trace to output and set cycle count to '1'
		digIO->Digital->Output->Memory->put_TraceSelect(_bstr_t("1101"),_bstr_t("Count3000"));
		digIO->Digital->Output->Memory->put_CycleCount(_bstr_t("1101"),1);
		

//		Enable the sender
		digIO->Digital->Output->Memory->put_Enable(_bstr_t("1101"),true);
			
//		Start the receiver now if not already started
			

//		Now Start the sender, he will pace the transfer
		digIO->Digital->Output->Memory->Start(_bstr_t("1101"));

//		Data will now be transmitted to the receiver
						

		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;
}
