ModbusMapper_Client.cs 8.77 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
using System ;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;

namespace FLY.Modbus
{
    /// <summary>
    /// 学习HMI,  根据 property 与 PLC寄存器 映射表 自动更新
    /// 
    /// 使用说明:
    /// 1.注册 NameDataChanged 事件, 当从PLC读取的数据更新后,通知property修改
    /// NameDataChanged += ......
    /// 
    /// 2.填写 property 与 PLC寄存器 映射表 
    /// DRMap
    /// 
    /// 3.通过FindDataMap() 找到 映射 注册需要更新的属性 
    /// dr = FindDataMap(obj, propertyName)
    /// key = 页面对应obj
    /// PlanAdd(key, dr)
    /// 
    /// 4.创建更新计划
    /// PlanMake()
    /// 
    /// 5.页面关闭,不需要再更新 property, 注销更新计划
    /// key = 页面对应obj
    /// PlanRemove(key)
    /// 
    /// 6.当property 修改后,调用 SetNameData 设置PLC
    /// SetNameData(obj, propertyName, propertyValue)
    /// </summary>
    public class ModbusMapper_Client : ModbusMapper
    {
        /// <summary>
        /// 更新周期,单位ms,
        /// </summary>
        public int UpdateInterval = 200;

        /// <summary>
        /// 实际更新间隔
        /// </summary>
        public TimeSpan ActUpdateInterval { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
47 48


潘栩锋's avatar
潘栩锋 committed
49 50 51
        private Stopwatch mStopwatch = new Stopwatch();
        class RegWrite
        {
潘栩锋's avatar
潘栩锋 committed
52
            public DataToRegs dr;
潘栩锋's avatar
潘栩锋 committed
53 54
            public object val;

潘栩锋's avatar
潘栩锋 committed
55
            public RegWrite(DataToRegs dr, object val)
潘栩锋's avatar
潘栩锋 committed
56
            {
潘栩锋's avatar
潘栩锋 committed
57
                this.dr = dr;
潘栩锋's avatar
潘栩锋 committed
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 86 87 88 89 90 91 92
                this.val = val;
            }
        }

        List<RegWrite> rws = new List<RegWrite>();

        /// <summary>
        /// tcp client
        /// </summary>
        public ClientTCP mclient;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="clienttcp"></param>
        public ModbusMapper_Client(ClientTCP clienttcp)
        {
            mclient = clienttcp;

            FObjBase.PollModule.Current.Poll_Config(OnPoll_update);

            mclient.PropertyChanged += Mclient_PropertyChanged;
        }



        public override string ToString()
        {
            return mclient.ToString();
        }
        private void Mclient_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsConnected")
            {
                if (!mclient.IsConnected)
                {
潘栩锋's avatar
潘栩锋 committed
93 94 95
                    foreach (var areaManager in mAreaManager)
                        areaManager.ClearPlanState();

潘栩锋's avatar
潘栩锋 committed
96 97 98 99 100 101
                    //复位全部状态
                    isInCommunication = false;
                    p_doing = null;
                    isUpdating = false;
                    curr_area_idx = 0;
                    // DRMap 的数据 与 下端 属性的数据一致
潘栩锋's avatar
潘栩锋 committed
102 103 104 105
                    //foreach (var areaManager in mAreaManager)
                    //    areaManager.UpdatePLCObjFromProperty();

                    //rws.Clear();
潘栩锋's avatar
潘栩锋 committed
106 107 108

                    mStopwatch.Stop();
                }
潘栩锋's avatar
潘栩锋 committed
109 110 111 112 113 114
                else
                {
                    foreach (var areaManager in mAreaManager)
                        areaManager.SetAllIsPlcValueChanged();
                    rws.Clear();
                }
潘栩锋's avatar
潘栩锋 committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            }
        }

        void OnPoll_update()
        {
            if (!mclient.IsConnected)
                return;

            if (!mStopwatch.IsRunning)
                mStopwatch.Restart();
            
            if (mStopwatch.ElapsedMilliseconds >= UpdateInterval)
            {
                //检查上次更新周期是否完成
                if (!isUpdating)
                {
                    isUpdating = true;
                    ActUpdateInterval = mStopwatch.Elapsed;
                    mStopwatch.Restart();
潘栩锋's avatar
潘栩锋 committed
134 135 136 137 138

                    foreach (var areaManager in mAreaManager)
                    {
                        areaManager.MakePlanReal();
                    }
潘栩锋's avatar
潘栩锋 committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
                }
            }
            NextPlan();
        }

        int curr_area_idx = 0;
        void NextPlan()
        {
            if (isInCommunication)//已经在通讯中
                return;

            if (rws.Count() > 0)
                UpdateWriteData();


            if (isUpdating)
            {
                p_doing = mAreaManager[curr_area_idx].GetNextPlan();
                if (p_doing != null)
                {
                    p_doing.isDoing = false;
                    UpdateReadData();
                }
                else
                {
                    //读完
                    curr_area_idx++;
                    if (curr_area_idx >= mAreaManager.Count())
                    {
                        isUpdating = false;
                        //读完一轮了
                        curr_area_idx = 0;
                    }
                    else
                    {
                        NextPlan();
                    }
                }
            }
        }

        /// <summary>
        /// 读 多个线圈回调
        /// </summary>
        /// <param name="val"></param>
        /// <param name="obj"></param>
        void Do_01_callback(bool[] val, object obj)
        {
潘栩锋's avatar
潘栩锋 committed
187
            Plan p = obj as Plan;
潘栩锋's avatar
潘栩锋 committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201

            p.isDoing = false;
            SetModbusData(PLCAddressArea.Coil, p.addr, val);
            isInCommunication = false;
            NextPlan();
        }
        /// <summary>
        /// 读多个寄存器回调
        /// </summary>
        /// <param name="val"></param>
        /// <param name="obj"></param>
        void Do_03_callback(UInt16[] val, object obj)
        {
            
潘栩锋's avatar
潘栩锋 committed
202
            Plan p = obj as Plan;
潘栩锋's avatar
潘栩锋 committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
            p.isDoing = false;
            SetModbusData(PLCAddressArea.Register, p.addr, val);
            isInCommunication = false;
            NextPlan();
        }

        void Do_0F_callback(object obj)
        {
            isInCommunication = false;
            NextPlan();
        }
        void Do_10_callback(object obj)
        {
            isInCommunication = false;
            NextPlan();
        }
        /// <summary>
        /// 正在通讯中
        /// </summary>
        bool isInCommunication = false;
        /// <summary>
        /// 读更新流程中
        /// </summary>
        bool isUpdating = false;
        /// <summary>
        /// 读更新 的计划
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
230
        Plan p_doing = null;
潘栩锋's avatar
潘栩锋 committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="propertyname"></param>
        //protected void NotifyPropertyChanged(string propertyname)
        //{
        //    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        //public event PropertyChangedEventHandler PropertyChanged;


        /// <summary>
        /// PC -> PLC
        /// 为命名数据赋值,会改变Modbus的寄存器值,并产生通知
        /// </summary>
        /// <param name="dr">数据映射</param>
        /// <param name="val">数据值,不能为空</param>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
252
        public override void SetNameData(DataToRegs dr, object val)
潘栩锋's avatar
潘栩锋 committed
253 254
        {
            RegWrite rw;
潘栩锋's avatar
潘栩锋 committed
255 256 257 258 259 260
            //rw = rws.Find((c) => { return (c.dr == dr); });
            //if (rw != null)
            //    rws.Remove(rw);

            rw = new RegWrite(dr, val);
            rws.Add(rw);
潘栩锋's avatar
潘栩锋 committed
261 262 263 264 265 266 267
        }
        /// <summary>
        /// 更新写命令缓冲区
        /// </summary>
        void UpdateWriteData()
        {
            RegWrite rw = rws.First();
潘栩锋's avatar
潘栩锋 committed
268 269 270 271 272 273
            rws.Remove(rw);

            //更新 本地的 PLC 数据
            rw.dr.isPlcValueChanged = true;

            switch (rw.dr.dataArea)
潘栩锋's avatar
潘栩锋 committed
274 275
            {
                case PLCAddressArea.Coil:
潘栩锋's avatar
潘栩锋 committed
276
                    if (mclient.Do_0F((UInt16)rw.dr.addr, new bool[] { (bool)rw.dr.plcValue }, Do_0F_callback, null) != ModbusClient_Errno.OK)
潘栩锋's avatar
潘栩锋 committed
277 278 279
                        return;
                    break;
                case PLCAddressArea.Register:
潘栩锋's avatar
潘栩锋 committed
280
                    if (mclient.Do_10((UInt16)rw.dr.addr, (UInt16[])rw.dr.plcValue, Do_10_callback, null) != ModbusClient_Errno.OK)
潘栩锋's avatar
潘栩锋 committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
                        return;
                    break;
            }

            isInCommunication = true;
        }
        void UpdateReadData()
        {
            switch (p_doing.area)
            {
                case PLCAddressArea.Coil:
                    mclient.Do_01((UInt16)p_doing.addr, (UInt16)p_doing.num, Do_01_callback, p_doing);
                    break;
                case PLCAddressArea.Register:
                    mclient.Do_03((UInt16)p_doing.addr, (UInt16)p_doing.num, Do_03_callback, p_doing);
                    break;
            }
            p_doing.isDoing = true;
            isInCommunication = true;
        }
潘栩锋's avatar
潘栩锋 committed
301 302


潘栩锋's avatar
潘栩锋 committed
303 304 305
    }

}