当前位置:网站首页>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";
}
}
}
}
边栏推荐
- [AUTOSAR-RTE]-5-Explicit (explicit) and Implicit (implicit) Sender-Receiver communication
- 想从手工测试转岗自动化测试,需要学习哪些技能?
- Select the smoke test case, and make the first pass for the product package entering QA
- Safety 20220712
- Recursive query single table - single table tree structure - (self-use)
- 端口排查步骤-7680端口分析-Dosvc服务
- The Map Entry understanding and application
- LocalDate addition and subtraction operations and comparison size
- [Swift] Customize the shortcut that pops up by clicking the APP icon
- 浅识Flutter 基本组件之CheckboxListTile组件
猜你喜欢

Use of QML

Problems that need to be solved in distributed system architecture

Component pass value provide/inject

组件传值 provide/inject

7年经验,功能测试工程师该如何一步步提升自己的能力呢?

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

(4) Recursion, variable parameters, access modifiers, understanding main method, code block

识Flutter 基本组件之showTimePicker 方法
![[Dynamic programming] Maximum sum of consecutive subarrays](/img/3d/10731cc64d1c69d2beb3666ae0f064.png)
[Dynamic programming] Maximum sum of consecutive subarrays

IIR filter and FIR filter
随机推荐
els 方块向右移动边界判断、向下加速
Golang中的addressable
[Swift] Customize the shortcut that pops up by clicking the APP icon
安全20220712
[Godot][GDScript] 2D cave map randomly generated
Know the showTimePicker method of the basic components of Flutter
log level and print log note
BP神经网络
Day32 LeetCode
安全20220709
Just debuted "Fight to Fame", safety and comfort are not lost
Recursive query single table - single table tree structure - (self-use)
[C language] Three-pointed chess (classic solution + list diagram)
Good place to download jar packages
The application and practice of mid-to-platform brand advertising platform
interprocess communication
postgresql 15源码浅析(5)—— pg_control
els block to the left to move the conditional judgment
(4) Recursion, variable parameters, access modifiers, understanding main method, code block
分布式锁以及实现方式三种