ソースを参照

update SMCChillerDrive && SMCShareChiller

zhouhr 1 年間 前
コミット
93fd866dbe
共有2 個のファイルを変更した633 個の追加0 個の削除を含む
  1. 248 0
      Venus/Venus_RT/Devices/SMCChillerDrive.cs
  2. 385 0
      Venus/Venus_RT/Devices/SMCShareChiller.cs

+ 248 - 0
Venus/Venus_RT/Devices/SMCChillerDrive.cs

@@ -0,0 +1,248 @@
+using Aitex.Core.Common.DeviceData;
+using Aitex.Core.RT.Device;
+using Aitex.Core.RT.Log;
+using Aitex.Core.RT.SCCore;
+using Aitex.Core.RT.Tolerance;
+using Aitex.Core.Util;
+using MECF.Framework.Common.Communications;
+using MECF.Framework.Common.Equipment;
+using MECF.Framework.Common.Utilities;
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using static Mono.Security.X509.X520;
+using Venus_Core;
+using System.Diagnostics;
+
+namespace Venus_RT.Devices
+{
+    /// <summary>
+    /// 描述:该类用于对单Chiller Controller下多设备共享 的驱动 只单一的用于串口的连接
+    /// 需要存储的变量及说明
+    /// Statusflag:与chiller的开关有关
+    /// Alarmflag: 报警
+    /// chillerIsOn:正在进行
+    /// ControlTcFeedback:返回值
+    /// </summary>
+
+    public class SMCChillerDrive
+    {
+
+        public SMCChillerState StatusSMC { get; set; }
+        private readonly string _PortNum = "COM92";
+        private readonly AsyncSerialPort _serial;
+
+        BlockingCollection<string> blockingCollection = new BlockingCollection<string>();
+
+        public string PortName => _PortNum;
+
+        private Dictionary<string,string> _address2Module;//不正确 应该以字典的形式 以头去打印
+        private Dictionary<string, ChillerData> _addressQuery;
+        private ModuleName Module;//不正确 应该以字典的形式 以头去打印
+
+        // --------------------------Constructor-----------------------
+        //
+        public SMCChillerDrive(string Port)
+        {
+            _PortNum = Port;
+            StatusSMC = SMCChillerState.Unknown;
+
+            _serial = new AsyncSerialPort(_PortNum, 9600, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, "\r\n", true);
+
+            if (!_serial.Open())
+            {
+                StatusSMC = SMCChillerState.Disconnected;
+                LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"SMC Chiller串口:{_PortNum}无法打开");
+            }
+
+            StatusSMC = SMCChillerState.Connected;
+
+            _serial.OnDataChanged += OnPortDataChanged;
+            _serial.OnErrorHappened += OnErrorOccurred;
+
+            _address2Module = new Dictionary<string, string>();
+            _addressQuery = new Dictionary<string, ChillerData>();
+
+            Task.Run(() =>
+            {
+                foreach (var data in blockingCollection.GetConsumingEnumerable())
+                {
+                    _serial?.Write(data);
+                    System.Threading.Thread.Sleep(500);
+                }
+            });
+
+        }
+
+        private void OnPortDataChanged(string obj)
+        {
+            try
+            {
+                if (SC.GetValue<bool>("System.IsSimulatorMode"))
+                {
+                    //Statusflag[0] = true;
+
+                    //if (_controlTcSetPoint <= SetPointLimitMax / 2 && _controlTcSetPoint >= SetPointLimitMin)
+                    //    _controlTcFeedback = _controlTcSetPoint;
+                    return;
+                }
+                if (string.IsNullOrEmpty(obj))
+                {
+                    LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller message IsNullOrEmpty");
+                    return;
+                }
+                string cmd = obj.Split(new char[] { '\r', '\n' })[0];
+
+                //注意 数据开头有":"
+                //地址位 决定了是哪个chamber的哪个type
+                ChillerData chillerData = null;
+                ModuleName module = ModuleName.System;
+                if (_address2Module.ContainsKey(cmd.Substring(1, 2)) && _addressQuery.ContainsKey(cmd.Substring(1, 2)))
+                {
+                    _addressQuery[cmd.Substring(1, 2)] = chillerData;
+                    module = ModuleHelper.Converter(_address2Module[cmd.Substring(1, 2)].Split('.')[0]);
+                }
+                else
+                {
+                    LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}]:address => {cmd.Substring(1, 2)} not in Dictionary");
+                    return;
+                }
+
+                //若为system或null 说明dic中无value或value无效
+                Debug.Assert(chillerData != null && module != ModuleName.System);
+
+                if (ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(cmd.Substring(1, cmd.Length - 3))).ToString("X2") !=
+                    cmd.Substring(cmd.Length - 2))  //LRC check
+                {
+                    LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{_PortNum}] 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
+                                        chillerData.ControlTcFeedback = (float)iBuf / 10;
+                                        break;
+                                    case 4:     //Status flag 1
+                                        for (int i = 0; i < 16; i++)
+                                        {
+                                            chillerData.Statusflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
+                                        }
+                                        break;
+                                    case 5:     //Alarm flag 1
+                                        for (int i = 0; i < 16; i++)
+                                        {
+                                            chillerData.Alarmflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
+                                        }
+                                        break;
+                                    case 6:     //Alarm flag 2
+                                        for (int i = 0; i < 16; i++)
+                                        {
+                                            chillerData.Alarmflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
+                                        }
+                                        break;
+                                    case 7:     //Alarm flag 3
+                                        for (int i = 0; i < 16; i++)
+                                        {
+                                            chillerData.Alarmflag[i + 32] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
+                                        }
+                                        break;
+                                    case 9:     //Status flag 2
+                                        for (int i = 0; i < 16; i++)
+                                        {
+                                            chillerData.Statusflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
+                                        }
+                                        break;
+                                }
+                            }
+                        }
+                        _addressQuery[cmd.Substring(1, 2)] = chillerData;
+                        break;
+                    case "06":  //寄存器的写成功消息
+                        break;
+                }
+            }
+            catch (Exception ex)
+            {
+                LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller error: [{ex.Message}]");
+            }
+        }
+
+
+        private void OnErrorOccurred(string obj)
+        {
+            StatusSMC = SMCChillerState.ERROR;
+            LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller error: [{obj}]");
+        }
+
+
+        public void SendCmd(string str)
+        {
+            //if (Module == "PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller")) return;
+            //消息通用格式:开头 + 内容 + LRC + 结尾
+            //_serial?.Write(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
+            blockingCollection.Add(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
+        }
+
+        public void AddAddress(string address, string chillertype)
+        {
+            //address => "02" chillertype => PMA.OutChiller
+            _address2Module.Add(address,chillertype);
+            _addressQuery.Add(address, new ChillerData());
+        }
+
+        public ChillerData QueryData(string address)
+        {
+            if (_addressQuery.ContainsKey(address))
+            {
+                return _addressQuery[address];
+            }
+            else
+            {
+                LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] => address:{address} not exist");
+                return new ChillerData();
+            }
+
+        }
+
+        public void Terminate()
+        {
+            _serial?.Close();
+        }
+    }
+
+    public class ChillerData
+    {
+        public bool[] Statusflag {get; set;}
+        public bool[] Alarmflag { get; set; }
+        public float ControlTcFeedback { get; set; }
+
+
+        public ChillerData()
+        {
+            Statusflag = new bool[32];
+            Alarmflag = new bool[48];
+            ControlTcFeedback = 0.0f;
+        }
+    }
+
+}

+ 385 - 0
Venus/Venus_RT/Devices/SMCShareChiller.cs

@@ -0,0 +1,385 @@
+using Aitex.Core.Common.DeviceData;
+using Aitex.Core.RT.Log;
+using Aitex.Core.RT.SCCore;
+using Aitex.Core.RT.Tolerance;
+using Aitex.Core.Util;
+using MECF.Framework.Common.Device.Bases;
+using MECF.Framework.Common.Equipment;
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Linq;
+using System.ServiceModel.Channels;
+using System.Text;
+using System.Threading.Tasks;
+using Venus_Core;
+
+namespace Venus_RT.Devices
+{
+    public class SMCShareChiller : ChillerBase
+    {
+        private SMCChillerDrive _driver;
+        private string _address;
+        private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
+        private const ushort CHK_ST_INTERVAL = 500;
+        bool[] Statusflag = new bool[32];
+        bool[] LastStatusflag = new bool[32];
+        bool[] Alarmflag = new bool[48];
+        bool[] LastAlarmflag = new bool[48];
+        string[] AlarmMsg = new string[48];
+        bool[] NeedToStop = new bool[48];
+        private ToleranceChecker _toleranceChecker = new ToleranceChecker();
+        private float _controlTcSetPoint;
+        private float _controlTcFeedback;
+        private readonly double _scSetPointLimitMax;
+        private readonly double _scSetPointLimitMin;
+        private SCConfigItem _scAlarmRange;
+        private SCConfigItem _scEnableAlarm;
+        private SCConfigItem _scAlarmTime;
+        private bool chillerIsOn;
+        JetChamber jetChamber;
+        private SMCMutiChillerMessage message;
+        public SMCChillerState StatusSMC{get; private set;}
+
+
+        public double SetPointLimitMax
+        {
+            get
+            {
+                return _scSetPointLimitMax; ;
+            }
+        }
+
+        public double SetPointLimitMin
+        {
+            get
+            {
+                return _scSetPointLimitMin; ;
+            }
+        }
+
+        [Subscription(AITChillerProperty.IsRunning)]
+        public override bool IsRunning
+        {
+            get
+            {
+                return chillerIsOn;
+            }
+        }
+
+        [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 override float CoolantInletTcFeedback
+        {
+            get
+            {
+                if (jetChamber == JetChamber.VenusSE)
+                {
+                    return GetAiValue($"{Module}.AI_Coolant_Inlet_Temp");
+
+                }
+                else
+                {
+                    return 0;
+                }
+            }
+        }
+
+
+        [Subscription(AITChillerProperty.CoolantOutletTempFeedback)]
+        public override float CoolantOutletTcFeedback
+        {
+            get
+            {
+                if (jetChamber == JetChamber.VenusSE)
+                {
+                    return GetAiValue($"{Module}.AI_Coolant_Outlet_Temp");
+
+                }
+                else
+                {
+                    return 0;
+                }
+            }
+        }
+
+        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;
+            }
+        }
+        public SMCShareChiller(ModuleName mod, string name) : base(mod.ToString(), name, name, "")
+        {
+            _address = SC.GetStringValue($"{mod}.{Name}.Address");
+            _driver = ChillerFactory.QuoteDrive(SC.GetStringValue($"{mod}.{Name}.Port"), _address, $"{mod}.{Name}");
+            _scSetPointLimitMax = SC.GetValue<double>($"{mod}.{name}.SetPointLimitMax");
+            _scSetPointLimitMin = SC.GetValue<double>($"{mod}.{name}.SetPointLimitMin");
+            _scEnableAlarm = SC.GetConfigItem($"{mod}.{name}.EnableToleranceAlarm");
+            _scAlarmRange = SC.GetConfigItem($"{mod}.{name}.ToleranceAlarmRange");
+            _scAlarmTime = SC.GetConfigItem($"{mod}.{name}.ToleranceAlarmTime");
+            jetChamber = (JetChamber)SC.GetValue<int>($"{mod}.ChamberType");
+            message = new SMCMutiChillerMessage(_address);
+            _timerQueryStatus.Start(CHK_ST_INTERVAL);
+            SetAlarmMsg();
+        }
+
+
+        public override void Monitor()
+        {
+            try
+            {
+                if (_timerQueryStatus.IsTimeout() && StatusSMC != SMCChillerState.ERROR)
+                {
+                    _driver.SendCmd(message.GET_ALL);
+                    _timerQueryStatus.Start(CHK_ST_INTERVAL);
+                }
+
+                if (_driver != null)
+                {
+                    Statusflag = _driver.QueryData(_address).Statusflag;
+                    Alarmflag = _driver.QueryData(_address).Alarmflag;
+                    _controlTcFeedback = _driver.QueryData(_address).ControlTcFeedback;
+                }
+
+
+                if (Statusflag[0])
+                {
+                    StatusSMC = SMCChillerState.ON;
+                    chillerIsOn = true;
+                }
+
+                else
+                {
+                    StatusSMC = SMCChillerState.OFF;
+                    chillerIsOn = false;
+                }
+
+                for (int i = 0; i < 48; i++)
+                {
+                    if (Alarmflag[i])
+                        StatusSMC = SMCChillerState.ERROR;
+                    if (Alarmflag[i] && !LastAlarmflag[i])//取报警信号上升沿
+                    {
+                        //报警一次
+                        if (NeedToStop[i])
+                            LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, AlarmMsg[i]);
+                        else
+                            LOG.Write(eEvent.WARN_DEVICE_CHILLER, 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)
+                    {
+                        LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, Display + $" temperature out of tolerance in {time:0} seconds");
+                    }
+                }
+
+                else
+                {
+                    _toleranceChecker.RST = true;
+                }
+
+            }
+            catch (Exception ex)
+            {
+                LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, ex.ToString());
+            }
+        }
+
+        
+
+        public override void Reset()
+        {
+            _toleranceChecker.RST = true;
+        }
+
+        public override void SetChillerTemp(float value, float offset)
+        {
+            int isettemp = Convert.ToInt16((value - offset) * 10);
+            _driver.SendCmd(message.SET_TEMP + isettemp.ToString("X4"));
+            _controlTcSetPoint = value;
+        }
+
+        public override void SetChillerOnOff(bool on)
+        {
+            _driver.SendCmd(on ? message.SET_ON : SMCChillerMessage.SET_OFF);
+        }
+
+        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 class SMCMutiChillerMessage
+    {
+        public string Start = ":";
+        public string End = "\r\n";
+        public string SlaveAddress = "00";
+        public string Function_R_M = "03";
+        public string Function_W_S = "06";
+        public string Function_W_M = "10";
+        public string Function_RW_M = "17";
+        public string Data_Address_GetTemp = "0000";
+        public string Data_Address_GetPress = "0002";
+        public string Data_Address_GetElec = "0003";
+        public string Data_Address_Status1 = "0004";
+        public string Data_Address_Alarm1 = "0005";
+        public string Data_Address_Alarm2 = "0006";
+        public string Data_Address_Alarm3 = "0007";
+        public string Data_Address_Status2 = "0009";
+        public string Data_Address_SetTemp = "000B";
+        public string Data_Address_SetRun = "000C";
+
+        public string SET_ON => SlaveAddress + Function_W_S + Data_Address_SetRun + "0001";
+        public string SET_OFF => SlaveAddress + Function_W_S + Data_Address_SetRun + "0000";
+        public string SET_TEMP => SlaveAddress + Function_W_S + Data_Address_SetTemp;
+        public string GET_ALL => SlaveAddress + Function_R_M + Data_Address_GetTemp + "000B";
+
+        public SMCMutiChillerMessage(string SlaveAddress)
+        {
+            this.SlaveAddress = SlaveAddress;
+        }
+    }
+
+    public class ChillerFactory
+    {
+        private static Dictionary<string, SMCChillerDrive> port2share = new Dictionary<string, SMCChillerDrive>();
+        
+        public static SMCChillerDrive QuoteDrive(string portname, string address, string chillertype)
+        {
+            if (port2share.ContainsKey(portname))
+            {
+                port2share[portname].AddAddress(address, chillertype);
+                return port2share[portname];
+            }
+            else
+            {
+                //不包含该参数
+                SMCChillerDrive sMCChillerDrive = new SMCChillerDrive(portname);
+                port2share.Add(portname, sMCChillerDrive);
+                sMCChillerDrive.AddAddress(address, chillertype);
+                return sMCChillerDrive;
+            }
+        }
+
+    }
+}