在图表上

时间:2019-02-27 11:17:35

标签: c# winforms plc

下面的图表每秒使用Winforms计时器更新其值。红线表示恒定的目标测试压力,蓝线表示从PLC对象读取的实际压力。

Y Axis = Testing Pressure, X Axis = Current Time, chart is updated with winforms timer at Interval = 1000 (every second)

需求显示蓝线达到恒定的所需测试压力(红线)到低于恒定的测试压力之间经过了多少秒
恒定的所需测试压力。

设置恒定的所需测试压力的块:

...
        chart1.ChartAreas[0].CursorY.Position = d;
        chart1.ChartAreas[0].CursorY.LineWidth = 1;
        chart1.ChartAreas[0].CursorY.LineColor = System.Drawing.Color.Red;

我卡住的部分(此块位于每秒更新图表的方法之内):

                                                                        double plcTestpressure = ((uint)plc.Read("MD220")).ConvertToDouble();
                    double reqTestPressure = Convert.ToDouble(txtTestingPressure.Text);

                    if (plcTestpressure > reqTestPressure && !isAboveReq)
                    {
                        DateTime aboveReq = new DateTime();
                        aboveReq = DateTime.Now;
                        isAboveReq = true;
                        //this is for checking the plc read pressure
                        string currentpressure = ((uint)plc.Read("MD220")).ConvertToDouble().ToString();
                    }
                    //check if current pressure is below required and that pressure WAS above required a second ago...
                    if(plcTestpressure < reqTestPressure && isAboveReq)
                    {
                        DateTime belowReq = new DateTime();
                        belowReq = DateTime.Now;                    
                        tickCounter = (belowReq - aboveReq).TotalSeconds;
                        isAboveReq = false;
                    }

我已经尝试并逐步完成了此块,但是它给了我tickCounter一个误导性的答案(33秒,当您可以在图形上直观地看到5秒过去了),并且在第一次将tickCounter分配给它之后,aboveReq日期时间戳不希望更改。

是否有更好的方法来实现这一目标?我要错了吗?我应该提供更多细节吗?

1 个答案:

答案 0 :(得分:0)

我必须假设您有多个名为“ aboveReq”的变量,因为在“ if”块中声明的变量是该块的局部变量。这意味着,当您在第二个“ if”块中访问“ aboveReq”变量时,就不会访问同一变量。

string currentpressure = ((uint)plc.Read("MD220")).ConvertToDouble().ToString();是否真的需要位于if块内(仅在高于目标时跟踪当前压力)?

//Outside of method, top of class
private DateTime? _startTime = null;
private DateTime? _endTime = null;

//In method
 string currentpressure = ((uint)plc.Read("MD220")).ConvertToDouble().ToString();

 bool breachPressure = plcTestpressure > reqTestPressure;

 if (breachPressure && _startTime == null)
 {
    _startTime = DateTime.Now;
 }
 else if(!breachPressure && _startTime != null)
 {
    _endTime = new DateTime();
    var tickCounter = _endTime.Value.Subtract(_startTime.Value).TotalSeconds;
 }

---------------------------------编辑----------------- ----------------------

  

我要错了吗?

如果将压力监控逻辑移到一个单独的类中,这将被认为是更干净的,从而符合单一责任原则。

您可以通过实现压力监控类来做到这一点,该类将在违反阈值时引发事件-类似于-

        public class PressureObserver
        {
            public event EventHandler<double> OnRaisedAboveThreshhold;
            public event EventHandler<double> OnFellBelowThreshhold;

            public double ThresholdPressure{ get; }

            private double _lastMeasured = 0; //Initial Pressure

            public PressureObserver(double thresholdPressure)
            {
                ThresholdPressure = thresholdPressure;
            }

            public void Observe(double plcTestpressure)
            {
                double pressureDelta = plcTestpressure - _lastMeasured;

                if (pressureDelta > 0) //Pressure climbed
                {
                    if(_lastMeasured < ThresholdPressure &&  //Last measurement was below threshold
                        plcTestpressure > ThresholdPressure) //This one is above, cross made
                    {
                        OnRaisedAboveThreshhold?.Invoke(this, plcTestpressure);
                    }
                }
                else if(pressureDelta < 0) //Pressure declined
                {
                    if (_lastMeasured > ThresholdPressure &&  //Last measurement was above threshold
                        plcTestpressure < ThresholdPressure) //This one is below, cross made
                    {
                        OnFellBelowThreshhold?.Invoke(this, plcTestpressure);
                    }
                }

                _lastMeasured = plcTestpressure;
            }
        }

然后在您的主班级,您将拥有字段

        private PressureObserver _pressureObserver;
        private DateTime _raisedAboveTime;
        private DateTime _fellBelowTime;
        private double _overpressureDuration;

您将定义两种对阈值变化做出反应的方法

        private void Obs_OnRaisedAboveTreshhold(object sender, double e)
        {
            //Code to do on raised above
            _raisedAboveTime = DateTime.Now;
        }

        private void Obs_OnFellBelowTreshhold(object sender, double e)
        {
            //Code to do on fell below
            _fellBelowTime = DateTime.Now;
            _overpressureDuration = _fellBelowTime.Subtract(_raisedAboveTime).TotalSeconds;
        }

在构造函数中,您将订阅观察者类

       _pressureObserver = new PressureObserver(60); //replace 60 with threshold

       _pressureObserver.OnRaisedAboveThreshhold += Obs_OnRaisedAboveTreshhold;
       _pressureObserver.OnFellBelowThreshhold += Obs_OnFellBelowTreshhold;

在滴答计时器中,您只需添加

_pressureObserver.Observe(plcTestpressure);