IOManager.cs 11.4 KB
using log4net;
using TSA_V.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace TSA_V.DeviceLibrary
{
    public class IOManager
    {
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        
        public static Dictionary<string, ConfigIO> DIList = new Dictionary<string, ConfigIO>();
        public static Dictionary<string, ConfigIO> DOList = new Dictionary<string, ConfigIO>();
        public static string KNDIP = "";
        public static ushort KNDPort = 0;
        public static void ConnectionIO()
        {
            if (!ISConnection())
            {
                IOBase.instance.ConnectionIP(KNDIP, KNDPort);
                System.Threading.Thread.Sleep(100); 
            }
        }

        public static bool ISConnection()
        {
            return IOBase.instance.IsConnection(KNDIP);
        }
     private static    StringBuilder builder = new StringBuilder("\r\n");
         
        static IOManager()
        {
            KNDIP = ConfigAppSettings.GetValue(Setting_Init.IO_IP);
            KNDPort = (ushort)ConfigAppSettings.GetIntValue(Setting_Init.IO_Port);
            byte slaveId = (byte)0;
            IOBase.Init();
            builder = new StringBuilder("\r\n");


            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, IOBase.instance.DIStartAddress, "SuddenStop", "X01_急停"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 1), "Footrest_Single", "X02_启动"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 2), "AirCheck_Single", "X03_气压检测"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 3), "SMEMA_Up", "X04_SMEMA前机送料"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 4), "SMEMA_Down", "X05_SMEMA后机要料"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 5), "LineInCheck", "X06_线体入口检测"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 6), "LineWorkCheck", "X07_线体工作区域检测"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 7), "LineOutCheck", "X08_线体出口检测"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 8), "StopCylinder_Up", "X09_挡停气缸上升端"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 9), "StopCylinder_Down", "X10_挡停气缸下降端"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 10), "SideCylinder_Forward", "X11_侧挡气缸前进端"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 11), "SideCylinder_Back", "X12_侧挡气缸后退端"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 12), "BottomCylinder_Down1", "X13_底部定位气缸1下降端"));
            AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 13), "BottomCylinder_Down2", "X14_底部定位气缸2下降端"));

            AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, IOBase.instance.DoStartAddress, "Run_Single", "Y01_设备运行指示灯"));
            AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 1), "Line_FStart", "Y02_线体皮带线运转"));
            AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 2), "SMEMA_Up", "Y03_SMEMA前机要料请求"));
            AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 3), "SMEMA_Down", "Y04_SMEMA后机送料请求"));
            AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 4), "StopCylinder_Up", "Y05_挡停气缸上升SOL"));
            AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 5), "StopCylinder_Down", "Y06_挡停气缸下降SOL"));
            AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 6), "SideCylinder_Forward", "Y07_侧挡气缸前进SOL"));
            AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 7), "SideCylinder_Back", "Y08_侧挡气缸后退SOL"));
            AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 8), "BottomCylinder_Up", "Y09_底部定位气缸上升SOL"));
            AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 9), "BottomCylinder_Down", "Y10_底部定位气缸下降SOL"));
            if (String.IsNullOrEmpty(builder.ToString()).Equals(false) && builder.ToString().Equals("\r\n").Equals(false))
            {
          //      LogUtil.info(builder.ToString());
            }
        }
        private static void  AddDI(ConfigIO io)
        {
            DIList.Add(io.ProName, io);
            builder.Append(" /// <summary>\r\n");
            builder.Append(" ///  " + io.Explain + "\r\n");
            builder.Append(" /// </summary>\r\n");
            builder.Append(" public static string " + io.ProName + "=\"" + io.ProName + "\";\r\n");
        }
        private static void AddDO(ConfigIO io)
        {
            DOList.Add(io.ProName, io);
            builder.Append(" /// <summary>\r\n");
            builder.Append(" ///  " + io.Explain + "\r\n");
            builder.Append(" /// </summary>\r\n");
            builder.Append(" public static string " + io.ProName + "=\"" + io.ProName + "\";\r\n");
        }
        /// <summary>
        ///  X01_急停
        /// </summary>
        public static string SuddenStop = "SuddenStop";
        /// <summary>
        ///  X02_启动
        /// </summary>
        public static string Footrest_Single = "Footrest_Single";
        /// <summary>
        ///  X03_气压检测
        /// </summary>
        public static string AirCheck_Single = "AirCheck_Single";
        /// <summary>
        ///  X04_SMEMA前机送料
        /// </summary>
        public static string SMEMA_Up = "SMEMA_Up";
        /// <summary>
        ///  X05_SMEMA后机要料
        /// </summary>
        public static string SMEMA_Down = "SMEMA_Down";
        /// <summary>
        ///  X06_线体入口检测
        /// </summary>
        public static string LineInCheck = "LineInCheck";
        /// <summary>
        ///  X07_线体工作区域检测
        /// </summary>
        public static string LineWorkCheck = "LineWorkCheck";
        /// <summary>
        ///  X08_线体出口检测
        /// </summary>
        public static string LineOutCheck = "LineOutCheck";
        /// <summary>
        ///  X09_挡停气缸上升端
        /// </summary>
        public static string StopCylinder_Up = "StopCylinder_Up";
        /// <summary>
        ///  X10_挡停气缸下降端
        /// </summary>
        public static string StopCylinder_Down = "StopCylinder_Down";
        /// <summary>
        ///  X11_侧挡气缸前进端
        /// </summary>
        public static string SideCylinder_Forward = "SideCylinder_Forward";
        /// <summary>
        ///  X12_侧挡气缸后退端
        /// </summary>
        public static string SideCylinder_Back = "SideCylinder_Back";
        /// <summary>
        ///  X13_底部定位气缸1下降端
        /// </summary>
        public static string BottomCylinder_Down1 = "BottomCylinder_Down1";
        /// <summary>
        ///  X14_底部定位气缸2下降端
        /// </summary>
        public static string BottomCylinder_Down2 = "BottomCylinder_Down2";
        /// <summary>
        ///  Y01_设备运行指示灯
        /// </summary>
        public static string Run_Single = "Run_Single";
        /// <summary>
        ///  Y02_线体皮带线运转
        /// </summary>
        public static string Line_FStart = "Line_FStart";
       
        /// <summary>
        ///  Y09_底部定位气缸上升SOL
        /// </summary>
        public static string BottomCylinder_Up = "BottomCylinder_Up";
        /// <summary>
        ///  Y10_底部定位气缸下降SOL
        /// </summary>
        public static string BottomCylinder_Down = "BottomCylinder_Down";

        #region IO控制方法
        public static void IOCheckAndMove(string ioType,IO_VALUE ioValue)
        {
            if (!IOValue(ioType).Equals(ioValue))
            {
                IOMove(ioType, ioValue);
            }
        }
        public static void IOMove(string ioType, IO_VALUE ioValue)
        {
            try
            {
                if (DOList.ContainsKey(ioType))
                {
                    ConfigIO io = DOList[ioType];
                    IOBase.instance.WriteSingleDO(io.DeviceName, io.SlaveID, io.IOIndex, ioValue);
                    Thread.Sleep(60);
                }
                else
                {
                    LogUtil.error(LOGGER, "没有DO=" + ioType);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(LOGGER, "KNDIOMove错误ioType=" + ioType + ",value=" + ioValue + ",错误信息:" + ex.ToString());
            }
        } 
        public static  IO_VALUE IOValue(string ioType)
        {
            IO_VALUE ioValue = IO_VALUE.LOW;
            if (DIList.ContainsKey(ioType))
            {
                ConfigIO configIo = DIList[ioType];
                ioValue = IOBase.instance.GetDIValue(configIo.DeviceName, configIo.SlaveID, configIo.IOIndex);
                //UpdateDoValue(ioType, ioValue);
            }
            else if (DOList.ContainsKey(ioType))
            {
                ConfigIO configIo = DOList[ioType];
                ioValue = IOBase.instance.GetDOValue(configIo.DeviceName, configIo.SlaveID, configIo.IOIndex);
            }
            else
            {
                LogUtil.error(LOGGER, "没有DO=" + ioType);
            }
            return ioValue;
        }

        #endregion

        public static ConfigIO getWaitIO(string p)
        {
            if (DIList.ContainsKey(p))
            {
                return DIList[p];
            }
            else if (DOList.ContainsKey(p))
            {
                return DOList[p];
            }
            return null;
        }

        public static bool ShuddenOK()
        {
            if (ISConnection().Equals(false))
            {
                return false;
            }
            if (IOValue(IOManager.SuddenStop).Equals(IO_VALUE.LOW))
            {
                return false;
            }
            return true;
        }

    }
    public enum IO_VALUE
    {
        /// <summary>
        /// 低位
        /// </summary>
        LOW = 0,

        /// <summary>
        /// 高位
        /// </summary>
        HIGH = 1,
    }


    public static class ConfigItemType
    {
        public static string DI = "DI";
        public static string DO = "DO";
        public static string AXIS = "AXIS";
        public static string PRO = "PRO";
    }
}