using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.Event; using Aitex.Core.RT.Log; using Aitex.Core.RT.OperationCenter; using Aitex.Core.RT.SCCore; using Aitex.Core.RT.Tolerance; using Aitex.Core.Util; using MECF.Framework.Common.Communications; using MECF.Framework.Common.Device.Bases; using MECF.Framework.Common.Equipment; using MECF.Framework.Common.Utilities; namespace JetVirgoPM.Devices { public enum SMCChillerState { ON = 0, OFF, Connected, Disconnected, Unknown, ERROR } static class SMCChillerMessage { public const string Start = ":"; public const string End = "\r\n"; public const string SlaveAddress = "01"; public const string Function_R_M = "03"; public const string Function_W_S = "06"; public const string Function_W_M = "10"; public const string Function_RW_M = "17"; public const string Data_Address_GetTemp = "0000"; public const string Data_Address_GetPress = "0002"; public const string Data_Address_GetElec = "0003"; public const string Data_Address_Status1 = "0004"; public const string Data_Address_Alarm1 = "0005"; public const string Data_Address_Alarm2 = "0006"; public const string Data_Address_Alarm3 = "0007"; public const string Data_Address_Status2 = "0009"; public const string Data_Address_SetTemp = "000B"; public const string Data_Address_SetRun = "000C"; public const string SET_ON = SlaveAddress + Function_W_S + Data_Address_SetRun + "0001"; public const string SET_OFF = SlaveAddress + Function_W_S + Data_Address_SetRun + "0000"; public const string SET_TEMP = SlaveAddress + Function_W_S + Data_Address_SetTemp; public const string GET_ALL = SlaveAddress + Function_R_M + Data_Address_GetTemp + "000B"; } class SMCChiller : ChillerBase { public SMCChillerState StatusSMC { get; set; } private float _controlTcSetPoint; private float _controlTcFeedback; private readonly double _scSetPointLimitMax; private readonly double _scSetPointLimitMin; private readonly string _PortNum = "COM92"; private readonly AsyncSerialPort _serial; private readonly DeviceTimer _timerQueryStatus = new DeviceTimer(); private const ushort CHK_ST_INTERVAL = 300; bool[] Statusflag = new bool[32]; //状态On or Off bool[] LastStatusflag = new bool[32]; //上次循环 状态On or Off bool[] Alarmflag = new bool[48]; //报警On or Off bool[] LastAlarmflag = new bool[48]; //上次循环 报警On or Off string[] AlarmMsg = new string[48]; //报警消息 bool[] NeedToStop = new bool[48]; //报警是否强制停机 private void SetAlarmMsg() { AlarmMsg[0] = "Low level in tank"; AlarmMsg[1] = "High circulating fluid discharge temp"; AlarmMsg[2] = "Circulating fluid discharge temp. rise"; AlarmMsg[3] = "Circulating fluid discharge temp."; AlarmMsg[4] = "High circulating fluid return temp."; AlarmMsg[5] = "High circulating fluid discharge pressure"; AlarmMsg[6] = "Abnormal pump operation"; AlarmMsg[7] = "Circulating fluid discharge pressure rise"; AlarmMsg[8] = "Circulating fluid discharge pressure drop"; AlarmMsg[9] = "High compressor intake temp."; AlarmMsg[10] = "Low compressor intake temp."; AlarmMsg[11] = "Low super heat temperature"; AlarmMsg[12] = "High compressor discharge pressure"; //AlarmMsg[13] Unused AlarmMsg[14] = "Refrigerant circuit pressure (high pressure side) drop"; AlarmMsg[15] = "Refrigerant circuit pressure (low pressure side) rise"; AlarmMsg[16] = "Refrigerant circuit pressure (low pressure side) drop"; AlarmMsg[17] = "Compressor overload"; AlarmMsg[18] = "Communication error"; AlarmMsg[19] = "Memory error"; AlarmMsg[20] = "DC line fuse cut"; AlarmMsg[21] = "Circulating fluid discharge temp. sensor failure"; AlarmMsg[22] = "Circulating fluid return temp. sensor failure"; AlarmMsg[23] = "Compressor intake temp. sensor failure"; AlarmMsg[24] = "Circulating fluid discharge pressure sensor failure"; AlarmMsg[25] = "Compressor discharge pressure sensor failure"; AlarmMsg[26] = "Compressor intake pressure sensor failure"; AlarmMsg[27] = "Maintenance of pump"; AlarmMsg[28] = "Maintenance of fan motor"; AlarmMsg[29] = "Maintenance of compressor"; AlarmMsg[30] = "Contact input 1 signal detection alarm"; AlarmMsg[31] = "Contact input 2 signal detection alarm"; AlarmMsg[32] = "Water leakage"; AlarmMsg[33] = "Electric resistivity/conductivity level rise"; AlarmMsg[34] = "Electric resistivity/conductivity level drop"; AlarmMsg[35] = "Electric resistivity/conductivity sensor error"; //AlarmMsg[36] Unused NeedToStop[0] = true; NeedToStop[1] = true; NeedToStop[4] = true; NeedToStop[5] = true; NeedToStop[6] = true; NeedToStop[9] = true; NeedToStop[10] = true; NeedToStop[11] = true; NeedToStop[12] = true; NeedToStop[14] = true; NeedToStop[15] = true; NeedToStop[16] = true; NeedToStop[17] = true; NeedToStop[19] = true; NeedToStop[20] = true; NeedToStop[21] = true; NeedToStop[22] = true; NeedToStop[23] = true; NeedToStop[24] = true; NeedToStop[25] = true; NeedToStop[26] = true; NeedToStop[30] = true; NeedToStop[31] = true; NeedToStop[32] = true; } public double SetPointLimitMax { get { return _scSetPointLimitMax; ; } } public double SetPointLimitMin { get { return _scSetPointLimitMin; ; } } [Subscription(AITChillerProperty.IsRunning)] public override bool IsRunning { get { return StatusSMC == SMCChillerState.ON; } } [Subscription(AITChillerProperty.IsError)] public override bool IsError { get { return StatusSMC == SMCChillerState.ERROR || StatusSMC == SMCChillerState.Disconnected; } } [Subscription(AITChillerProperty.ControlTcSetPoint)] public float ControlTcSetPoint { get { return _controlTcSetPoint; } } [Subscription(AITChillerProperty.ControlTcFeedback)] public float ControlTcFeedback { get { return _controlTcFeedback; } } [Subscription(AITChillerProperty.CoolantInletTempFeedback)] public float CoolantInletTcFeedback { get { if (Name == "Chiller2") { return GetAiValue($"{Module}.AI_2_CoolantInletTemp"); } return GetAiValue($"{Module}.AI_1_CoolantInletTemp"); } } [Subscription(AITChillerProperty.CoolantOutletTempFeedback)] public float CoolantOutletTcFeedback { get { if (Name == "Chiller2") { return GetAiValue($"{Module}.AI_2_CoolantOutletTemp"); } return GetAiValue($"{Module}.AI_1_CoolantOutletTemp"); } } public override float Temperature { get { return CoolantOutletTcFeedback; } } public override AITChillerData DeviceData { get { AITChillerData deviceData = new AITChillerData { Module = Module, DeviceName = Name, DeviceModule = Module, DeviceSchematicId = DeviceID, DisplayName = Display, IsError = false, IsWarning = false, IsPowerOn = IsRunning, ScaleMax = SetPointLimitMax, ScaleMin = SetPointLimitMin, SetPoint = ControlTcSetPoint, FeedBack = CoolantOutletTcFeedback, CoolantInletFeedBack = CoolantInletTcFeedback, CoolantOutletFeedBack = CoolantOutletTcFeedback, Unit = "℃", }; return deviceData; } } private SCConfigItem _scAlarmRange; private SCConfigItem _scEnableAlarm; private SCConfigItem _scAlarmTime; private SCConfigItem _scWarningTime; private SCConfigItem _scWarningRange; private ToleranceChecker _toleranceChecker = new ToleranceChecker(); private ToleranceChecker _toleranceWarningChecker = new ToleranceChecker(); // --------------------------Constructor----------------------- // public SMCChiller(ModuleName mod,string name) : base(mod.ToString(), name, name, "") { //if (Module == "PMB" && SC.GetValue($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue($"PMB.Chiller.EnableChiller")) return; _PortNum = SC.GetStringValue($"{mod}.{Name}.Port"); _scSetPointLimitMax = SC.GetValue($"{Module}.{Name}.SetPointLimitMax"); _scSetPointLimitMin = SC.GetValue($"{Module}.{Name}.SetPointLimitMin"); StatusSMC = SMCChillerState.Unknown; _scEnableAlarm = SC.GetConfigItem($"{Module}.{Name}.EnableToleranceAlarm"); _scAlarmRange = SC.GetConfigItem($"{Module}.{Name}.ToleranceAlarmRange"); _scAlarmTime = SC.GetConfigItem($"{Module}.{Name}.ToleranceAlarmTime"); _scWarningRange = SC.GetConfigItem($"{Module}.{Name}.ToleranceWarningRange"); _scWarningTime = SC.GetConfigItem($"{Module}.{Name}.ToleranceWarningTime"); _serial = new AsyncSerialPort(_PortNum, 9600, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, "\r\n", true); } public override bool Initialize() { base.Initialize(); //if (Module == "PMB" && SC.GetValue($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue($"PMB.Chiller.EnableChiller")) //{ // OP.Subscribe($"{Module}.{RtOperation.SetPMBChillerState}", (function, args) => // { // StatusSMC = Convert.ToBoolean(args[0]) ? SMCChillerState.ON : SMCChillerState.OFF; // return true; // }); // return true; //} SetAlarmMsg(); if (!_serial.Open()) { StatusSMC = SMCChillerState.Disconnected; EV.PostAlarmLog(this.Module, "SMC Chiller串口无法打开"); return false; } StatusSMC = SMCChillerState.Connected; _serial.OnDataChanged += OnPortDataChanged; _serial.OnErrorHappened += OnErrorOccurred; _timerQueryStatus.Start(CHK_ST_INTERVAL); return true; } private void OnPortDataChanged(string obj) { try { if (string.IsNullOrEmpty(obj)) { LOG.Error($"[{Module}] smc chiller message IsNullOrEmpty"); return; } string cmd = obj.Split(new char[] { '\r', '\n' })[0]; if (ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(cmd.Substring(1, cmd.Length - 3))).ToString("X2") != cmd.Substring(cmd.Length - 2)) //LRC check { LOG.Error($"[{Module}] smc chiller message LRC check error"); return; } switch (cmd.Substring(3, 2)) //function code { case "03": //返回的寄存器内容 if (cmd.Substring(5, 2) == "16") //返回了全部11个寄存器、22个Byte数据 { for (int iRegisterNo = 0; iRegisterNo < 11; iRegisterNo++) { string sBuf = cmd.Substring(4 * iRegisterNo + 7, 4); int iBuf = 0; for (int i = 3; i > -1; i--) { iBuf += Convert.ToInt32(sBuf.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i); } switch (iRegisterNo) { case 0: //Circulating fluid discharge temperature _controlTcFeedback = (float)iBuf / 10; break; case 4: //Status flag 1 for (int i = 0; i < 16; i++) { Statusflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf); } break; case 5: //Alarm flag 1 for (int i = 0; i < 16; i++) { Alarmflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf); } break; case 6: //Alarm flag 2 for (int i = 0; i < 16; i++) { Alarmflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf); } break; case 7: //Alarm flag 3 for (int i = 0; i < 16; i++) { Alarmflag[i + 32] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf); } break; case 9: //Status flag 2 for (int i = 0; i < 16; i++) { Statusflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf); } break; } } } break; case "06": //寄存器的写成功消息 break; } } catch (Exception ex) { LOG.Error($"[{Module}] smc chiller error: [{ex.Message}]"); } } private void OnErrorOccurred(string obj) { StatusSMC = SMCChillerState.ERROR; LOG.Error($"[{Module}] smc chiller error: [{obj}]"); } public override void Monitor() { try { //if (Module == "PMB" && SC.GetValue($"PMB.Chiller1.ChillerSameWithPMA") && SC.GetValue($"PMB.Chiller1.EnableChiller")) return; if (!SC.GetValue($"{Module}.{Name}.EnableChiller")) return; if (_timerQueryStatus.IsTimeout() && this.StatusSMC != SMCChillerState.ERROR) { this.SendCmd(SMCChillerMessage.GET_ALL); _timerQueryStatus.Start(CHK_ST_INTERVAL); } if (Statusflag[0]) this.StatusSMC = SMCChillerState.ON; else this.StatusSMC = SMCChillerState.OFF; for (int i = 0; i < 48; i++) { if (Alarmflag[i]) this.StatusSMC = SMCChillerState.ERROR; if (Alarmflag[i] && !LastAlarmflag[i])//取报警信号上升沿 { //报警一次 if (NeedToStop[i]) EV.PostAlarmLog(this.Module, AlarmMsg[i]); else EV.PostWarningLog(this.Module, AlarmMsg[i]); } LastAlarmflag[i] = Alarmflag[i]; } if (IsRunning && _scEnableAlarm != null && _scEnableAlarm.BoolValue) { var range = _scAlarmRange.IntValue; var time = _scAlarmTime.IntValue; _toleranceChecker.Monitor(_controlTcFeedback, _controlTcSetPoint - Math.Abs(range), _controlTcSetPoint + Math.Abs(range), time); if (_toleranceChecker.Trig) { EV.PostAlarmLog(Module, Display + $" temperature out of tolerance in {time:0} seconds"); } } else { _toleranceChecker.RST = true; } if (IsRunning && _scEnableAlarm != null && _scEnableAlarm.BoolValue) { var range = _scWarningRange.IntValue; var time = _scWarningTime.IntValue; _toleranceWarningChecker.Monitor(_controlTcFeedback, _controlTcSetPoint - Math.Abs(range), _controlTcSetPoint + Math.Abs(range), time); if (_toleranceWarningChecker.Trig) { EV.PostWarningLog(Module, Display + $" temperature out of tolerance in {time:0} seconds"); } } else { _toleranceWarningChecker.RST = true; } } catch (Exception ex) { throw ex; } } private void SendCmd(string str) { //if (Module == "PMB" && SC.GetValue($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue($"PMB.Chiller.EnableChiller")) return; //消息通用格式:开头 + 内容 + LRC + 结尾 _serial?.Write(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n"); } public override void Reset() { _toleranceChecker.RST = true; _toleranceWarningChecker.RST = true; } public override void Terminate() { _serial?.Close(); } public override void SetChillerTemp(float value, float offset) { int isettemp = Convert.ToInt16((value - offset) * 10); SendCmd(SMCChillerMessage.SET_TEMP + isettemp.ToString("X4")); _controlTcSetPoint = value; } public override void SetChillerOnOff(bool on) { SendCmd(on ? SMCChillerMessage.SET_ON : SMCChillerMessage.SET_OFF); } } }