MeteringSignalBean.cs 2.6 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace OnlineStore.DeviceLibrary
{
    /// <summary>
    /// 压紧机构计量检测信号处理
    /// </summary>
    public class MeteringSignal
    {
        public static int ComAxisChangeValue = ConfigAppSettings.GetIntValue(Setting_Init.ComAxisChangeValue);
        /// <summary>
        /// 信号变化次数
        /// </summary>
        public static int  SignalChangeCount=0;
        public static int TragetChangeCount = 0;
        public static IO_VALUE PreSignalValue = IO_VALUE.LOW;
        private static System.Timers.Timer timer = null;
        public static void StartCheck(int targetPositon, int currPosition)
        {
            int chazhi = Math.Abs(targetPositon - currPosition);
            int count = chazhi / ComAxisChangeValue;
            if (count > 0 && chazhi > ComAxisChangeValue)
            {
                StartCheck(count);
            }
        }
        private static void StartCheck(int targetCount)
        {
            TragetChangeCount = targetCount;
            if (timer == null)
            {
                timer = new System.Timers.Timer();
                timer.Interval = 50;
                timer.AutoReset = true;
                timer.Elapsed += Timer_Elapsed;
                timer.Enabled = false;
            }
            LogUtil.debug("开始压紧轴计量检测,预计变化" + TragetChangeCount + "次");
            IsInProcess = false;
            PreSignalValue = IO_VALUE.LOW;
            SignalChangeCount = 0;
            timer.Start();
        }

        public static void StopCheck()
        {
            if (timer==null)
            { return; }
            timer.Stop();
            TragetChangeCount = 0;
            SignalChangeCount = 0;
        }
        private static  bool IsInProcess = false;
        protected static void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (IsInProcess) { return; }
            IsInProcess = true;


            IO_VALUE value = IOManager.IOValue(IO_Type.CompressAxis_Check);
            if (value.Equals(IO_VALUE.HIGH) && PreSignalValue.Equals(IO_VALUE.LOW))
            {
                SignalChangeCount++;
                LogUtil.debug("检测到信号变化,已经变化" + SignalChangeCount + "次");
            }
            PreSignalValue = value;
            if (TragetChangeCount <= SignalChangeCount)
            {
                timer.Stop();
            }
            IsInProcess = false;
        }
    }
}