BasePLC.cs 2.16 KB
using RCS.Model.Comm;
using RCS.Model.Entity;

namespace RCS.Communication
{
    /// <summary>
    /// PLC顶层接口
    /// </summary>
    public abstract class BasePLC
    {
        #region 变量、属性

        /// <summary>
        /// PLC的IP地址
        /// </summary>
        public string IP { get; set; }

        #endregion

        #region PLC操作

        /// <summary>
        /// 连接,通常指定PLC的初始化请在连接中进行
        /// </summary>
        /// <returns></returns>
        public abstract BllResult Connect();

        /// <summary>
        /// 断开,通常在断开之前请先停止逻辑处理,延迟后再调用
        /// </summary>
        /// <returns></returns>
        public abstract BllResult DisConnect();

        /// <summary>
        /// 获取连接状态
        /// hack:对于使用OPC实现,此链接状态指示为client与OPC服务器的连接状态;所以使用OPC的情况下需要额外检测读写是否正确。
        /// </summary>
        /// <returns></returns>
        public abstract BllResult GetConnectStatus();

        /// <summary>
        /// 读取地址
        /// </summary>
        /// <param name="equipmentProps"></param>
        /// <returns></returns>
        public abstract BllResult Reads(List<Base_EquipmentProp> equipmentProps);

        /// <summary>
        /// 写入地址
        /// </summary>
        /// <param name="equipmentProps"></param>
        /// <returns></returns>
        public abstract BllResult<List<string>> Writes(List<Base_EquipmentProp> equipmentProps);

        #endregion

        #region 字节位操作

        //index从0开始  
        //获取取第index位  
        public int GetBit(byte b, int index) { return ((b & (1 << index)) > 0) ? 1 : 0; }
        //将第index位设为1  
        public byte SetBit(byte b, int index) { return (byte)(b | (1 << index)); }
        //将第index位设为0   
        public byte ClearBit(byte b, int index) { return (byte)(b & (byte.MaxValue - (1 << index))); }
        //将第index位取反   
        public byte ReverseBit(byte b, int index) { return (byte)(b ^ (byte)(1 << index)); }

        #endregion
    }
}