using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FLY.Modbus
{
///
/// 数据映射的类;
/// Data: PC 属性
/// Regs: PLC 对象(寄存器、线圈)
///
public class DataToRegs
{
///
/// 所属的plc
///
public ModbusMapper mapper;
///
/// 对应 PLC寄存器区 coil or register
///
public PLCAddressArea dataArea;
///
/// PLC 首地址
///
public int addr;
///
/// 原始寄存器字符串
///
public string regAddr;
///
/// 原始 owner 名字
///
public string ownerName;
///
/// PLC 尾地址
///
public int endAddr;
///
/// value 的C#类型 float,int16,int32
///
public REG_TYPE type;
///
/// 放大倍数, value * scale 才是属性的值
///
public double scale;
///
/// object
///
public object owner;
///
/// object 的 property 名称, 只能是 数字类型都是double, 剩下就是 bool
///
public string propertyName;
///
/// property 的值, 没有缩小. 只有 double 与 bool
///
public object value;
///
/// PLC obj ,bool or UInt16[]
///
public object plcValue;
///
/// PLC obj 寄存器值是否改变
///
public bool isPlcValueChanged;
///
/// 需要从PLC读取数据更新
///
public bool isNeedUpdate = false;
///
///
///
///
///
///
///
///
///
///
public DataToRegs(ModbusMapper mapper, PLCAddressArea dataArea, int addr, REG_TYPE type, double scale, object owner, string propertyname, string regAddr, string ownerName)
{
this.mapper = mapper;
this.dataArea = dataArea;
this.addr = addr;
this.type = type;
this.scale = scale;
this.owner = owner;
this.propertyName = string.Copy(propertyname);
this.regAddr = regAddr;
this.ownerName = ownerName;
if (type == REG_TYPE.BOOL)
{
plcValue = false;
endAddr = addr;
}
else
{
plcValue = new UInt16[ModbusMapper.RegSize(type)];
endAddr = addr + ((UInt16[])plcValue).Count() - 1;
}
}
///
/// PLC 对象(寄存器、线圈)转 PC 属性
///
/// bool or UInt16[]
///
public object ToPropertyObj(object plc_value)
{
return RegTypeConverter.ToObject(plc_value, type, scale);
}
///
/// PLC 对象(寄存器、线圈)转 PC 属性, 本地
///
public void ToPropertyObj()
{
value = ToPropertyObj(plcValue);
}
///
/// PC 属性 转 PLC 对象(寄存器、线圈)
///
///
/// bool or UInt16[]
public object ToPLCObj(object value)
{
return RegTypeConverter.ToRegs(value, type, scale);
}
///
/// PC 属性 转 PLC 对象(寄存器、线圈), 本地
///
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;
}
}
///
///
///
///
public override string ToString()
{
return propertyName + " [" + ((int)dataArea).ToString() + "](" + addr + ")";
}
}
}