//****************************************************************************
//    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 demonstrates how to receive digital data on the 34950A module
// and place it into memory (creating a Trace).  The data is received by this
// card and is assumed to be transmitted from some other device (not shown).
//


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

namespace DigitalMemRead
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class DigitalMemRead
	{
		/// <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.161::INSTR", false, true, standardInitOptions + "," + driverSetupOptions);

//              Open a handle to the 34950 driver
				IAgilent34950A digIO = (IAgilent34950A) host.Personality.Select("Agilent34950A");
			    
//				Configure receiver for Input, width = byte, threshold 2.5
				digIO.Digital.Input.Configure("1101",Agilent34950AChannelWidthEnum.Agilent34950AChannelWidthByte, 2.5);

//				Set up handshaking
				digIO.Digital.Handshake.Configure("1101",Agilent34950AHandshakeModeEnum.Agilent34950AHandshakeModeSynchronous,2.5,5.0);
				digIO.Digital.Handshake.SetPolarity("1101",Agilent34950AHandshakeLineIDEnum.Agilent34950AHandshakeLineIDH2,Agilent34950APolarityEnum.Agilent34950APolarityInverted);
				// NOTE: Polarity above may need to be "Inverted" if you are pacing the sender
				// with the same clock the receiver is using -- that way, the rising edge can 
				// cause an output on the sender, and the falling edge can start the "read";
				// the time between edges allows the data to become "stable" before the read. 
				// Unpredictable results occur if you attempt to use the same edge of a single clock
				// source to both pace the "send" and to trigger the "read".

//				set up receiver, rate is 500K, external source for the strobe
				digIO.Digital.Handshake.set_Rate("1101",500000);
				digIO.Digital.Handshake.set_SyncStrobe("1101",Agilent34950AHandshakeStrobeSourceEnum.Agilent34950AHandshakeStrobeSourceExternal);
				digIO.Digital.Input.Memory.Clear("1101");
//				Set sample count to 3000 
				digIO.Digital.Input.Memory.set_SampleCount("1101",3000);

//				Set up the sender (not shown here)

//				Start the receiver first, then start the sender
				digIO.Digital.Input.Memory.Start("1101");

//				Start the sender, he will pace the transfer

//				After transfer complete, stop the receiver 
				digIO.Digital.Input.Memory.Stop("1101");

//				Now read what was written into memory
//				NOTE: You MUST Disable memory on the receiver before reading the contents
				digIO.Digital.Input.Memory.set_Enable("1101",false);

				// Set a longer timeout before data fetch
				host.System.TimeoutMilliseconds = 8000;

				// declare array to hold data
				int[] dataArray = new int[6000];

				// See how much data was sent
				int numPoints = digIO.Digital.Input.Memory.get_DataPoints("1101");

				if (numPoints > 0)
				{
					dataArray = digIO.Digital.Input.Memory.ReadAllData("1101");
					Console.WriteLine("{0} data points were transmitted.", numPoints);
				}
				else
					Console.WriteLine("No data was transmitted.");
			}
			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

			Console.WriteLine("Done - Press Enter to Exit");
			Console.ReadLine();

		}  // End Main()
	}
}