Big changes

This commit is contained in:
Duc
2025-03-13 12:04:22 -07:00
parent c689fcb7f9
commit ffa9905494
748 changed files with 199255 additions and 3743 deletions

View File

@@ -0,0 +1,127 @@
// Ignore Spelling: FRONTPANEL WATCHDOGDELAY WATCHDOGON WATCHDOGOFF OCP OVP SELFTEST
using Raytheon.Common;
namespace Raytheon.Instruments
{
public class PowerScpiCommands
{
// module current commands
public string _SET_OCP_INRUSH_DELAY_CMD;
public string _SET_OCP_CMD;
public string _SET_OCP_ON_CMD;
public string _READ_CURRENT_CMD;
public string _READ_OCP_CMD;
public string _READ_INRUSH_DELAY_CMD;
//public string _READ_OCP_CMD = "CURR:LIM" ?;
// module voltage commands
public string _SET_OVP_CMD;
public string _SET_VOLTAGE_SLEW_CMD;
public string _SET_VOLTAGE_SETPOINT_CMD;
public string _SET_CONSTANT_VOLTAGE_CMD;
public string _READ_VOLTAGE_CMD;
public string _READ_VOLTAGE_SETPOINT_CMD;
public string _READ_OVP_CMD;
public string _READ_VOLTAGE_SLEW_CMD;
// module set output commands
public string _SET_OUTPUT_DISABLE_CMD;
public string _SET_OUTPUT_ENABLE_CMD;
// module query status
public string _READ_OUTPUT_STATUS_CMD;
public string _READ_ERROR_STATUS_CMD;
public string _READ_PROTECTION_STATUS_CMD;
// system commands
public string _CLEAR_CMD;
public string _RESET_CMD;
public string _SELFTEST_CMD;
public string _REBOOT_CMD;
public string _READ_ERROR_CODE_CMD;
// system disable/enable commands
public string _SET_FRONTPANEL_DISABLE_CMD;
public string _SET_FRONTPANEL_ENABLE_CMD;
// system watchdog commands
public string _SET_WATCHDOGDELAY_CMD;
public string _SET_WATCHDOGON_CMD;
public string _SET_WATCHDOGOFF_CMD;
// system coupling commands
public string _SET_COUPLE_CHANNELS_CMD;
public string _SET_COUPLE_ON_CMD;
public string _SET_COUPLE_OUTPUT_PROTECT_ON_CMD;
public string _QUERY_COUPLE_CHANNELS_CMD;
public string _QUERY_COUPLE_STATE_CMD;
// system grouping Commands
public string _SET_GROUP_DEFINE_CMD;
public string _UNGROUP_ALL_CHANNELS_CMD;
public string _QUERY_GROUP_CHANNELS_CMD;
private void ReadModuleCommands(string defFile)
{
IniFile ini = new IniFile(defFile);
_SET_OCP_INRUSH_DELAY_CMD = ini.ReadValue("MODULE", "SET_INRUSH_DELAY_CMD");
_SET_OCP_CMD = ini.ReadValue("MODULE", "SET_OCP_CMD");
_SET_OCP_ON_CMD = ini.ReadValue("MODULE", "SET_OCP_ON_CMD");
_READ_CURRENT_CMD = ini.ReadValue("MODULE", "READ_CURRENT_CMD");
_READ_OCP_CMD = ini.ReadValue("MODULE", "READ_OCP_CMD");
_READ_INRUSH_DELAY_CMD = ini.ReadValue("MODULE", "READ_INRUSH_DELAY_CMD");
_SET_OVP_CMD = ini.ReadValue("MODULE", "SET_OVP_CMD");
_SET_VOLTAGE_SLEW_CMD = ini.ReadValue("MODULE", "SET_VOLTAGE_SLEW_CMD");
_SET_VOLTAGE_SETPOINT_CMD = ini.ReadValue("MODULE", "SET_VOLTAGE_SETPOINT_CMD");
_SET_CONSTANT_VOLTAGE_CMD = ini.ReadValue("MODULE", "SET_CONSTANT_VOLTAGE_CMD");
_READ_VOLTAGE_CMD = ini.ReadValue("MODULE", "READ_VOLTAGE_CMD");
_READ_VOLTAGE_SETPOINT_CMD = ini.ReadValue("MODULE", "READ_VOLTAGE_SETPOINT_CMD");
_READ_OVP_CMD = ini.ReadValue("MODULE", "READ_OVP_CMD");
_READ_VOLTAGE_SLEW_CMD = ini.ReadValue("MODULE", "READ_VOLTAGE_SLEW_CMD");
_SET_OUTPUT_DISABLE_CMD = ini.ReadValue("MODULE", "SET_OUTPUT_DISABLE_CMD");
_SET_OUTPUT_ENABLE_CMD = ini.ReadValue("MODULE", "SET_OUTPUT_ENABLE_CMD");
_READ_OUTPUT_STATUS_CMD = ini.ReadValue("MODULE", "READ_OUTPUT_STATUS_CMD");
_READ_ERROR_STATUS_CMD = ini.ReadValue("MODULE", "READ_ERROR_STATUS_CMD");
_READ_PROTECTION_STATUS_CMD = ini.ReadValue("MODULE", "READ_PROTECTION_STATUS_CMD");
}
private void ReadSystemCommands(string defFile)
{
IniFile ini = new IniFile(defFile);
_CLEAR_CMD = ini.ReadValue("SYSTEM", "CLEAR_CMD");
_RESET_CMD = ini.ReadValue("SYSTEM", "RESET_CMD");
_SELFTEST_CMD = ini.ReadValue("SYSTEM", "SELFTEST_CMD");
_REBOOT_CMD = ini.ReadValue("SYSTEM", "REBOOT_CMD");
_READ_ERROR_CODE_CMD = ini.ReadValue("SYSTEM", "READ_ERROR_CODE_CMD");
_SET_FRONTPANEL_DISABLE_CMD = ini.ReadValue("SYSTEM", "SET_FRONTPANEL_DISABLE_CMD");
_SET_FRONTPANEL_ENABLE_CMD = ini.ReadValue("SYSTEM", "SET_FRONTPANEL_ENABLE_CMD");
_SET_WATCHDOGDELAY_CMD = ini.ReadValue("SYSTEM", "SET_WATCHDOGDELAY_CMD");
_SET_WATCHDOGON_CMD = ini.ReadValue("SYSTEM", "SET_WATCHDOGON_CMD");
_SET_WATCHDOGOFF_CMD = ini.ReadValue("SYSTEM", "SET_WATCHDOGOFF_CMD");
_SET_COUPLE_CHANNELS_CMD = ini.ReadValue("SYSTEM", "SET_COUPLE_CHANNELS_CMD");
_SET_COUPLE_ON_CMD = ini.ReadValue("SYSTEM", "SET_COUPLE_ON_CMD");
_SET_COUPLE_OUTPUT_PROTECT_ON_CMD = ini.ReadValue("SYSTEM", "SET_COUPLE_OUTPUT_PROTECT_ON_CMD");
_QUERY_COUPLE_CHANNELS_CMD = ini.ReadValue("SYSTEM", "QUERY_COUPLE_CHANNELS");
_QUERY_COUPLE_STATE_CMD = ini.ReadValue("SYSTEM", "QUERY_COUPLE_STATE");
_SET_GROUP_DEFINE_CMD = ini.ReadValue("SYSTEM", "SET_GROUP_DEFINE_CMD");
_UNGROUP_ALL_CHANNELS_CMD = ini.ReadValue("SYSTEM", "UNGROUP_ALL_CHANNELS_CMD");
_QUERY_GROUP_CHANNELS_CMD = ini.ReadValue("SYSTEM", "QUERY_GROUP_CHANNELS");
}
public PowerScpiCommands(string scpiDefFile)
{
IniFile ini = new IniFile(scpiDefFile);
ReadModuleCommands(scpiDefFile);
ReadSystemCommands(scpiDefFile);
}
}
}

View File

@@ -0,0 +1,979 @@
// 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
}
}

View File

@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(SolutionDir)Solution.props" />
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AssemblyName>Raytheon.Instruments.PowerSupplySystemKeysight</AssemblyName>
<Product>Power Supply System Keysight Manual implementation</Product>
<Description>Power Supply System Keysight Manual implementation</Description>
<OutputType>Library</OutputType>
<!-- Static versioning (Suitable for Development) -->
<!-- Disable the line below for dynamic versioning -->
<Version>1.1.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="5.0.0" />
<PackageReference Include="Raytheon.Instruments.PowerSupplySystem.Contracts" Version="1.3.0" />
<PackageReference Include="Raytheon.Instruments.DCPwr.Contracts" Version="2.7.0" />
<PackageReference Include="Raytheon.Common" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PowerSupplySystemSim\PowerSupplySystemSim.csproj" />
</ItemGroup>
<!-- Copy all *.dlls and *.pdb in the output folder to a temp folder -->
<Target Name="CopyFiles" AfterTargets="AfterBuild">
<ItemGroup>
<FILES_1 Include="$(OutDir)*.dll" />
<FILES_2 Include="$(OutDir)*.pdb" />
</ItemGroup>
<Copy SourceFiles="@(FILES_1)" DestinationFolder="$(HalTempFolder)" />
<Copy SourceFiles="@(FILES_2)" DestinationFolder="$(HalTempFolder)" />
</Target>
</Project>

View File

@@ -0,0 +1,140 @@
// **********************************************************************************************************
// PowerSupplySystemKeysightFactory.cs
// 2/20/2023
// NGI - Next Generation Interceptor
//
// Contract No. HQ0856-21-C-0003/1022000209
//
// THIS DOCUMENT DOES NOT CONTAIN TECHNOLOGY OR TECHNICAL DATA CONTROLLED UNDER EITHER THE U.S.
// INTERNATIONAL TRAFFIC IN ARMS REGULATIONS OR THE U.S. EXPORT ADMINISTRATION REGULATIONS.
//
// 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.
//
// UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
//
// DESTRUCTION NOTICE: FOR CLASSIFIED DOCUMENTS FOLLOW THE PROCEDURES IN DOD 5220.22-M,
// NATIONAL INDUSTRIAL SECURITY PROGRAM OPERATING MANUAL, FEBRUARY 2006,
// INCORPORATING CHANGE 1, MARCH 28, 2013, CHAPTER 5, SECTION 7, OR DODM 5200.01-VOLUME 3,
// DOD INFORMATION SECURITY PROGRAM: PROTECTION OF CLASSIFIED INFORMATION, ENCLOSURE 3,
// SECTION 17. FOR CONTROLLED UNCLASSIFIED INFORMATION FOLLOW THE PROCEDURES IN DODM 5200.01-VOLUME 4,
// INFORMATION SECURITY PROGRAM: CONTROLLED UNCLASSIFIED INFORMATION.
//
// CONTROLLED BY: MISSILE DEFENSE AGENCY
// CONTROLLED BY: GROUND-BASED MIDCOURSE DEFENSE PROGRAM OFFICE
// CUI CATEGORY: CTI
// DISTRIBUTION/DISSEMINATION CONTROL: F
// POC: Alex Kravchenko (1118268)
// **********************************************************************************************************
using NLog;
using Raytheon.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Reflection;
namespace Raytheon.Instruments
{
[ExportInstrumentFactory(ModelNumber = "PowerSupplySystemKeysightFactory")]
public class PowerSupplySystemKeysightFactory : IInstrumentFactory
{
/// <summary>
/// The supported interfaces
/// </summary>
private readonly List<Type> _supportedInterfaces = new List<Type>();
private ILogger _logger;
private readonly IConfigurationManager _configurationManager;
private const string DefaultConfigPath = @"C:\ProgramData\Raytheon\InstrumentManagerService";
private static string DefaultPath;
public PowerSupplySystemKeysightFactory(string defaultConfigPath = DefaultConfigPath)
: this(null, defaultConfigPath)
{
}
/// <summary>
/// COECommDeviceInstrumentFactory injection constructor
/// </summary>
/// <param name="configManager"></param>
/// <param name="simEngine"></param>
/// <param name="logger"></param>
[ImportingConstructor]
public PowerSupplySystemKeysightFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
[Import(AllowDefault = true)] string defaultConfigPath = null)
{
DefaultPath = defaultConfigPath;
if (LogManager.Configuration == null)
{
var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\\nlog.config");
}
_configurationManager = configManager ?? GetConfigurationManager();
_supportedInterfaces.Add(typeof(IPowerSupplySystem));
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IInstrument GetInstrument(string name)
{
try
{
_logger = LogManager.GetLogger(name);
return new PowerSupplySystemKeysight(name, _configurationManager, _logger);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public object GetInstrument(string name, bool simulateHw)
{
try
{
_logger = LogManager.GetLogger(name);
if (simulateHw)
return new PowerSupplySystemSim(name, _configurationManager, _logger);
else
return new PowerSupplySystemKeysight(name, _configurationManager, _logger);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Gets supported interfaces
/// </summary>
/// <returns></returns>
public ICollection<Type> GetSupportedInterfaces()
{
return _supportedInterfaces.ToArray();
}
/// <summary>
/// returns configuration based on the predefined path or default path c:/ProgramData/Raytheon/InstrumentManagerService
/// </summary>
/// <returns></returns>
private static IConfigurationManager GetConfigurationManager()
{
return string.IsNullOrEmpty(DefaultPath) ? new RaytheonConfigurationManager() : new RaytheonConfigurationManager(DefaultPath);
}
}
}