// UNCLASSIFIED /*------------------------------------------------------------------------- RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON COMPANY. THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. GOVERNMENT. UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. -------------------------------------------------------------------------*/ using System; using NationalInstruments.ModularInstruments.NIDmm; using NLog; using Raytheon.Common; using Raytheon.Instruments.Dmm; using Raytheon.Units; namespace Raytheon.Instruments { /// /// A simulation implementation of the DMM interface. /// This class basically just absorbs function calls and returns dummy data. /// public class DMMNiPxi : IDmm, IDisposable { #region PrivateMemberVariables private NIDmm _niDmm; private string _name; private readonly string _address; //private readonly string _options; private double _lastRange; private double _lastResolution; private MeasurementFunction _lastType; private State _state; private SelfTestResult _selfTestResult; private readonly ILogger _logger; private readonly IConfigurationManager _configurationManager; private readonly IConfiguration _configuration; #endregion #region PrivateFunctions /// /// The Finalizer. /// ~DMMNiPxi() { Dispose(false); } /// /// /// /// protected virtual void Dispose(bool disposing) { if (disposing) { if (_state == State.Ready) { _niDmm.DriverUtility.Reset(); _niDmm.Close(); _niDmm.Dispose(); _state = State.Uninitialized; } } } #endregion #region PublicFunctions /// /// DMMNiPxi factory constructor /// /// /// public DMMNiPxi(string deviceName, IConfigurationManager configurationManager) { Name = deviceName; _logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}"); _configurationManager = configurationManager; _configuration = _configurationManager.GetConfiguration(Name); _address = _configuration.GetConfigurationValue("DMMNiPxi", "Address", ""); //_options = _configuration.GetConfigurationValue("DMMNiPxi", "Options", ""); _lastType = 0; _lastRange = 0; _lastResolution = 0; //created in Initialize() _niDmm = null; _state = State.Uninitialized; _selfTestResult = SelfTestResult.Unknown; } /// /// /// /// /// /// public DMMNiPxi(string deviceName, string address)//, string options) { _name = deviceName; _address = address; //_options = options; _lastType = 0; _lastRange = 0; _lastResolution = 0; // set in Initialize() _niDmm = null; _logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}"); _state = State.Uninitialized; _selfTestResult = SelfTestResult.Unknown; } /// /// /// /// public bool ClearErrors() { throw new NotImplementedException(); } /// /// /// /// /// /// public void ConfigureCurrentMeasurement(MeasurementFunction type, Current range, Current resolution) { throw new NotImplementedException(); /*_lastType = type; _lastRange = range.Amps; _lastResolution = resolution.Amps;*/ } /// /// /// /// /// /// public void ConfigureFrequencyMeasurement(MeasurementFunction type, Frequency range, Frequency resolution) { if (type != MeasurementFunction.Frequency) { throw new Exception("only frequency is acceptable for param type: " + _lastType.ToString()); } _lastType = type; _lastRange = range.Hertz; _lastResolution = resolution.Hertz; } /// /// /// /// /// /// public void ConfigureResistanceMeasurement(MeasurementFunction type, Resistance range, Resistance resolution) { if (type != MeasurementFunction.FourWireResistance && type != MeasurementFunction.TwoWireResistance) { throw new Exception("only FourWireResistance or TwoWireResistance is acceptable for param type: " + _lastType.ToString()); } _lastType = type; _lastRange = range.Ohms; _lastResolution = resolution.Ohms; } /// /// /// /// /// /// public void ConfigureVoltageMeasurement(MeasurementFunction type, Voltage range, Voltage resolution) { if (type != MeasurementFunction.DCVolts && type != MeasurementFunction.ACVolts) { throw new Exception("only ac or dc volt is acceptable for param type: " + type.ToString()); } _lastType = type; _lastRange = range.Volts; _lastResolution = resolution.Volts; } /// /// /// public string DetailedStatus { get { return "This is a NI Dmm"; } } /// /// /// public bool DisplayEnabled { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } /// /// /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// /// public bool FrontPanelEnabled { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } /// /// /// public InstrumentMetadata Info { get { throw new NotImplementedException(); } } /// /// /// public void Initialize() { if (_state == State.Uninitialized) { _niDmm = new NIDmm(_address, false, true);// _options); _selfTestResult = PerformSelfTest(); _state = State.Ready; } else { throw new Exception("expected the state to be Uninitialized, state was: " + _state.ToString()); } } /// /// /// public MeasurementFunction MeasurementType { get { throw new NotImplementedException(); } } /// /// /// /// /// public Current MeasureCurrent(int timeout) { throw new NotImplementedException(); /* if (_lastType != MeasurementFunction.DCVolts && _lastType != MeasurementFunction.ACVolts) { throw new Exception("only ac or dc volt is acceptable for param type: " + type.ToString()); } return Current.FromAmps(ReadCurrent();*/ } /// /// /// /// /// public Frequency MeasureFrequency(int timeout) { if (_lastType != MeasurementFunction.Frequency) { throw new Exception("only frequency is acceptable for param type: " + _lastType.ToString()); } DmmAuto auto = DmmAuto.Off; if (_lastRange < 0) { auto = DmmAuto.On; // set the starting range for auto range to 10 _lastRange = 10; } _niDmm.MeasurementFunction = DmmMeasurementFunction.Frequency; _niDmm.Range = _lastRange; _niDmm.Resolution = _lastResolution; _niDmm.AutoRange = auto; double frequency = _niDmm.Measurement.Read(); return Frequency.FromHertz(frequency); } /// /// /// /// /// public Resistance MeasureResistance(int timeout) { if (_lastType != MeasurementFunction.FourWireResistance && _lastType != MeasurementFunction.TwoWireResistance) { throw new Exception("only FourWireResistance or TwoWireResistance is acceptable for param type: " + _lastType.ToString()); } if (_lastType == MeasurementFunction.FourWireResistance) { _niDmm.MeasurementFunction = DmmMeasurementFunction.FourWireResistance; } else { _niDmm.MeasurementFunction = DmmMeasurementFunction.TwoWireResistance; } DmmAuto auto = DmmAuto.Off; if (_lastRange < 0) { auto = DmmAuto.On; // set the starting range for auto range to 10 _lastRange = 10; } _niDmm.Range = _lastRange; _niDmm.Resolution = _lastResolution; _niDmm.AutoRange = auto; double resistance = _niDmm.Measurement.Read(); if (double.IsNaN(resistance)) { resistance = double.MaxValue; } return Resistance.FromOhms(resistance); } /// /// /// /// /// public Voltage MeasureVoltage(int timeout) { if (_lastType != MeasurementFunction.DCVolts && _lastType != MeasurementFunction.ACVolts) { throw new Exception("only ac or dc volt is acceptable for param type: " + _lastType.ToString()); } if (_lastType == MeasurementFunction.ACVolts) { _niDmm.MeasurementFunction = DmmMeasurementFunction.ACVolts; } else { _niDmm.MeasurementFunction = DmmMeasurementFunction.DCVolts; } DmmAuto auto = DmmAuto.Off; if (_lastRange < 0) { auto = DmmAuto.On; // set the starting range for auto range to 10 _lastRange = 10; } _niDmm.Range = _lastRange; _niDmm.Resolution = _lastResolution; _niDmm.AutoRange = auto; double volts = _niDmm.Measurement.Read(); if (double.IsNaN(volts)) { volts = double.MaxValue; } return Voltage.FromVolts(volts); } /// /// /// public string Name { get { return _name; } set { _name = value; } } /// /// /// /// public SelfTestResult PerformSelfTest() { DmmSelfTestResult res = _niDmm.DriverUtility.SelfTest(); if (res.Code != 0) { _selfTestResult = Raytheon.Instruments.SelfTestResult.Fail; throw new Exception("self test returned: " + res.Code + "," + res.Message + " on card " + _name); } _selfTestResult = Raytheon.Instruments.SelfTestResult.Pass; return _selfTestResult; } /// /// /// public void Reset() { _niDmm.DriverUtility.Reset(); } /// /// /// public SelfTestResult SelfTestResult { get { return _selfTestResult; } } /// /// /// public State Status { get { return _state; } } /// /// /// public void Shutdown() { if (_state == State.Ready) { _niDmm.DriverUtility.Reset(); _niDmm.Close(); _niDmm.Dispose(); _state = State.Uninitialized; } } #endregion } }