//                           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 NLog;
using Raytheon.Common;
using Raytheon.Units;
using System;
using System.Net.Sockets;
using System.Text;

namespace Raytheon.Instruments
{
   /// <summary>
   /// A class that provides an interface for controlling power supply modules via SCPI commands.
   /// </summary>
   public class PowerSupplyKeysight : IDCPwr
	{
		#region PublicClassMembers
#pragma warning disable CS0067
		public event EventHandler<OverCurrentEventArgs> OverCurrent;
		public event EventHandler<OverVoltageEventArgs> OverVoltage;
#pragma warning restore

		#endregion

		#region PrivateClassMembers

		private string _name;
		private readonly PowerScpiCommands _scpiCommands;
		private double _overCurrentProtection;
		private double _overVoltageProtection;
		private double _voltageSetpoint;
		private readonly double _voltageSetpointInitial;
		private readonly double _maxVoltageSetpoint;
		private readonly double _minVoltageSetpoint;
		private readonly double _inRushDelay;
		private readonly double _slewRateVoltsPerSecond;
		private readonly int _moduleNumber;
		private byte[] _readBuffer;
		private NetworkStream _tcpStream;
		private State _state;
		private const int _READ_TIMEOUT = 5000;

		/// <summary>
		/// NLog logger
		/// </summary>
		private readonly ILogger _logger;

		#endregion

		#region PublicFuctions

		/// <summary>
		/// The constructor which sets the power supply to constant voltage mode, set the OCP, OVP and setpoint voltage
		/// </summary>
		/// <param name="name">The logical name of the supply.</param>
		/// <param name="scpiCommands">Scpi commands to use.</param>
		/// <param name="overCurrentProtection">The overcurrent protection setting (Amps).</param>
		/// <param name="overVoltageProtection">The overvoltage protection setting (Volts).</param>
		/// <param name="voltageSetpoint">The voltage setpoint (Volts).</param>
		/// <param name="maxVoltageSetpoint">The voltage setpoint max (Volts) soft limit.</param>
		/// <param name="minVoltageSetpoint">The voltage setpoint min (Volts) soft limit.</param>
		/// <param name="inRushDelayInSeconds">In rush delay in seconds.-1 to leave it as default</param>
		/// <param name="slewRateVoltsPerSecond">The ramp up rate.-1 to leave it as default</param>
		/// <param name="tcpStream">TCP Stream (Ethernet).</param>
		/// <param name="moduleNumber">The module number (multiple modules in a system).</param>
		public PowerSupplyKeysight(string name, PowerScpiCommands scpiCommands, double overCurrentProtection, double overVoltageProtection, double voltageSetpoint, double maxVoltageSetpoint, double minVoltageSetpoint, double inRushDelayInSeconds, double slewRateVoltsPerSecond, NetworkStream tcpStream, int moduleNumber = -1)
		{
			const int READ_BUFFER_SIZE = 512;

			_name = name;
			_logger = LogManager.GetCurrentClassLogger();
			_scpiCommands = scpiCommands;
			_overCurrentProtection = overCurrentProtection;
			_overVoltageProtection = overVoltageProtection;
			_voltageSetpoint = voltageSetpoint;
			_voltageSetpointInitial = voltageSetpoint;
			_maxVoltageSetpoint = maxVoltageSetpoint;
			_minVoltageSetpoint = minVoltageSetpoint;
			_inRushDelay = inRushDelayInSeconds;
			_slewRateVoltsPerSecond = slewRateVoltsPerSecond;
			_moduleNumber = moduleNumber;
			_readBuffer = new byte[READ_BUFFER_SIZE];

			_tcpStream = tcpStream;

			_state = State.Uninitialized;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public bool ClearErrors()
		{
			return _state == State.Uninitialized;
		}

		/// <summary>
		/// Dispose of this object.
		/// </summary>
		public void Dispose()
		{
			try
			{
				Dispose(true);

				GC.SuppressFinalize(this);
			}
			catch (Exception)
			{
				try
				{
					//ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
				}
				catch (Exception)
				{
					//Do not rethrow. Exception from error logger that has already been garbage collected
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Current CurrentLimit
		{
			get
			{
				return Current.FromAmps(ReadOcpSetpoint());
			}

			set
			{
				_overCurrentProtection = value.Amps;

				SetAndConfirmOCP();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string DetailedStatus
		{
			get
			{
				return "This is a Keysight Scpi Power Supply";
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool DisplayEnabled
		{
			get
			{
				return false;
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool Enabled
		{
			get
			{
				return IsOutputOn();
			}

			set
			{
				if (value == false)
				{
					Off();
				}
				else
				{
					On();
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool FrontPanelEnabled
		{
			get
			{
				return false;
			}

			set
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// Returns this object's module number.
		/// </summary>
		/// <returns>The module number.</returns>
		/*public int GetModuleNumber()
		{
		   return _moduleNumber;
		}*/

		/// <summary>
		/// 
		/// </summary>
		public bool InhibitEnabled
		{
			get
			{
				return false;
			}

			set
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public InstrumentMetadata Info
		{
			get
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void Initialize()
		{
			if (_state == State.Uninitialized)
			{
				// make sure it is off
				Off();

				// set up power supply
				// for 6702 look at page 36
				SetConstantVoltageMode();
				SetAndConfirmOVP();
				SetAndConfirmOCP();
				SetVoltageSetpoint(_voltageSetpoint);

				// -1.0 means leave as default
				if (_inRushDelay != -1.0)
				{
					SetAndConfirmInRushDelay();
				}

				// -1.0 means leave as default
				if (_slewRateVoltsPerSecond != -1.0)
				{
					SetAndConfirmSlewRate();
				}

				_state = State.Ready;
			}
			else
			{
				throw new Exception("PowerSupplyKeysight::Initialize() - " + _name + " expected the state to be Uninitialized, state was: " + _state.ToString());
			}
		}


		/// <summary>
		/// Control the power supply internal mechanical relay state
		/// </summary>
		/// <param name="shallWeConnect">True to connect, false to disconnect</param>
		public void MechanicalRelayOutputControl(bool shallWeConnect)
		{
			throw new Exception("Not yet implemented");
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public Current MeasureCurrent()
		{
			return Current.FromAmps(ReadCurrent());
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public Voltage MeasureVoltage()
		{
			return Voltage.FromVolts(ReadVoltage());
		}

		/// <summary>
		/// 
		/// </summary>
		public string Name
		{
			get { return _name; }
			set { _name = value; }
		}

		/// <summary>
		/// 
		/// </summary>
		public Voltage OutputVoltage
		{
			get
			{
				return Voltage.FromVolts(ReadVoltageSetpoint());
			}

			set
			{
				double volts = value.Volts;

				// do not let host set the voltage out of range, unless it is being set to 0
				if (volts != 0.0)
				{
					if (volts > _maxVoltageSetpoint || volts < _minVoltageSetpoint)
					{
						throw new Exception("PowerSupplyKeysight::OutputVoltage() - Desired voltage setpoint out of range for supply " + _name + ". Commanded setpoint: " + value.ToString() + ", Max: " + _maxVoltageSetpoint.ToString() + ", Min: " + _minVoltageSetpoint.ToString());
					}
				}

				SetVoltageSetpoint(volts);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Voltage OverVoltageProtection
		{
			get
			{
				return Voltage.FromVolts(ReadOvpSetpoint());
			}

			set
			{
				_overVoltageProtection = value.Volts;

				SetAndConfirmOVP();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool OverVoltageProtectionEnabled
		{
			get
			{
				return true;
			}

			set
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public SelfTestResult PerformSelfTest()
		{
			throw new NotImplementedException("PowerSupplyKeysight::PerformSelfTest() - self test is not implemented in the " + _name + " supply");
		}

		/// <summary>
		/// Read the overvoltage and overcurrent protection status.
		/// </summary>
		/// <returns>The binary sum of all bits (decimal value) set in the Questionable Status Condition register.</returns>
		public int ReadProtectionStatus()
		{
			// send the command
			string command = _scpiCommands._READ_PROTECTION_STATUS_CMD + " " + GetModuleCmd() + "\n";
			string rspStr = IOQuery(command);

			// parse the response
			string[] tokens = rspStr.Split('\n');

			int ret = Util.ConvertStringToInt32(tokens[0]);

			return ret;
		}

		/// <summary>
		/// 
		/// </summary>
		public void Reset()
		{
			throw new NotImplementedException("PowerSupplyKeysight::Reset() - cant reset a module, only the system");
		}

		/// <summary>
		/// 
		/// </summary>
		public SelfTestResult SelfTestResult
		{
			get
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public State Status
		{
			get
			{
				return _state;
			}
		}

		/// <summary>
		/// 
		/// </summary>

		public void Shutdown()
		{
			if (_state == State.Ready)
			{
				string errorMsg = "";

				try
				{
					Off();
				}
				catch (Exception err)
				{
					errorMsg += err.Message + " ";
				}

				_state = State.Uninitialized;

				if (errorMsg != "")
				{
					throw new Exception("PowerSupplyKeysight::ShutdDown() - Power Supply " + _name + " had an error: " + errorMsg);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Voltage VoltageSoftLimit
		{
			get
			{
				return Voltage.FromVolts(_maxVoltageSetpoint);
			}

			set
			{
				throw new NotImplementedException();
			}
		}

      #endregion

      #region PrivateFuctions
      /// <summary>
      /// The finalizer.
      /// </summary>
      ~PowerSupplyKeysight()
      {
         Dispose(false);
      }

      /// <summary>
      /// Dispose of this object
      /// </summary>
      /// <param name="disposing">True = currently disposing, False = not disposing.</param>
      protected virtual void Dispose(bool disposing)
      {
         try
         {
            if (disposing)
            {
               if (_state == State.Ready)
               {
                  try
                  {
                     Off();
                  }
                  catch (Exception)
                  {
                  }

                  _state = State.Uninitialized;
               }
            }
         }
         catch (Exception)
         {
            try
            {
               //ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
            }
            catch (Exception)
            {
               //Do not rethrow. Exception from error logger that has already been garbage collected
            }
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      private double GetSlewRate()
      {
         // send the command
         string command = _scpiCommands._READ_VOLTAGE_SLEW_CMD + " " + GetModuleCmd() + "\n";
         string rspStr = IOQuery(command);

         string[] tokens = rspStr.Split('\n');

         // parse the response
         double rsp = Util.ConvertStringToDouble(tokens[0]);

         return rsp;
      }

      /// <summary>
      /// Query the output state.
      /// </summary>
      /// <returns>The output state. True = On, False = Off.</returns>
      private bool IsOutputOn()
      {
         // send the command and read the rsp
         string command = _scpiCommands._READ_OUTPUT_STATUS_CMD + " " + GetModuleCmd() + "\n";
         string rspStr = IOQuery(command);

         // parse the response
         string[] tokens = rspStr.Split('\n');

         int rsp = Util.ConvertStringToInt32(tokens[0]);

         if (rsp == 0)
         {
            return false;
         }
         else
         {
            return true;
         }
      }

      /// <summary>
      /// Turn off the output.
      /// </summary>
      private void Off()
      {
         // send the command
         string command = _scpiCommands._SET_OUTPUT_DISABLE_CMD + "," + GetModuleCmd() + "\n";
         IOWrite(command);
      }

      /// <summary>
      /// Turn on the output.
      /// </summary>
      private void On()
      {
         // send the command
         string command = _scpiCommands._SET_OUTPUT_ENABLE_CMD + "," + GetModuleCmd() + "\n";
         IOWrite(command);
      }

      /// <summary>
      /// Read the current.
      /// </summary>
      /// <returns>The current (Amps).</returns>
      private double ReadCurrent()
      {
         // send the command
         string command = _scpiCommands._READ_CURRENT_CMD + " " + GetModuleCmd() + "\n";
         string rspStr = IOQuery(command);

         string[] tokens = rspStr.Split('\n');

         // parse the response
         double rsp = Util.ConvertStringToDouble(tokens[0]);

         return rsp;
      }

      /// <summary>
      /// Read the voltage.
      /// </summary>
      /// <returns>The voltage (Volts).</returns>
      private double ReadVoltage()
      {
         // send the command
         string command = _scpiCommands._READ_VOLTAGE_CMD + " " + GetModuleCmd() + "\n";
         string rspStr = IOQuery(command);

         string[] tokens = rspStr.Split('\n');

         // parse the response
         double rsp = Util.ConvertStringToDouble(tokens[0]);

         return rsp;
      }

      /// <summary>
      /// set the slew rate of the supply
      /// </summary>
      /// <param name="commandedSlew">slew in volts per second</param>
      private void SetAndConfirmSlewRate()
      {
         // send the command
         string command = _scpiCommands._SET_VOLTAGE_SLEW_CMD + " " + _slewRateVoltsPerSecond + "," + GetModuleCmd() + "\n";
         IOWrite(command);

         // read back the slew
         double programmedSlewValue = GetSlewRate();

         if (programmedSlewValue != _slewRateVoltsPerSecond)
         {
            string errMsg = "PowerSupplyKeysight:SetAndConfirmSlewRate() - power supply " + _name + " reported a setpoint slew rate that was not cmmanded. Trying to set it to: " + _slewRateVoltsPerSecond + ", the power supply returned: " + programmedSlewValue;
            throw new Exception(errMsg);
         }
      }

      /// <summary>
      /// Set the voltage setpoint.
      /// </summary>
      /// <param name="voltage">The desired voltage setpoint.</param>
      private void SetVoltageSetpoint(double voltage)
      {
         const double TOLERANCE = .1;

         // do not let host set the voltage out of range, unless it is being set to 0
         if (voltage != 0.0)
         {
            if (voltage > _maxVoltageSetpoint || voltage < _minVoltageSetpoint)
            {
               throw new Exception("PowerSupplyKeysight:SetVoltageSetpoint() - Desired voltage setpoint for supply " + _name + " out of range. Commanded setpoint: " + voltage.ToString() + ", Max: " + _maxVoltageSetpoint.ToString() + ", Min: " + _minVoltageSetpoint.ToString());
            }

            if (voltage > _overVoltageProtection)
            {
               throw new Exception("PowerSupplyKeysight:SetVoltageSetpoint() - Desired voltage setpoint for supply " + _name + " out of range. Commanded setpoint: " + voltage.ToString() + ", OVP is: " + _overVoltageProtection.ToString());
            }
         }

         // send the command
         string voltageCommand = _scpiCommands._SET_VOLTAGE_SETPOINT_CMD + " " + voltage + "," + GetModuleCmd() + "\n";
         IOWrite(voltageCommand);

         // read back the voltage to make sure the command worked
         double voltageSetpointReturned = ReadVoltageSetpoint();

         if (voltageSetpointReturned < (voltage - TOLERANCE))
         {
            string errMsg = "PowerSupplyKeysight:SetVoltageSetpoint() - power supply " + _name + " reported setpoint voltage lower than expected. Trying to set it to: " + voltage + ", the power supply returned: " + voltageSetpointReturned;
            throw new Exception(errMsg);
         }
         else if (voltageSetpointReturned > (voltage + TOLERANCE))
         {
            string errMsg = "PowerSupplyKeysight:SetVoltageSetpoint() - power supply " + _name + " reported setpoint voltage higher than expected. Trying to set it to: " + voltage + ", the power supply returned: " + voltageSetpointReturned;
            throw new Exception(errMsg);
         }

         // update our member now that everything checks out
         _voltageSetpoint = voltage;
      }

      /// <summary>
      /// 
      /// </summary>
      private string GetResponse()
      {
         string response = String.Empty;

         int bytesRead = _tcpStream.Read(_readBuffer, 0, _readBuffer.Length);

         if (bytesRead > 0)
         {
            response = Encoding.ASCII.GetString(_readBuffer, 0, bytesRead);
         }
         
         return response;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="readTimeout"></param>
      private void CommSetReadTimeout(int readTimeout)
      {
         _tcpStream.ReadTimeout = readTimeout;
      }

      /// <summary>
      /// 
      /// </summary>
      private void CommInterfaceWrite(byte[] dataToWrite)
      {
         _tcpStream.Write(dataToWrite, 0, dataToWrite.Length);
      }

      /// <summary>
      /// Get the error code.
      /// </summary>
      /// <returns>The error code (number).</returns>
      private int GetErrorCode()
      {
         // not calling IOQuery() here so IOQuery() can call GetErrorCode() after each query

         string command = _scpiCommands._READ_ERROR_STATUS_CMD + "\n";

         // convert to a byte array
         byte[] commandBuffer = Encoding.ASCII.GetBytes(command);

         // send the data out
         CommInterfaceWrite(commandBuffer);

         // clear our buffer
         Array.Clear(_readBuffer, 0, _readBuffer.Length);

         // read the response
         string rspStr = GetResponse();

         // parse the response
         string[] tokens = rspStr.Split(',');

         int ret = Util.ConvertStringToInt32(tokens[0]);

         return ret;
      }

      /// <summary>
      /// Get the module formatted string.
      /// </summary>
      /// <returns>The module formatted string.</returns>
      private string GetModuleCmd()
      {
         string ret = "";

         if (_moduleNumber > 0)
         {
            ret = "(@" + _moduleNumber + ")";
         }

         return ret;
      }

      /// <summary>
      /// Send a command to the power supply and get the response.
      /// </summary>
      /// <param name="commandString">The command to send.</param>
      /// <returns>The response.</returns>
      private string IOQuery(string commandString)
      {
         // not calling IOWrite() so IOWrite() can check for errors after each write

         // convert to a byte array
         byte[] commandBuffer = Encoding.ASCII.GetBytes(commandString);

         // send the data out
         CommInterfaceWrite(commandBuffer);

         // clear our buffer
         Array.Clear(_readBuffer, 0, _readBuffer.Length);

         // read the response
         string rspStr = GetResponse();

         // check for errors
         int err = GetErrorCode();
         if (err != 0)
         {
            throw new Exception("PowerSupplyKeysight:IOQuery() - Power Supply " + _name + " returned error code: " + err.ToString());
         }

         return rspStr;
      }

      /// <summary>
      /// Sends a SCPI Command to the instrument.
      /// </summary>
      /// <param name="commandString">The SCPI Command to be sent to the instrument.</param>
      private void IOWrite(string commandString)
      {
         // convert to a byte array
         byte[] commandBuffer = Encoding.ASCII.GetBytes(commandString);

         // send the data out
         CommInterfaceWrite(commandBuffer);

         // check for errors
         int err = GetErrorCode();
         if (err != 0)
         {
            throw new Exception("PowerSupplyKeysight:IOWrite() - Power Supply " + _name + " returned error code: " + err.ToString());
         }
      }

      /// <summary>
      /// Reads the overcurrent protection setting from the supply.
      /// </summary>
      /// <returns>The overcurrent setting (Amps).</returns>
      private double ReadOcpSetpoint()
      {
         // send the command
         string command = _scpiCommands._READ_OCP_CMD + " " + GetModuleCmd() + "\n";
         string rspStr = IOQuery(command);

         string[] tokens = rspStr.Split('\n');

         // parse the response
         double rsp = Util.ConvertStringToDouble(tokens[0]);

         return rsp;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      private double ReadInrushDelaySetpoint()
      {
         // send the command
         string command = _scpiCommands._READ_INRUSH_DELAY_CMD + " " + GetModuleCmd() + "\n";
         string rspStr = IOQuery(command);

         string[] tokens = rspStr.Split('\n');

         // parse the response
         double rsp = Util.ConvertStringToDouble(tokens[0]);

         return rsp;
      }

      /// <summary>
      /// Read the overvoltage protection setting from the supply.
      /// </summary>
      /// <returns>The overvoltage setting (Volts).</returns>
      private double ReadOvpSetpoint()
      {
         // send the command
         string command = _scpiCommands._READ_OVP_CMD + " " + GetModuleCmd() + "\n";
         string rspStr = IOQuery(command);

         string[] tokens = rspStr.Split('\n');

         // parse the response
         double rsp = Util.ConvertStringToDouble(tokens[0]);

         return rsp;
      }

      /// <summary>
      /// Read the voltage setpoint.
      /// </summary>
      /// <returns>The voltage< setpoint (Volts)./returns>
      private double ReadVoltageSetpoint()
      {
         // send the command
         string command = _scpiCommands._READ_VOLTAGE_SETPOINT_CMD + " " + GetModuleCmd() + "\n";
         string rspStr = IOQuery(command);

         string[] tokens = rspStr.Split('\n');

         // parse the response
         double rsp = Util.ConvertStringToDouble(tokens[0]);

         return rsp;
      }

      /// <summary>
      /// Put supply in constant voltage mode.
      /// </summary>
      private void SetConstantVoltageMode()
      {
         // send the command
         string command = _scpiCommands._SET_CONSTANT_VOLTAGE_CMD + "," + GetModuleCmd() + "\n";
         IOWrite(command);
      }

      /// <summary>
      /// 
      /// </summary>
      private void SetAndConfirmInRushDelay()
      {
         // set in rush delay
         if (_inRushDelay != -1)
         {
            string inrushCommand = _scpiCommands._SET_OCP_INRUSH_DELAY_CMD + " " + _inRushDelay + "," + GetModuleCmd() + "\n";
            IOWrite(inrushCommand);

            // confirm
            double inrushCommandSetpointReturned = ReadInrushDelaySetpoint();
            if (inrushCommandSetpointReturned != _inRushDelay)
            {
               string errMsg = "PowerSupplyKeysight::SetAndConfirmInRushDelay() - power supply " + _name + " reported in rush delay not as expected. Trying to set it to: " + _inRushDelay + ", the power supply returned: " + inrushCommandSetpointReturned;
               throw new Exception(errMsg);
            }
         }
      }

      /// <summary>
      /// Sets and confirms the overcurrent protection value.
      /// </summary>
      private void SetAndConfirmOCP()
      {
         // set current
         string setCurrentCommand = _scpiCommands._SET_OCP_CMD + " " + _overCurrentProtection + "," + GetModuleCmd() + "\n";
         IOWrite(setCurrentCommand);

         // confirm
         double currentSetpointReturned = ReadOcpSetpoint();
         if (currentSetpointReturned != _overCurrentProtection)
         {
            string errMsg = "PowerSupplyKeysight::SetAndConfirmOCP() - power supply " + _name + " reported setpoint current not as expected. Trying to set it to: " + _overCurrentProtection + ", the power supply returned: " + currentSetpointReturned;
            throw new Exception(errMsg);
         }

         //enable OCP
         string ocpEnableCommand = _scpiCommands._SET_OCP_ON_CMD + "," + GetModuleCmd() + "\n";
         IOWrite(ocpEnableCommand);
      }

      /// <summary>
      /// Sets and confirms the overvoltage protection value.
      /// </summary>
      private void SetAndConfirmOVP()
      {
         // send the command
         string command = _scpiCommands._SET_OVP_CMD + " " + _overVoltageProtection + "," + GetModuleCmd() + "\n";
         IOWrite(command);

         // confirm
         double ovpReturned = ReadOvpSetpoint();

         if (ovpReturned != _overVoltageProtection)
         {
            string errMsg = "PowerSupplyKeysight:SetAndConfirmOVP() - power supply " + _name + " reported setpoint ovp not as expected. Trying to set it to: " + _overVoltageProtection + ", the power supply returned: " + ovpReturned;
            throw new Exception(errMsg);
         }
      }
      #endregion
   }
}
