当前位置:网站首页>C# 实现PLC的定时器
C# 实现PLC的定时器
2022-07-31 03:39:00 【罗迪尼亚的熔岩】
感谢余工!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
{
/* * 类库名称:延时指令FB * 创建时间:2020.12.28 * 修改时间:2020.12.28 * 使用说明: * 1.访问器 只读 时间过程值获取 double subTimeUser * 2.访问器 只写 时间设定 double setTimeUser * 3.访问器 只写 计时器触发 bool timerOn * 4.访问器 只读 时间到达标志 bool timerCheckUser */
#region 私有变量
private double subTime;//时间相差数
private DateTime dtRecord;//时间记忆
private bool firstTimeRecord;//运行第一次记录标志
private double setTime;//设定时间
private bool timerCheck;//时间到
#endregion
#region 公共访问器
/// <summary>
///时间过程值 只读
/// </summary>
public double subTimeUser //只读
{
get {
return subTime; }
}
/// <summary>
/// 时间设定 只写
/// </summary>
public double setTimeUser //只写
{
set
{
setTime = value;
}
}
/// <summary>
/// 计时器触发 只写
/// </summary>
public bool timerOn //只写
{
set
{
timerOnFun(value);
}
}
/// <summary>
/// 时间到达标志 只读
/// </summary>
public bool timerCheckUser //只读
{
get {
return timerCheck; }
}
#endregion
#region 私有方法
/// <summary>
/// 计数开始方法(开始标志)
/// </summary>
/// <param name="on"></param>
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";
}
}
}
}
边栏推荐
- Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
- RESTful api interface design specification
- Addition and Subtraction of Scores in LeetCode Medium Questions
- RESTful api接口设计规范
- Mysql 45 study notes (23) How does MYSQL ensure that data is not lost
- LeetCode每日一练 —— 138. 复制带随机指针的链表
- IDEA comment report red solution
- type_traits元编程库学习
- $attrs/$listeners
- 数据库文件中的未分配的块和未使用的块的区别
猜你喜欢

Redis implements distributed locks

Port inspection steps - 7680 port analysis - Dosvc service

SIP协议标准和实现机制

How to develop a high-quality test case?

With 7 years of experience, how can functional test engineers improve their abilities step by step?

立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业

IDEA常用快捷键与插件

Mysql 45 study notes (23) How does MYSQL ensure that data is not lost

LeetCode每日一练 —— 138. 复制带随机指针的链表
![[Compilation principle] Lexical analysis program design principle and implementation](/img/eb/035234a402edf18beb7e2f82ebec17.png)
[Compilation principle] Lexical analysis program design principle and implementation
随机推荐
Based on the local, linking the world | Schneider Electric "Industrial SI Alliance" joins hands with partners to go to the future industry
[Paper reading] Mastering the game of Go with deep neural networks and tree search
The distance value between two arrays of LeetCode simple questions
"A daily practice, happy water problem" 1331. Array serial number conversion
[CV project debugging] CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT problem
(六)枚举、注解
Safety 20220722
(Line segment tree) Summary of common problems of basic line segment tree
[AUTOSAR-RTE]-5-Explicit (explicit) and Implicit (implicit) Sender-Receiver communication
LeetCode每日一练 —— OR36 链表的回文结构
Redis 统计用户新增和留存
some of my own thoughts
立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业
Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
RESTful api interface design specification
Port inspection steps - 7680 port analysis - Dosvc service
Recursive query single table - single table tree structure - (self-use)
Addition and Subtraction of Scores in LeetCode Medium Questions
$attrs/$listeners
ClickHouse:设置远程连接