栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

C# 实现PLC的定时器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C# 实现PLC的定时器

感谢余工!b站 https://space.bilibili.com/241846092?spm_id_from=333.337.search-card.all.click

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsTon
{
    public class DelayTime
    {
        

        #region 私有变量

        private double subTime;//时间相差数
        private DateTime dtRecord;//时间记忆
        private bool firstTimeRecord;//运行第一次记录标志
        private double setTime;//设定时间
        private bool timerCheck;//时间到

        #endregion

        #region 公共访问器
        /// 
        ///时间过程值 只读
        /// 
        public double subTimeUser //只读
        {
            get { return subTime; }
        }

        /// 
        /// 时间设定 只写
        /// 
        public double setTimeUser //只写
        {
            set
            {
                setTime = value;
            }
        }

        /// 
        /// 计时器触发 只写
        /// 
        public bool timerOn //只写
        {
            set
            {
                timerOnFun(value);
            }
        }

        /// 
        /// 时间到达标志 只读
        /// 
        public bool timerCheckUser //只读
        {
            get { return timerCheck; }
        }
        #endregion

        #region 私有方法

        /// 
        /// 计数开始方法(开始标志)
        /// 
        /// 
        private void timerOnFun(bool on)
        {
            if (on) //当前延时计算开始
            {
                if (!firstTimeRecord) //当前第一次获取时间值标志
                {
                    dtRecord = DateTime.Now; //赋值至时间记录
                    firstTimeRecord = true; //已读取第一次时间记忆
                }

                subTime = (DateTime.Now - dtRecord).TotalMilliseconds; //获取当前时间与上一次时间的差值

                if (subTime >= setTime) //若差值比设置值大则输出延时到达信号
                {
                    timerCheck = true;
                }
            }
            else
            {
                firstTimeRecord = false; //消除第一次获取时间值标志
                timerCheck = false;
            }
        }





        #endregion

    }
}

namespace WinFormsTon
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            this.label1.Text = "0";
            this.label2.Text = "0";
        }

        private int a = 0;
        private DelayTime ton = new DelayTime();


        private void btnStart_Click_1(object sender, EventArgs e)
        {

            Task.Run(() =>
            {
                while (true)
                {
                    ton.setTimeUser = 5000.0; //ms
                    ton.timerOn = true;
                };
            });
            


        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.label1.Text = ton.subTimeUser.ToString();

            if (ton.timerCheckUser)
            {
                this.label2.Text = "200";
            }

        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032200.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号