public class EAPowerControl
{
#region 常用变量
/// <summary>
/// 输出日志接口
/// </summary>
private IPrintAction printAction;
private static readonly string readError = "SYST:ERR?"; // 读取错误代码
private static readonly string noError = "0,\"No error\""; // 正确信息
private static int waitingSecond = 100;
private static string ioAddress = DevicesConfig.Default.Config.ConnectName;//"dongzhou";//System.Configuration.ConfigurationManager.AppSettings["smartdac.ip"];
private static string connectType = DevicesConfig.Default.Config.ConnectType;//"COM";//System.Configuration.ConfigurationManager.AppSettings["DAQ970.Connect.Type"];
/// <summary>
/// 设置电压 最大值 和最小值 把 MIN 替换成 数字即可 设置。 SOURce:VOLTage 10
/// </summary>
private static string setVOLTageMin = "SOURce:VOLTage MIN";
private static string setVOLTageMax = "SOURce:VOLTage MAX";
/// <summary>
/// 设置电流 最大值 和最小值 把 MIN 替换成 数字即可 设置。SOURce:CURRent 10
/// </summary>
private static string setCURRentMin = "SOURce:CURRent MIN";
private static string setCURRentMax = "SOURce:CURRent Max";
/// <summary>
/// 设置功率 最大值 和最小值 把 MIN 替换成 数字即可 设置。 SOURce:POWer 10
/// </summary>
private static string setPOWerMin = "SOURce:POWer MIN";
private static string setPOWerMax = "SOURce:POWer MAX";
/// <summary>
/// 输出 开关
/// </summary>
private static string outPutOFF = "OUTPut OFF";
private static string outPutON = "OUTPut ON";
/// <summary>
/// 输入开关
/// </summary>
private static string inPutOFF = "OUTPut OFF";
private static string inPutON = "OUTPut ON";
/// <summary>
///关锁解锁 锁定了不能 远程。
/// </summary>
private static string lockOff = "SYSTem:LOCK OFF";
private static string lockOn = "SYSTem:LOCK ON";
/// <summary>
///系统:通信:协议:SCPI启用
/// </summary>
private static string SCPIEnable = " SYSTem:COMMunicate:PROTocol:SCPI ENABLE"; // 启用
private static string SCPIDisable = "SYSTem:COMMunicate:PROTocol:SCPI DISABLE";// 禁用 scpi
/// <summary>
/// 获取当前设置电压
/// </summary>
private static string getVOLTage = "VOLTage?";// MEASure:VOLTage? 测量电压 获取当前运行电压
/// <summary>
/// 获取当前设置电流
/// </summary>
private static readonly string getCURRent = "CURRent?";// MEASure:CURRent? 测量电压 获取当前运行电流
/// <summary>
/// 获取当前设置 功率
/// </summary>
private static readonly string getPOWer = "POWer?";// MEASure:POWer? 测量电压 获取当前运行功率
#endregion
private static EAPowerControl eA_PowerControl { get; set; }
/// <summary>
/// 单例
/// </summary>
public static EAPowerControl EA_Control
{
get
{
Connect();
return Singleton<EAPowerControl>.Instance;
}
}
#region SCPI 命令
/// <summary>
/// usbSession
/// </summary>
private static MessageBasedSession iMessageSession;
/// <summary>
/// IMessageBasedFormattedIO
/// </summary>
private static IMessageBasedFormattedIO io { get; set; }
public static bool Connect()
{
//check null
if (iMessageSession == null)
{
switch (connectType)
{
case "COM":
{
iMessageSession = new SerialSession(ioAddress);
}
break;
case "USB":
{
iMessageSession = new UsbSession(ioAddress);
}
break;
case "TCP":
{
iMessageSession = new TcpipSession(ioAddress);
}
break;
default:
throw new Exception("EA电源配置不正确");
break;
}
//TimeoutMilliseconds
iMessageSession.TimeoutMilliseconds = 3000;
io = iMessageSession.FormattedIO;
try
{
var ret = EA_Control.SendCommand(lockOn); // 打开远程
if (string.IsNullOrEmpty(ret))
{
ret = EA_Control.SendCommand(setPOWerMax); // 设置功率
if (string.IsNullOrEmpty(ret))
{
ret = EA_Control.SendCommand(setCURRentMax);// 设置电流
if (!string.IsNullOrEmpty(ret))
{
Util.LogUtil.Error("ERROR EA电源设置电流失败" + ret);
throw new Exception($"EA电源设置电流失败 {ret}");
}
}
else
{
Util.LogUtil.Error("ERROR EA电源设置功率失败" + ret);
throw new Exception($"EA电源设置功率失败 {ret}");
}
}
else
{
Util.LogUtil.Error("ERROR EA电源配置不正确" + ret);
throw new Exception("EA电源配置不正确");
}
}
catch (Exception ex)
{
Util.LogUtil.Error("ERROR EA电源配置不正确" + ex.Message);
throw new Exception("EA电源配置不正确");
}
}
return true;
}
public void DisConnect()
{
try
{
//check null
iMessageSession?.Dispose();
iMessageSession = null;
}
catch (Exception ex)
{
Util.LogUtil.Error("ERROR DisConnect" + ex.ToString());
}
}
public string Read()
{
//read
string content = SendQuery("READ?");
return content;
}
/// <summary>
/// 发送查询命令
/// </summary>
/// <param name="command"></param>
/// <returns> 正确返回 返回 接收信息 ,错误 返回错误 信息</returns>
public string SendQuery(string command)
{
int counter = 3;
while (counter > 0)
{
try
{
Stopwatch sw = new Stopwatch();
sw.Start();
Util.LogUtil.Debug($"SendQuery:" + command);
//PrintfAndFlush
io.PrintfAndFlush(command);
string answer = io.ReadLine();
sw.Stop();
//log time
Util.LogUtil.Debug($"SendQuery:" + command + "\t" + sw.ElapsedMilliseconds);
// Console.WriteLine($"SendQuery:" + command + "\t" + sw.ElapsedMilliseconds);
//log receive
Util.LogUtil.Debug($"Receive:" + answer);
Thread.Sleep(waitingSecond);
io.PrintfAndFlush(readError);
Thread.Sleep(waitingSecond);
string error = io.ReadLine();
//Contains
if (error.Contains(noError))
{
return answer;
}
else if (counter == 1)
{
Util.LogUtil.Error("ERROR SendQuery: " + error);
return "";
}
}
catch (ThreadAbortException)
{
return "";
}
catch (Exception ex)
{
//log error
Util.LogUtil.Error("ERROR SendQuery: " + ex.ToString());
}
finally
{
counter -= 1;
}
}
return "";
}
/// <summary>
/// 发送命令
/// </summary>
/// <param name="command"></param>
/// <returns> 正确返回 空 错误 返回错误信息 </returns>
public string SendCommand(string command)
{
//log
Util.LogUtil.Debug("SendCommand:" + command);
int counter = 3; // 默认三次响应
while (counter > 0)
{
//mInstrument.WriteString(command);
io.PrintfAndFlush(command);
Thread.Sleep(waitingSecond); //500
io.PrintfAndFlush(readError);
Thread.Sleep(waitingSecond);
string error = io.ReadLine();
//Contains
if (error.Contains(noError))
{
break;
}
else if (counter == 1)
{
string errorInfo = "ERROR 902: " + error;
Util.LogUtil.Error(errorInfo);
throw new Exception(errorInfo);
}
counter -= 1;
}
Thread.Sleep(500);
return "";
}
#endregion
#region 电源控制
/// <summary>
/// 打开 EA 程控电源
/// </summary>
/// <param name="line">通道 不需要 适配 接口</param>
/// <param name="voltage">电压 </param>
/// <param name="name"> 名称 不需要适配 接口< </param>
/// <returns></returns>
public bool OpenPower(int line, double voltage, string name)
{
var com = setVOLTageMin.Replace("MIN", voltage.ToString());
var ret = SendCommand(com);
if (!string.IsNullOrEmpty(ret))
{
Util.LogUtil.Debug($"设置EA程控失败,错误原因:{ret}");
return false;
}
ret = SendCommand(outPutON);
if (!string.IsNullOrEmpty(ret))
{
Util.LogUtil.Debug($"设置EA程控失败,错误原因:{ret}");
return false;
}
return true;
}
/// <summary>
/// 关闭程控电源
/// </summary>
/// <param name="line"> 线路 适配 接口 </param>
/// <param name="name"> </param>
/// <returns></returns>
public bool ClosePower(int line, string name)
{
var ret = SendCommand(outPutOFF);
if (!string.IsNullOrEmpty(ret))
{
Util.LogUtil.Debug($"设置EA程控失败,错误原因:{ret}");
return false;
}
return true;
}
/// <summary>
/// 重置程控电源
/// </summary>
/// <param name="line"></param>
/// <param name="voltage"></param>
/// <param name="name"></param>
/// <returns></returns>
public bool RestartPower(int line, double voltage, string name, string path)
{
var ret = SendCommand(outPutOFF);
if (!string.IsNullOrEmpty(ret))
{
Util.LogUtil.Debug($"设置EA程控失败,错误原因:{ret}");
return false;
}
if (!string.IsNullOrEmpty(path))
FileUtil.WriteFile(path, $"power{line}_off:{GlobalAPI.Global.Timer.Elapsed.TotalMilliseconds}", true);
printAction.PrintAction("EA_POWER", $"Power Line:{line} Closed");
Thread.Sleep(3000);//等待3秒 电压降下
ret = SendCommand(outPutON);
if (!string.IsNullOrEmpty(ret))
{
Util.LogUtil.Debug($"设置EA程控失败,错误原因:{ret}");
return false;
}
var strvoltage = SendQuery(getVOLTage);
if (!string.IsNullOrEmpty(path))
FileUtil.WriteFile(path, $"power{line}_on:{GlobalAPI.Global.Timer.Elapsed.TotalMilliseconds}", true);
printAction.PrintAction("EA_POWER", $"Power Line:{line} voltage set {strvoltage} ");
return true;
}
public string GetPowerVoltage()
{
var com = $" MEASure:{getVOLTage}";
var ret = SendQuery(com);
return ret;
}
/// <summary>
/// 打开 EA 程控电源
/// </summary>
/// <param name="line">通道 不需要</param>
/// <param name="voltage">电压 </param>
/// <param name="name"> 名称 不需要 </param>
/// <returns></returns>
public bool SetAcc(int line, double current, string name)
{
var com = setCURRentMin.Replace("MIN", current.ToString());
var ret = SendCommand(com);
if (!string.IsNullOrEmpty(ret))
{
Util.LogUtil.Debug($"设置EA程控失败,错误原因:{ret}");
return false;
}
ret = SendCommand(outPutON);
if (!string.IsNullOrEmpty(ret))
{
Util.LogUtil.Debug($"设置EA程控失败,错误原因:{ret}");
return false;
}
return true;
}
#endregion
}