//Example of writing patterns out of memory on a 34950A card

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

using System;
using Agilent.Agilent34980A.Interop;
using Agilent.Agilent34950A.Interop;


namespace DigitalMemoryWrite
{
	/// <summary>
	/// Summary description for Class
	/// </summary>
	class DigitalMemoryWrite
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		public static void Main(string[] args)
		{
			// create an instance of the core driver
			IAgilent34980A2 host = new Agilent34980AClass();

			try
			{
//				Setup IVI-defined initialization options
				string standardInitOptions =
					"QueryInstrStatus=true, Simulate=false";

//				Setup driver-specific initialization options
				string driverSetupOptions =
					"DriverSetup= Model=Agilent34980A";

//              Open a handle to the instrument
				host.Initialize("TCPIP0::156.140.92.11::INSTR", false, true, standardInitOptions + "," + driverSetupOptions);

//              Open a handle to the 34950 driver
				IAgilent34950A digIO = (IAgilent34950A) host.Personality.Select("Agilent34950A");
			    
//				Configure sender for Output, width = byte
				digIO.Digital.Output.Configure("1101",Agilent34950AChannelWidthEnum.Agilent34950AChannelWidthByte, 5.0);
//				Create a waveform to output, size = 3000
				digIO.Digital.Trace.CreateStandard("1101",Agilent34950ATraceTypeEnum.Agilent34950ATraceTypeCount,"Count3000",3000);

//				Set up handshaking
				digIO.Digital.Handshake.Configure("1101",Agilent34950AHandshakeModeEnum.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.set_Enable("1101",false);
				digIO.Digital.Handshake.set_SyncStrobe("1101",Agilent34950AHandshakeStrobeSourceEnum.Agilent34950AHandshakeStrobeSourceInternal);

//				Select the name of the trace to output and set cycle count to '1'
				digIO.Digital.Output.Memory.set_TraceSelect("1101","Count3000");
				digIO.Digital.Output.Memory.set_CycleCount("1101",1);

//				Enable the sender
				digIO.Digital.Output.Memory.set_Enable("1101",true);
			
//				Start the receiver now if not already started
			

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

//				Data will now be transmitted to the receiver
			
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				int errNum = 5;
				string errMssg = "";
				while (errNum != 0)
				{
					host.Utility.ErrorQuery(ref errNum,ref errMssg);
					if (errNum != 0)
						Console.WriteLine("{0}",errMssg);
				}			
			}
			host.Close();   // close the connection to the instrument gracefully

		}  // End Main()
	}
}
