CoilData.cs 1.86 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FLY.Modbus
{

    /// <summary>
    /// property 与 PLC继电器 更新操作,与 RegsData 对应
    /// </summary>
    public class CoilData : AreaManager
    {
        public CoilData(PLCAddressArea area, int maxOfOneRead) : base(area, maxOfOneRead)
        {

        }

        /// <summary>
        /// 根据当前regs的配置指定读寄存器的计划
        /// </summary>
        public override void MakePlan()
        {
            //this.maxOfOneRead = maxOfOneRead;
            if (regs == null)
            {
                throw new Exception("还没有执行 BuildArray()");
            }


            lock (plan_buffer)
            {
                plan_buffer.Clear();

                int addr = -1;
                int num = 0;
                for (int i = 0; i < regs.Count(); i++)
                {
                    if (!regs[i].isNeedUpdate)
                        continue;

                    if (addr == -1)
                    {
                        addr = regs[i].addr;
                        num = 1;
                    }
                    else
                    {
                        int n = regs[i].addr - addr + 1;
                        if (n <= maxOfOneRead)
                        {
                            num = n;
                        }
                        else
                        {
                            plan_buffer.Add(new Plan(area, addr, num));
                            addr = -1;
                            i--;
                        }
                    }
                }
                if (addr != -1)
                {
                    plan_buffer.Add(new Plan(area, addr, num));
                    addr = -1;
                }
                isPlanChanged = true;
            }
        }

    }
}