DataToRegs.cs 4.62 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FLY.Modbus
{
    /// <summary>
    /// 数据映射的类;
    /// Data: PC 属性 
    /// Regs: PLC 对象(寄存器、线圈)
    /// </summary>
    public class DataToRegs
    {
        /// <summary>
        /// 所属的plc
        /// </summary>
        public ModbusMapper mapper;

        /// <summary>
        /// 对应 PLC寄存器区 coil or register
        /// </summary>
        public PLCAddressArea dataArea;

        /// <summary>
        /// PLC 首地址
        /// </summary>
        public int addr;

        /// <summary>
        /// PLC 尾地址
        /// </summary>
        public int endAddr;

        /// <summary>
        /// value 的C#类型 float,int16,int32
        /// </summary>
        public REG_TYPE type;

        /// <summary>
        /// 放大倍数, value * scale 才是属性的值
        /// </summary>
        public double scale;

        /// <summary>
        /// object 
        /// </summary>
        public object owner;

        /// <summary>
        /// object 的 property 名称, 只能是 数字类型都是double, 剩下就是 bool
        /// </summary>
        public string propertyName;

        /// <summary>
        /// property 的值, 没有缩小. 只有 double 与 bool 
        /// </summary>
        public object value;

        /// <summary>
        /// PLC obj ,bool or UInt16[]
        /// </summary> 
        public object plcValue;

        /// <summary>
        /// PLC obj 寄存器值是否改变
        /// </summary>
        public bool isPlcValueChanged;

        /// <summary>
        /// 需要从PLC读取数据更新
        /// </summary>
        public bool isNeedUpdate = false;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="dataArea"></param>
        /// <param name="addr"></param>
        /// <param name="type"></param>
        /// <param name="scale"></param>
        /// <param name="owner"></param>
        /// <param name="propertyname"></param>
        public DataToRegs(ModbusMapper mapper, PLCAddressArea dataArea, int addr, REG_TYPE type, double scale, object owner, string propertyname)
        {
            this.mapper = mapper;
            this.dataArea = dataArea;
            this.addr = addr;
            this.type = type;
            this.scale = scale;
            this.owner = owner;
            this.propertyName = string.Copy(propertyname);

            if (type == REG_TYPE.BOOL)
            {
                plcValue = false;
                endAddr = addr;
            }
            else
            {
                plcValue = new UInt16[ModbusMapper.RegSize(type)];
                endAddr = addr + ((UInt16[])plcValue).Count() - 1;
            }
        }

        /// <summary>
        /// PLC 对象(寄存器、线圈)转 PC 属性
        /// </summary>
        /// <param name="plc_value">bool or UInt16[]</param>
        /// <returns></returns>
        public object ToPropertyObj(object plc_value)
        {
113 114
            return RegTypeConverter.ToObject(plc_value, type, scale);
            
潘栩锋's avatar
潘栩锋 committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        }
        /// <summary>
        /// PLC 对象(寄存器、线圈)转 PC 属性, 本地
        /// </summary>
        public void ToPropertyObj()
        {
            value = ToPropertyObj(plcValue);
        }

        /// <summary>
        /// PC 属性 转 PLC 对象(寄存器、线圈)
        /// </summary>
        /// <param name="value"></param>
        /// <returns>bool or UInt16[]</returns>
        public object ToPLCObj(object value)
        {
131
            return RegTypeConverter.ToRegs(value, type, scale);
潘栩锋's avatar
潘栩锋 committed
132 133 134 135 136 137 138 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
        }
        /// <summary>
        /// PC 属性 转 PLC 对象(寄存器、线圈), 本地
        /// </summary>
        public void ToPLCObj()
        {
            object plcobj = ToPLCObj(value);

            //更新 本地的 PLC 数据
            switch (dataArea)
            {
                case PLCAddressArea.Coil:
                    plcValue = plcobj;
                    break;
                case PLCAddressArea.Register:
                    {
                        UInt16[] src = plcobj as UInt16[];
                        UInt16[] dest = plcValue as UInt16[];
                        Array.Copy(src, dest, dest.Length);
                    }
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return propertyName + " [" + ((int)dataArea).ToString() + "](" + addr + ")";
        }
    }
}