当前位置:网站首页>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";
}
}
}
}
边栏推荐
- 慧通编程第4关 - 魔法学院第6课
- 【AUTOSAR-RTE】-5-Explicit(显式)和Implicit(隐式) Sender-Receiver communication
- LeetCode每日一练 —— 138. 复制带随机指针的链表
- 《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
- Distributed locks and three implementation methods
- LeetCode simple problem to find the subsequence of length K with the largest sum
- 【AUTOSAR-RTE】-4-Port and Interface and Data Type
- Redis实现分布式锁
- Mysql 45 study notes (twenty-five) MYSQL guarantees high availability
- els block to the right
猜你喜欢
Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
$attrs/$listeners
[Paper reading] Mastering the game of Go with deep neural networks and tree search
LocalDate加减操作及比较大小
[Compilation principle] Lexical analysis program design principle and implementation
Know the showTimePicker method of the basic components of Flutter
5. How does the SAP ABAP OData service support the $filter operation
想从手工测试转岗自动化测试,需要学习哪些技能?
endian mode
Just debuted "Fight to Fame", safety and comfort are not lost
随机推荐
顺序表的实现
The application and practice of mid-to-platform brand advertising platform
Distributed locks and three implementation methods
Postgresql 15 source code analysis (5) - pg_control
BUG definition of SonarQube
「 每日一练,快乐水题 」1331. 数组序号转换
立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业
日志级别 和 打印log注意
RESTful api接口设计规范
(5) final, abstract class, interface, inner class
(4) Recursion, variable parameters, access modifiers, understanding main method, code block
Pytest e-commerce project combat (on)
Pytest电商项目实战(上)
安全20220712
Zotero如何删除自动生成的标签
【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
els 方块向左移动条件判断
(Line segment tree) Summary of common problems of basic line segment tree
【AUTOSAR-RTE】-5-Explicit(显式)和Implicit(隐式) Sender-Receiver communication
Good place to download jar packages