HMI.cs 6.09 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using System.Text.RegularExpressions;

using System.Collections.ObjectModel;

using FLY.ModbusModule;
using FObjBase;


namespace FLY.FeedbackRenZiJia.Server
{

    public class HMI : IPLCLink
    {
        FLY.ModbusModule.ServerTCP modbusserver;
        PLCRegister mPLCRegs;

        #region 地址

        const int ADDR_M_HasElectricCurrent = 0;//bit 三相电 电流
        const int ADDR_M_HasFan = 1;//bit 风机启动
        
        const int ADDR_D_ChannelCnt = 0;//加热通道数
        const int ADDR_D_HeatUpdate = 1;//加热更新
        const int ADDR_D_CurrUpdate = 2;//当前值更新
        
        const int ADDR_D_Heats = 100;//加热量
        const int ADDR_D_Currs = 300;//当前值

          
        #endregion


        public HMI()
        {

            mPLCRegs = new PLCRegister(3, 600);
            mPLCRegs.RegChanged += new PLCRegister.RegChangedEventHandler(mPLCRegs_RegChanged);

            RegToPropertyNameInit();

            modbusserver = new ServerTCP(mPLCRegs);
            
            PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD, new PollModule.PollHandler(OnPoll));
        }

        void mPLCRegs_RegChanged(object sender, PLCRegister.RegChangedEventArgs e)
        {
            var propertyNames = from _r in rpn where _r.type == e.RegType && e.IsCover(_r.addr,_r.len) select _r.propertyName;
            if (propertyNames.Count() > 0)
            {
                foreach (var n in propertyNames)
                    NotifyPropertyChanged(n);
            }
        }




        class RegToPropertyName 
        {
            public PLCRegister.RegChangedEventArgs.REG_TYPE type;
            public int addr;
            public int len;
            public string propertyName;
            public RegToPropertyName(PLCRegister.RegChangedEventArgs.REG_TYPE type, int addr, int len, string propertyName)
            {
                this.type = type;
                this.addr = addr;
                this.len = len;
                this.propertyName = propertyName;
            }
        }
        List<RegToPropertyName> rpn = new List<RegToPropertyName>();
        
        /// <summary>
        /// 用于 外部写入后,地址转为PropertyName 触发 PropertyChanged
        /// </summary>
        void RegToPropertyNameInit() 
        {
86 87
            rpn.Add(new RegToPropertyName(PLCRegister.RegChangedEventArgs.REG_TYPE.COIL, ADDR_M_HasElectricCurrent, 1, nameof(HasElectricity)));
            rpn.Add(new RegToPropertyName(PLCRegister.RegChangedEventArgs.REG_TYPE.COIL, ADDR_M_HasFan, 1, nameof(HasFan)));
潘栩锋's avatar
潘栩锋 committed
88

89 90 91
            rpn.Add(new RegToPropertyName(PLCRegister.RegChangedEventArgs.REG_TYPE.REG, ADDR_D_ChannelCnt, 1, nameof(ChannelCnt)));
            rpn.Add(new RegToPropertyName(PLCRegister.RegChangedEventArgs.REG_TYPE.REG, ADDR_D_HeatUpdate, 1, nameof(HeatUpdate_R)));
            rpn.Add(new RegToPropertyName(PLCRegister.RegChangedEventArgs.REG_TYPE.REG, ADDR_D_CurrUpdate, 1, nameof(CurrUpdate)));        
潘栩锋's avatar
潘栩锋 committed
92 93 94 95 96 97 98 99
        }

        public void OnPoll()
        {
                //需要访问modbusserver 判断最近有没通讯
                if (DateTime.Now.Subtract(modbusserver.LastCommDT).TotalSeconds > 3)
                {
                    //有3秒没通讯
100
                    IsConnected = false;
潘栩锋's avatar
潘栩锋 committed
101 102 103
                }
                else
                {
104
                    IsConnected = true;
潘栩锋's avatar
潘栩锋 committed
105 106 107 108 109
                }
        }

        #region PLCLink 成员
        #region 状态
110 111


112
        public bool IsConnected { get; private set; }
113

潘栩锋's avatar
潘栩锋 committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        #endregion

        /// <summary>
        /// 当前电流 有没?
        /// </summary>
        public bool HasElectricity
        {
            get {
                return mPLCRegs.GetBool(ADDR_M_HasElectricCurrent);
            }
        }

        /// <summary>
        /// 风机是否启动?
        /// </summary>
        public bool HasFan
        {
            get
            {
                return mPLCRegs.GetBool(ADDR_M_HasFan);
            }
        }
136 137 138 139 140 141 142 143 144 145 146 147


        public void SetChannelCnt(UInt16 channelCnt)
        {
            ChannelCnt = channelCnt;
        }
        public void SetHeatUpdate(UInt16 heatUpdate)
        {
            HeatUpdate = heatUpdate;

        }

潘栩锋's avatar
潘栩锋 committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        public ushort ChannelCnt
        {
            get
            {
                return mPLCRegs.GetUInt16(ADDR_D_ChannelCnt);
            }
            set
            {
                mPLCRegs.SetUInt16(ADDR_D_ChannelCnt, value);
            }
        }
        public ushort HeatUpdate
        {
            get
            {
                return mPLCRegs.GetUInt16(ADDR_D_HeatUpdate);
            }
            set
            {
                mPLCRegs.SetUInt16(ADDR_D_HeatUpdate, value);
            }
        }
170 171 172 173 174 175 176
        public ushort HeatUpdate_R
        {
            get
            {
                return mPLCRegs.GetUInt16(ADDR_D_HeatUpdate);
            }
        }
潘栩锋's avatar
潘栩锋 committed
177 178 179 180 181 182 183 184 185 186 187 188
        public ushort CurrUpdate
        {
            get
            {
                return mPLCRegs.GetUInt16(ADDR_D_CurrUpdate);
            }
            set
            {
                mPLCRegs.SetUInt16(ADDR_D_CurrUpdate, value);
            }
        }

189
        public void SetHeat(IEnumerable<UInt16> value) 
潘栩锋's avatar
潘栩锋 committed
190
        {
191 192 193 194
            for (int i = 0; i < value.Count(); i++)
            {
                mPLCRegs.SetUInt16(ADDR_D_Heats + i, value.ElementAt(i));
            }
潘栩锋's avatar
潘栩锋 committed
195
        }
196
        public IEnumerable<UInt16> GetCurr()
潘栩锋's avatar
潘栩锋 committed
197
        {
198 199 200 201 202 203
            List<UInt16> list = new List<UInt16>();
            for (int i = 0; i < ChannelCnt; i++)
            {
                list.Add(mPLCRegs.GetUInt16(ADDR_D_Currs + i));
            }
            return list; 
潘栩锋's avatar
潘栩锋 committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        }
        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion

    }
}