| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 | using System;using System.Collections;using System.Collections.Concurrent;using System.Text;using System.Threading.Tasks;using System.Xml;using Aitex.Core.Common.DeviceData;using Aitex.Core.RT.DataCenter;using Aitex.Core.RT.Device;using Aitex.Core.RT.Event;using Aitex.Core.RT.IOCore;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.DataCenter;using MECF.Framework.Common.Device.Bases;using MECF.Framework.Common.Equipment;using MECF.Framework.Common.Utilities;using Venus_Core;namespace Venus_RT.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//, IoHeaterController    {        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 = 1000;        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];       //报警是否强制停机        BlockingCollection<string> blockingCollection = new BlockingCollection<string>();        JetChamber jetChamber = (JetChamber)(SC.GetValue<int>("System.ChamberSelect"));//Venus or Kepler        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 override float CoolantInletTcFeedback        {            get            {                if (jetChamber == JetChamber.Venus)                {                    return GetAiValue($"{Module}.AI_Coolant_Inlet_Temp");                }                else                {                    return 0;                }            }        }        [Subscription(AITChillerProperty.CoolantOutletTempFeedback)]        public override float CoolantOutletTcFeedback        {            get            {                if (jetChamber == JetChamber.Venus)                {                    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;            }        }        private SCConfigItem _scAlarmRange;        private SCConfigItem _scEnableAlarm;        private SCConfigItem _scAlarmTime;        private ToleranceChecker _toleranceChecker = new ToleranceChecker();        // --------------------------Constructor-----------------------        //        public SMCChiller(ModuleName mod, string name) : base(mod.ToString(), name, name, "")        {            if (Module == "PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"PMB.Chiller.EnableChiller")) return;            _PortNum = SC.GetStringValue($"{mod}.{Name}.Port");            _scSetPointLimitMax = SC.GetValue<double>($"{Module}.{Name}.SetPointLimitMax");            _scSetPointLimitMin = SC.GetValue<double>($"{Module}.{Name}.SetPointLimitMin");            StatusSMC = SMCChillerState.Unknown;            _scEnableAlarm = SC.GetConfigItem($"{Module}.{Name}.EnableToleranceAlarm");            _scAlarmRange = SC.GetConfigItem($"{Module}.{Name}.ToleranceAlarmRange");            _scAlarmTime = SC.GetConfigItem($"{Module}.{Name}.ToleranceAlarmTime");            _serial = new AsyncSerialPort(_PortNum, 9600, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, "\r\n", true);            Task.Run(() =>            {                foreach (var data in blockingCollection.GetConsumingEnumerable())                {                    _serial?.Write(data);                    System.Threading.Thread.Sleep(500);                }            });        }        public override bool Initialize()        {            base.Initialize();            if (Module == "PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMA") && SC.GetValue<bool>($"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;                LOG.Write( eEvent.ERR_DEVICE_CHILLER, 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 (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, Module, $"[{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.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{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.Write(eEvent.ERR_DEVICE_CHILLER,  Module, $"[{Module}] smc chiller error: [{ex.Message}]");            }        }        private void OnErrorOccurred(string obj)        {            StatusSMC = SMCChillerState.ERROR;            LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{Module}] smc chiller error: [{obj}]");        }        public override void Monitor()        {            try            {                if (Module=="PMB" && SC.GetValue<bool>($"PMB.Chiller.ChillerSameWithPMB") && SC.GetValue<bool>($"PMB.Chiller.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])                            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());            }        }        private 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 override void Reset()        {            _toleranceChecker.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);        }    }}
 |