当前位置:网站首页>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";
}
}
}
}
边栏推荐
- The Map Entry understanding and application
- [AUTOSAR-RTE]-5-Explicit (explicit) and Implicit (implicit) Sender-Receiver communication
- [Compilation principle] Design principle and implementation of recursive descent parsing
- Daily practice of LeetCode - 138. Copy a linked list with random pointers
- Atomic operation CAS
- [shell basics] determine whether the directory is empty
- Redis implements distributed locks
- BUG definition of SonarQube
- Distributed locks and three implementation methods
- Why don't you programmers make a living off your own projects?And have to work for someone else?
猜你喜欢

LocalDate加减操作及比较大小

Detailed explanation of TCP (2)

一份高质量的测试用例如何养成?

识Flutter 基本组件之showTimePicker 方法

Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code

《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
![[C language] General method of expression evaluation](/img/59/cf43b7dd16c203b4f31c1591615955.jpg)
[C language] General method of expression evaluation
![[Compilation principle] Design principle and implementation of recursive descent parsing](/img/51/cd054a8246dc108520d6ff9ea26c60.png)
[Compilation principle] Design principle and implementation of recursive descent parsing

Use of QML

The distance value between two arrays of LeetCode simple questions
随机推荐
IDEA常用快捷键与插件
Key Technologies of Interface Testing
浅识Flutter 基本组件之showDatePicker方法
浅识Flutter 基本组件之CheckboxListTile组件
Redis 使用LIST做最新评论缓存
浅识Flutter 基本组件之CheckBox组件
Redis实现分布式锁
The application and practice of mid-to-platform brand advertising platform
VS QT - ui does not display newly added members (controls) || code is silent
Detailed explanation of TCP (2)
The els block moves the boundary to the right, and accelerates downward.
Why SocialFi achievement Web3 decentralized social in the future
[AUTOSAR-RTE]-5-Explicit (explicit) and Implicit (implicit) Sender-Receiver communication
"A daily practice, happy water problem" 1331. Array serial number conversion
安全20220712
els block to the right
Bubble sort, selection sort, insertion sort, binary search directly
LocalDate加减操作及比较大小
「 每日一练,快乐水题 」1331. 数组序号转换
IIR filter and FIR filter