FlyADIODefine.cs 8.68 KB
Newer Older
1 2 3 4 5 6
using FLY.Thick.Base.IService;
using FObjBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
潘栩锋's avatar
潘栩锋 committed
7 8 9 10 11

namespace FLY.Thick.Base.Common
{
    /// <summary>
    /// AD盒IO定义,避免忘记IO口的定义,出现功能重复
12 13 14 15 16 17 18 19 20 21
    /// 
    /// 使用方法:
    /// 1.每款机型例如吹膜 继承FlyADIODefine
    /// 2.添加 输入口,输出口
    /// 3.重载 GetInputPropertyNames 显示输出 输入口定义
    /// 4.重载 GetOutputPropertyNames 显示输出 输出口定义
    /// 5.重定义 static new FlyADIODefine Instance
    /// 
    /// 6.在Gage中 调用 FlyADIODefine.SetInstance(new FlyADIODefine());
    /// 7.在Gage中 注册 AD盒事件, AD盒版本修改时,FlyADIODefine.SerVersion(version);
潘栩锋's avatar
潘栩锋 committed
22
    /// </summary>
23
    public class FlyADIODefine: IFlyAdIoDefineService
潘栩锋's avatar
潘栩锋 committed
24
    {
25 26 27 28 29
        protected static FlyADIODefine instance;
        public static void SetInstance(FlyADIODefine flyADIODefine) 
        {
            instance = flyADIODefine;
        }
30 31 32
        /// <summary>
        /// 子类 需要 重新 把子类对象 赋值给 Instance, 确保整个环境只用一个版本的 单例
        /// </summary>
33
        public static FlyADIODefine Instance => instance;
34
        
潘栩锋's avatar
潘栩锋 committed
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
        #region 输入口
        /// <summary>
        /// 归零信号
        /// </summary>
        [Description("归零信号")]
        public int InNo_Org { get; protected set; } = 2 - 1;

        /// <summary>
        /// 正向限位
        /// </summary>
        [Description("正向限位")]
        public int InNo_Limit_Forw { get; protected set; } = 3 - 1;
        /// <summary>
        /// 反向限位
        /// </summary>
        [Description("反向限位")]
        public int InNo_Limit_Backw { get; protected set; } = 4 - 1;

        /// <summary>
        /// 数据有效
        /// </summary>
        [Description("数据有效")]
        public int InNo_DataValid { get; protected set; } = 6 - 1;

        /// <summary>
        /// 急停 and 手动正转
        /// </summary>
        [Description("急停 & 手动正转")]
        public int InNo_Manual_Forw { get; protected set; } = 7 - 1;

        /// <summary>
        /// 急停 and 手动反转
        /// </summary>
        [Description("急停 & 手动反转")]
        public int InNo_Manual_Backw { get; protected set; } = 8 - 1;

        /// <summary>
        /// 同步输入信号
        /// </summary>
        [Description("同步输入信号")]
        public int InNo_Sync { get; protected set; } = 9 - 1;

        /// <summary>
        /// 辊速信号
        /// </summary>
        [Description("辊速信号")]
        public int InNo_Roll { get; protected set; } = 11 - 1;

        /// <summary>
        /// 控制主轴脉冲启动计数输入, o3 接 i11;
        /// i11 为 0 计数停止
        /// </summary>
        [Description("主轴脉冲启动计数")]
        public int InNo_Pos2OnOff { get; protected set; } = 11 - 1;

        /// <summary>
        /// 纵向光纤信号
        /// </summary>
        [Description("纵向光纤信号")]
        public int InNo_VSign { get; protected set; } = 12 - 1;

        #endregion
        #region 输出口
        /// <summary>
        /// 变频器反转 VF0 是松下的变频器牌子
        /// </summary>
        [Description("变频器反转")]
102 103 104 105 106 107
        public int OutNo_VF0_Backw { get; protected set; } = 1 - 1;
        /// <summary>
        /// 变频器正转 VF0 是松下的变频器牌子
        /// </summary>
        [Description("变频器正转")]
        public int OutNo_VF0_Forw { get; protected set; } = 2 - 1;
潘栩锋's avatar
潘栩锋 committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        /// <summary>
        /// 变频器减速 VF0 是松下的变频器牌子
        /// </summary>
        [Description("变频器减速")]
        public int OutNo_VF0_Slow { get; protected set; } = 3 - 1;

        /// <summary>
        /// 控制主轴脉冲启动计数
        /// </summary>
        [Description("主轴脉冲启动计数")]
        public int OutNo_Pos2OnOff { get; protected set; } = 3 - 1;

        /// <summary>
        /// 同步输出信号
        /// </summary>
        [Description("同步输出信号")]
        public int OutNo_Sync { get; protected set; } = 3 - 1;

        /// <summary>
        /// 报警信号
        /// </summary>
        [Description("报警信号")]
        public int OutNo_Alarm { get; protected set; } = 4 - 1;


        #endregion

135
        private int version = 2;
136 137 138 139 140
        /// <summary>
        /// 根据AD盒的版本,设置
        /// </summary>
        /// <param name="version"></param>
        public virtual void SerVersion(int version)
潘栩锋's avatar
潘栩锋 committed
141
        {
142 143
            this.version = version;
            if (this.version == 3)
潘栩锋's avatar
潘栩锋 committed
144
            {
145 146 147 148 149
                InNo_Org = 13 - 1;
                InNo_Limit_Forw = 14 - 1;
                InNo_Limit_Backw = 16 - 1;

                InNo_Roll = 15 - 1;
潘栩锋's avatar
潘栩锋 committed
150 151 152
            }
            else
            {
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 187 188 189 190 191 192 193 194 195 196 197
                InNo_Org = 2 - 1;
                InNo_Limit_Forw = 3 - 1;
                InNo_Limit_Backw = 4 - 1;

                InNo_Roll = 11 - 1;
            }
        }

        /// <summary>
        /// 获取输入口 属性名
        /// </summary>
        /// <returns></returns>
        protected virtual List<string> GetInputPropertyNames() 
        {
            List<string> reponse = new List<string>();
            Type t = this.GetType();
            var properties = t.GetProperties();

            foreach (var property in properties)
            {
                if (property.Name.StartsWith("InNo_"))
                {
                    //输入
                    reponse.Add(property.Name);
                }
            }
            return reponse;
        }
        /// <summary>
        /// 获取输出口 属性名
        /// </summary>
        /// <returns></returns>
        protected virtual List<string> GetOutputPropertyNames()
        {
            List<string> reponse = new List<string>();
            Type t = this.GetType();
            var properties = t.GetProperties();

            foreach (var property in properties)
            {
                if (property.Name.StartsWith("OutNo_"))
                {
                    //输入
                    reponse.Add(property.Name);
                }
潘栩锋's avatar
潘栩锋 committed
198
            }
199 200 201 202 203
            return reponse;
        }
        public void GetIODefine(AsyncCBHandler asyncDelegate, object asyncContext)
        {
            //获取全部带 Description 的属性,且名字 InNo_XXX 或 OutNo_XXX
204 205 206 207 208 209 210 211 212 213 214 215 216
            IODefineCollection reponse = new IODefineCollection();
            
            List<IODefine> list = new List<IODefine>();
            reponse.List = list;
            if (version == 3)
            {
                reponse.InCount = 16;
                reponse.OutCount = 8;
            }
            else {
                reponse.InCount = 12;
                reponse.OutCount = 4;
            }
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

            var inputPropertyNames = GetInputPropertyNames();
            var outputPropertyNames = GetOutputPropertyNames();

            Type t = this.GetType();
            
            foreach (var propertyName in inputPropertyNames)
            {
                var property = t.GetProperty(propertyName);
               
                var attrs = property.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attrs.Count() == 0)
                {
                    throw new Exception($"{propertyName} 没有写 [Description]");
                }

                var attr = attrs.First() as DescriptionAttribute;
                string desp = attr.Description;

                var index = (int)property.GetValue(this);
                var iodefine = new IODefine()
                {
                    IoType = IODefine.IOTYPE.Input,
                    Index = index,
                    Description = desp
                };
243
                list.Add(iodefine);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
            }

            foreach (var propertyName in outputPropertyNames)
            {
                var property = t.GetProperty(propertyName);

                var attrs = property.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attrs.Count() == 0)
                {
                    throw new Exception($"{propertyName} 没有写 [Description]");
                }

                var attr = attrs.First() as DescriptionAttribute;
                string desp = attr.Description;

                var index = (int)property.GetValue(this);
                var iodefine = new IODefine()
                {
                    IoType = IODefine.IOTYPE.Output,
                    Index = index,
                    Description = desp
                };
266
                list.Add(iodefine);
267 268
            }

269
            asyncDelegate?.Invoke(asyncContext, reponse);
潘栩锋's avatar
潘栩锋 committed
270 271 272
        }
    }
}