当前位置:网站首页>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";
}
}
}
}
边栏推荐
- Automation strategies for legacy systems
- 【AUTOSAR-RTE】-5-Explicit(显式)和Implicit(隐式) Sender-Receiver communication
- 端口排查步骤-7680端口分析-Dosvc服务
- Problems that need to be solved in distributed system architecture
- 立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业
- [Compilation principle] Design principle and implementation of recursive descent parsing
- (tree) Last Common Ancestor (LCA)
- 5. How does the SAP ABAP OData service support the $filter operation
- Safety 20220712
- Safety 20220709
猜你喜欢

Daily practice of LeetCode - 138. Copy a linked list with random pointers

LeetCode每日一练 —— OR36 链表的回文结构

BP神经网络

Daily practice of LeetCode - palindrome structure of OR36 linked list

识Flutter 基本组件之showTimePicker 方法

IDEA comment report red solution

type_traits metaprogramming library learning

【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.

C语言从入门到如土——数据的存储

Recursive query single table - single table tree structure - (self-use)
随机推荐
[C language] Preprocessing operation
Automation strategies for legacy systems
Point Cloud DBSCAN Clustering (MATLAB, not built-in function)
浅识Flutter 基本组件之CheckboxListTile组件
【AUTOSAR-RTE】-4-Port and Interface and Data Type
Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
Just debuted "Fight to Fame", safety and comfort are not lost
What is a system?
Redis uses LIST to cache the latest comments
els 方块向左移动条件判断
(线段树) 基础线段树常见问题总结
(四)递归、可变参数、访问修饰符、理解 main 方法、代码块
Can‘t load /home/Iot/.rnd into RNG
els block to the left to move the conditional judgment
(5) final, abstract class, interface, inner class
Bubble sort, selection sort, insertion sort, binary search directly
安全20220712
LeetCode每日一练 —— 138. 复制带随机指针的链表
Web container and IIS --- Middleware penetration method 1
log level and print log note