/// <summary> /// Initializes the Agilent instrument /// </summary> /// <param name="resourceName"></param> /// <returns></returns> public bool Init(string resourceName) { lock (initLock) { _ResourceName = resourceName; if (IsInistialized) { return(true); } try { if (_Driver == null) { _Driver = new AgilentU254x(); } else { Close(); Marshal.ReleaseComObject(_Driver); _Driver = new AgilentU254x(); } _Driver.Initialize(resourceName, false, true, Options); _Driver.DriverOperation.QueryInstrumentStatus = false; _Driver.DriverOperation.Cache = false; _Driver.DriverOperation.RecordCoercions = false; _Driver.DriverOperation.InterchangeCheck = false; _Driver.System.TimeoutMilliseconds = 5000; _ChannelArray = new AgilentU254xDigitalChannel[] { _Driver.Digital.Channels.get_Item("DIOA"), _Driver.Digital.Channels.get_Item("DIOB"), _Driver.Digital.Channels.get_Item("DIOC"), _Driver.Digital.Channels.get_Item("DIOD") }; for (int i = 0; i < _ChannelArray.Length; i++) { if (_ChannelArray[i].Direction != AgilentU254xDigitalChannelDirectionEnum.AgilentU254xDigitalChannelDirectionOut) { _ChannelArray[i].Direction = AgilentU254xDigitalChannelDirectionEnum.AgilentU254xDigitalChannelDirectionOut; } } Reset_Digital(); _AI_ChannelCollection = new AI_Channels(ref _Driver); _AO_ChannelCollection = new AO_Channels(ref _Driver); _IsInitialized = true; return(true); } catch { return(false); } } }
static void Main(string[] args) { Process currProcess = Process.GetCurrentProcess(); // Printing initial memory, occupied by the process int memsizeInit = getMemoryUsage(ref currProcess); Console.WriteLine(string.Format("Initial memory occupied by the process is {0}\r\n", memsizeInit)); #region Initializing COM objects driver = new AgilentU254x(); driver.Initialize(ResourceID, false, true); driver.DriverOperation.QueryInstrumentStatus = false; driver.DriverOperation.Cache = false; driver.DriverOperation.RecordCoercions = false; driver.DriverOperation.InterchangeCheck = false; driver.System.TimeoutMilliseconds = 5000; analogInChannel1 = driver.AnalogIn.Channels.get_Item("AIn1"); analogInChannel2 = driver.AnalogIn.Channels.get_Item("AIn2"); analogInChannel3 = driver.AnalogIn.Channels.get_Item("AIn3"); analogInChannel4 = driver.AnalogIn.Channels.get_Item("AIn4"); analogInChannel1.Configure(AgilentU254xAnalogPolarityEnum.AgilentU254xAnalogPolarityBipolar, 1.25, true); analogInChannel2.Configure(AgilentU254xAnalogPolarityEnum.AgilentU254xAnalogPolarityBipolar, 1.25, false); analogInChannel3.Configure(AgilentU254xAnalogPolarityEnum.AgilentU254xAnalogPolarityBipolar, 1.25, false); analogInChannel4.Configure(AgilentU254xAnalogPolarityEnum.AgilentU254xAnalogPolarityBipolar, 1.25, false); #endregion #region Test Acquisition int samplingFrequency = 500000; driver.AnalogIn.MultiScan.Configure(samplingFrequency, -1); driver.AnalogIn.Acquisition.BufferSize = samplingFrequency; bool acquisitionInProgress = true; driver.AnalogIn.Acquisition.Start(); var acquisitionTask = Task.Factory.StartNew(new Action(() => { int readingsCounter = 0; short[] buf = { 0 }; while (acquisitionInProgress) { if (driver.AnalogIn.Acquisition.BufferStatus == AgilentU254xBufferStatusEnum.AgilentU254xBufferStatusDataReady) { driver.AnalogIn.Acquisition.Fetch(ref buf); Console.WriteLine(string.Format("Number of readings with sampling frequency {0} is {1}.", samplingFrequency, readingsCounter + 1)); ++readingsCounter; } } })); int numOfSeconds = 10; Thread.Sleep(numOfSeconds * 1000); driver.AnalogIn.Acquisition.Stop(); acquisitionInProgress = false; while (!acquisitionTask.IsCompleted) { ; } #endregion #region Releasing COM objects if (analogInChannel4 != null) { while (Marshal.ReleaseComObject(analogInChannel4) > 0) { ; } analogInChannel4 = null; } if (analogInChannel3 != null) { while (Marshal.ReleaseComObject(analogInChannel3) > 0) { ; } analogInChannel3 = null; } if (analogInChannel2 != null) { while (Marshal.ReleaseComObject(analogInChannel2) > 0) { ; } analogInChannel2 = null; } if (analogInChannel1 != null) { while (Marshal.ReleaseComObject(analogInChannel1) > 0) { ; } analogInChannel1 = null; } if (driver != null) { while (Marshal.ReleaseComObject(driver) > 0) { ; } driver = null; } #endregion // Printing memory, occupied by the process after data readings Console.WriteLine(); int memsizeEnd = getMemoryUsage(ref currProcess); Console.WriteLine(string.Format("Memory occupied by the process after data readings is {0}\r\n", memsizeEnd)); Console.ReadKey(); }