Nested usage of C# timer

Posted by tronicsmasta on Thu, 03 Oct 2019 14:52:21 +0200

Code background:
One of the processes of dynamic test bench. Step response test requirements

In short, the signal generator gives the test object a step signal, and the displacement sensor detects the displacement state of the test object after the signal is obtained.
In order to achieve this goal, the displacement sensor reads data as fast as possible, but the C # timer will lose data within 10 ms, but this is not the focus of our consideration, the focus is on the use of Timer: the signal generator once, the sensor according to the cycle of the signal generator.

 //Dynamic Cylinder Subclass
    public class Dynamic_Cylinder : SharedBase
    {
        //Step response test parameters
        public static int step_time = 0; 
        public static double[] Array3 = new double[1000];//Abscissa time 
        public static double[] Array4 = new double[1000]; //Ordinate displacement
        //Step response timer
        public System.Timers.Timer stepTimer3 = new System.Timers.Timer();
        public System.Timers.Timer stepTimer4 = new System.Timers.Timer();
        // Step response test method
        public void StepResponseTest()
        {
            //At this time, the hydraulic cylinder has been in the middle, allowing the hydraulic cylinder to run at the highest speed of 40% of the total stroke, that is, 12.8mm.
            stepTimer3.Start();
        }
        public int shortTime = 0;
        public double step = 8;
        //The step response test signal generator gives a square wave with a maximum speed of half*10ms.
        public void timer6_Tick(object sender, EventArgs e)
        {
            if (shortTime < 1)
            {
                server.InstantWrite<double>("Proportional servo valve VDS1_a", 5);
                shortTime++;
            }
            else
            {
                stepTimer3.Stop();
                stepTimer4.Start();
            }
        }
        //Acquisition of step response frequency
        public void timer7_Tick(object sender, EventArgs e)
        {
            //According to square wave, the stroke of hydraulic cylinder should be from 16 mm to 28.8 mm.
            if (Dynamic_Cylinder.Hydraulic cylinder stroke <28.8)
            { 
                //Acquisition of Very Longitudinal Coordinate Points of Hydraulic Cylinder Response Curve into Two Arrays
                Dynamic_Cylinder.Array3[Dynamic_Cylinder.Steprecorder] = Dynamic_Cylinder.step_time;
                Dynamic_Cylinder.Array4[Dynamic_Cylinder.Steprecorder] =  Dynamic_Cylinder.Hydraulic cylinder stroke;

                ++Dynamic_Cylinder.Steprecorder;
                //Ignore: The following two lines of code can be replaced by the last line of comment in a formal hardware connection test
                ++Dynamic_Cylinder.step_time;
                Dynamic_Cylinder.Hydraulic cylinder stroke = Dynamic_Cylinder.Hydraulic cylinder stroke + step;
                step = step*4/5;
                // Dynamic_Cylinder.Hydraulic cylinder stroke= server.InstantRead<double>("displacement sensor LVDT");
            }
            else
            {
                stepTimer4.Stop();
                for (int j = 0; j <= Dynamic_Cylinder.Steprecorder; j++)//Mapping
                {
                    //That is, testGraphInfo. List. Add (MainForm. ps1, MainForm. wy1 /(MainForm. ps1*a)) whose ordinate is wy1 /(ps1*a) 
                    string str = string.Format("Collection first{0}Group data", j);
                    LOG.Info(str);
                    testGraphInfo.List.Add(Dynamic_Cylinder.Array3[j], Dynamic_Cylinder.Array4[j]);
                }
                TestEndEvent();
            }
            

Timer in c # is self-threaded, so debugging needs to take this into account.
In addition, I opened a test thread in the test class, and did Timer event binding in the run() function body of this thread. To simplify, I deleted all other test contents.
(Test==11 is the flag of step response test)

 public override void Run(Object stateInfo)
        {
            LOG.Info("Test start...");
            //recorder.StartRecord(); // Start Test Record
            try
            {
                LOG.Info("Start recording data");
                //If it is a step response test
                if (MainForm.test == 10)
                {
                    stepTimer1.Enabled = true;
                    stepTimer1.Interval = 100;
                    stepTimer1.Elapsed += timer4_Tick;//Event binding
                    stepTimer2.Enabled = true;
                    stepTimer2.Interval = 10;
                    stepTimer2.Elapsed += timer5_Tick;//Event binding
                }
                //Obtaining experimental data package
                istested = true;
                //Stop recording test
                // recorder.EndRecort();      
                // LOG.Info("The experiment is over, please print the experiment report...");
            }
            catch (Exception e)
            {
                LOG.Debug(e);
                throw e;
            }
            finally
            {
              if (MainForm.test == 11)
                    StepResponseTest();
                else
                    TestEndEvent();
            }
        }