博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#计时器timer的嵌套用法
阅读量:4112 次
发布时间:2019-05-25

本文共 3691 字,大约阅读时间需要 12 分钟。

代码背景:

动态试验台流程之一.阶跃响应试验需求
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190816205327872.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NtaWxlMDAxaXNtZQ==,size_16,color_FFFFFF,t_7

简而言之,就是信号发生器给试验对象一阶跃信号,位移传感器检测试验对象得到信号后的位移状态。

为了达到目的,位移传感器读取数据的频率要尽可能地快,但是C#的计时器Timer在10ms之内就会出现数据丢失,不过这不是我们要考虑的重点,重点是Timer的使用:信号发生器一次,传感器按信号发生器的周期进行。

//动态油缸子类    public class Dynamic_Cylinder : SharedBase    {        //阶跃响应试验参数        public static int step_time = 0;         public static double[] Array3 = new double[1000];//横坐标时间         public static double[] Array4 = new double[1000]; //纵坐标位移        //阶跃响应专用计时器        public System.Timers.Timer stepTimer3 = new System.Timers.Timer();        public System.Timers.Timer stepTimer4 = new System.Timers.Timer();        // 阶跃响应试验方法        public void StepResponseTest()        {            //此时液压缸已在中位,让液压缸以最高速度运行总行程的40%,即12.8mm            stepTimer3.Start();        }        public int shortTime = 0;        public double step = 8;        //阶跃响应试验信号发生计时器,给一个最高速度一半*10ms的方波        public void timer6_Tick(object sender, EventArgs e)        {            if (shortTime < 1)            {                server.InstantWrite
("比例伺服阀VDS1_a", 5); shortTime++; } else { stepTimer3.Stop(); stepTimer4.Start(); } } //采集阶跃响应频率 public void timer7_Tick(object sender, EventArgs e) { //根据方波,液压缸行程应该是从16mm到28.8mm if (Dynamic_Cylinder.液压缸行程 <28.8) { //把液压缸响应曲线的很纵坐标点分别采集到两个数组里 Dynamic_Cylinder.Array3[Dynamic_Cylinder.Steprecorder] = Dynamic_Cylinder.step_time; Dynamic_Cylinder.Array4[Dynamic_Cylinder.Steprecorder] = Dynamic_Cylinder.液压缸行程; ++Dynamic_Cylinder.Steprecorder; //Ignore:以下两行代码在正式连硬件试验时可以用最后一行注释代替 ++Dynamic_Cylinder.step_time; Dynamic_Cylinder.液压缸行程 = Dynamic_Cylinder.液压缸行程 + step; step = step*4/5; // Dynamic_Cylinder.液压缸行程 = server.InstantRead
("位移传感器LVDT"); } else { stepTimer4.Stop(); for (int j = 0; j <= Dynamic_Cylinder.Steprecorder; j++)//绘图 { //即testGraphInfo.List.Add(MainForm.ps1, MainForm.wy1 / (MainForm.ps1 * a))纵坐标就是wy1/(ps1*a) string str = string.Format("采集第{0}组数据", j); LOG.Info(str); testGraphInfo.List.Add(Dynamic_Cylinder.Array3[j], Dynamic_Cylinder.Array4[j]); } TestEndEvent(); }

c#里的Timer是自带线程的,所以调试的时候也需要考虑到这个问题。

另外,我在试验类中开了一个试验线程,在这个线程的run()函数体中做了Timer的事件绑定,为了简化,我把其它试验内容都删掉了
(Test==11是阶跃响应试验的flag)

public override void Run(Object stateInfo)        {            LOG.Info("试验开始...");            //recorder.StartRecord();    //开始试验记录            try            {                LOG.Info("开始记录数据");                //如果是阶跃响应试验                if (MainForm.test == 10)                {                    stepTimer1.Enabled = true;                    stepTimer1.Interval = 100;                    stepTimer1.Elapsed += timer4_Tick;//事件绑定                    stepTimer2.Enabled = true;                    stepTimer2.Interval = 10;                    stepTimer2.Elapsed += timer5_Tick;//事件绑定                }                //获取实验数据包                istested = true;                //停止记录试验                // recorder.EndRecort();                      // LOG.Info("试验结束,请打印实验报告...");            }            catch (Exception e)            {                LOG.Debug(e);                throw e;            }            finally            {              if (MainForm.test == 11)                    StepResponseTest();                else                    TestEndEvent();            }        }

转载地址:http://lbrsi.baihongyu.com/

你可能感兴趣的文章
在Eclipse中查看Android源码
查看>>
Android-Socket登录实例
查看>>
Android使用webservice客户端实例
查看>>
层在页面中的定位
查看>>
[转]C语言printf
查看>>
C 语言 学习---获取文本框内容及字符串拼接
查看>>
C 语言学习 --设置文本框内容及进制转换
查看>>
C 语言 学习---判断文本框取得的数是否是整数
查看>>
C 语言 学习---ComboBox相关、简单计算器
查看>>
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第四章 - 程序计数器
查看>>
第七章 - 本地方法栈
查看>>
第十一章 - 直接内存
查看>>
JDBC核心技术 - 上篇
查看>>
JDBC核心技术 - 下篇
查看>>
一篇搞懂Java反射机制
查看>>
一篇彻底搞懂Java注解与枚举类
查看>>
【2021-MOOC-浙江大学-陈越、何钦铭-数据结构】树
查看>>