//Example of using the Multifunction card 34952A

// """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
//  © 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.
// """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

//' 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).

using System;
using System.Diagnostics;
using System.Threading;

using Ivi.Driver.Interop;
using Agilent.Agilent34980A.Interop;
using Agilent.Agilent34952A.Interop;


	public class Multifunction
	{
		public static void Main(string[] args)
		{
			string respStr;
			byte byteResp;
			double real64Resp;
			int index;

	//		create an instance of the driver interface
			IAgilent34980A2 host = new Agilent34980AClass();
		
			try
			{
				// Setup IVI-defined initialization options
				string standardInitOptions = "QueryInstrStatus=false, Simulate=false";
				
	// Setup driver-specific initialization options
				string driverSetupOptions = 
					"DriverSetup=Model=Agilent34980A";
					
	//			Open a session to the instrument and reset the instrument
	//			host.Initialize("GPIB::9::INSTR", false, true, standardInitOptions + "," + driverSetupOptions);
				host.Initialize("TCPIP0::156.140.92.93::INSTR", false, true, standardInitOptions + "," + driverSetupOptions);

	//			Get a handle to the 34952A moduel "personality"
				IAgilent34952A multiFunc = (IAgilent34952A) host.Personality.Select( "Agilent34952A" );

				respStr = host.System.get_CardType(1);
				Console.WriteLine("Card type was:" + respStr);
                
	//			Configure channels 1 and 2 as output, and channel 3 as Input
				multiFunc.Digital.Configure("1001",Agilent34952ADigitalModeEnum.Agilent34952ADigitalModeWord,
					Agilent34952ADirectionEnum.Agilent34952ADirectionInput);
				multiFunc.Digital.Configure("1003",Agilent34952ADigitalModeEnum.Agilent34952ADigitalModeByte,
					Agilent34952ADirectionEnum.Agilent34952ADirectionOutput);
				
	//			Write 6255 as Word to channels 1 and 2
				multiFunc.Digital.WriteWord("1001",6255);
				
	//			Write Byte of 85 to channel 2 
				multiFunc.Digital.WriteByte("1002",85);
	//			Write Byte of 70 to channel 1 
				multiFunc.Digital.WriteByte("1001",70);

	//			Read byte on channel 3 (input chan) and print it
				byteResp = multiFunc.Digital.ReadByte("1003");
				Console.WriteLine("Byte response from channel 1003 was:" + 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.set_Voltage("1006", 4.0);
				
                                            
	//			Now lets configure and operate the totalizer (note that we enabled the gate via the DAC above)
				multiFunc.Totalize.Configure("1005",Agilent34952ATotalizeTypeEnum.Agilent34952ATotalizeTypeRead,
					Agilent34952ATotalizeThresholdModeEnum.Agilent34952ATotalizeThresholdModeAC,
					Agilent34952ATotalizeSlopeEnum.Agilent34952ATotalizeSlopePositive);

	//			clear any counts that may be on the totalizer
				multiFunc.Totalize.ClearTotal("1005");
				
	//			Read totalizer, if you have nothing connected it will read 0
				for (index=0; index<=3; index++)
				{
					real64Resp = multiFunc.Totalize.ReadData("1005");
					Console.WriteLine("Pass " + index + " Reading = " + real64Resp);
				}
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
			}
			host.Close();   // close the connection to the instrument gracefully
			Console.WriteLine("Done - Press Enter to Exit");
			Console.ReadLine();

		
		}
	}

