当前位置:网站首页>在C# 中实现上升沿,并模仿PLC环境验证 If 语句使用上升沿和不使用上升沿的不同
在C# 中实现上升沿,并模仿PLC环境验证 If 语句使用上升沿和不使用上升沿的不同
2022-07-05 10:21:00 【罗迪尼亚的熔岩】
PLC可以是认为跑在while 死循环里的程序(Ob100除外),每一个OB块都是一个单独的线程。了解了这点,就可以更好的进行C# 上位机编程和 PLC的SCL(ST)编程。
在SCL中,如果If 语句的条件使用了长信号,If语句会反复执行,一些交换值,传递值,更新值的操作就无法完成,这时候If语句的条件必须要使用瞬态信号,If 的条件使用上升沿是一个好选择。
上升沿:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FPTest
{
public class R_TRIG
{
///
/// 这个属性存储上一次的bool状态,get;private set;这个写法相当于PLC的 Output接口
///
public bool Last {
get; private set; }
///
/// 这个属性填被检测的bool量,set;相当于PLC的Input接口
///
public bool CLK
{
set
{
Q = value && !Last;//我们知道上升沿是从0变1的一瞬间,所以本次扫描为真上次为假时就产生了上升沿
Last = value;//每个扫描周期刷新参考位
}
}
///
/// 这个就是检测的状态,外部获取这个变量就知道上升沿有没有产生
/// 相当于PLC的 Output接口
public bool Q {
get; private set; }
}
}
下降沿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FPTest
{
public class F_TRIG
{
public bool Last {
get; private set; }
public bool CLK
{
set
{
Q = !value && Last;//1变0 上次为真本次为假
Last = value;
}
}
public bool Q {
get; private set; }
}
}
主程序
namespace FPTest
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
rTrig = new R_TRIG();
fTrig = new F_TRIG();
this.label1.Text = a.ToString();
this.label2.Text = b.ToString();
}
private R_TRIG rTrig;
private F_TRIG fTrig;
private int a = 0;
private int b = 0;
CancellationTokenSource cts = new CancellationTokenSource();
private void btnUp_Click(object sender, EventArgs e)
{
Task.Run(async () =>
{
while (!cts.IsCancellationRequested)
{
await Task.Delay(200);
rTrig.CLK = true;
if (rTrig.Q)
{
a = b;
b = int.Parse(this.textBox1.Text.Trim()) ;
}
this.label1.Invoke(new Action(() => {
this.label1.Text = a.ToString(); }));
this.label2.Invoke(new Action(() => {
this.label2.Text = b.ToString(); }));
}
}, cts.Token);
}
private void NoRTrig_Click(object sender, EventArgs e)
{
Task.Run(async () =>
{
while (!cts.IsCancellationRequested)
{
await Task.Delay(200);
if (true)
{
a = b;
b = int.Parse(this.textBox1.Text.Trim());
}
this.label1.Invoke(new Action(() => {
this.label1.Text = a.ToString(); }));
this.label2.Invoke(new Action(() => {
this.label2.Text = b.ToString(); }));
}
}, cts.Token);
}
}
}

初始状态

使用上升沿,If中执行了一次

不使用上升沿,a,b无法传递值
边栏推荐
- 一个可以兼容各种数据库事务的使用范例
- IDEA新建sprintboot项目
- Excerpt from "sword comes" (VII)
- C function returns multiple value methods
- 【js学习笔记五十四】BFC方式
- dsPIC33EP 时钟初始化程序
- 小程序中自定义行内左滑按钮,类似于qq和wx消息界面那种
- > Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
- 流程控制、
- flex4 和 flex3 combox 下拉框长度的解决办法
猜你喜欢

mongoDB副本集
![[论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation](/img/fa/d2061bc7bd437f062d46a009cf32cf.png)
[论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation

微信核酸检测预约小程序系统毕业设计毕设(8)毕业设计论文模板

非技术部门,如何参与 DevOps?

csdn软件测试入门的测试基本流程
![[dark horse morning post] Luo Yonghao responded to ridicule Oriental selection; Dong Qing's husband Mi Chunlei was executed for more than 700million; Geely officially acquired Meizu; Huawei releases M](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] Luo Yonghao responded to ridicule Oriental selection; Dong Qing's husband Mi Chunlei was executed for more than 700million; Geely officially acquired Meizu; Huawei releases M

微信核酸检测预约小程序系统毕业设计毕设(7)中期检查报告

【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...

5g NR system architecture
![[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems](/img/6c/5b14f47503033bc2c85a259a968d94.png)
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
随机推荐
[vite] 1371 - develop vite plug-ins by hand
What are the top ten securities companies? Is it safe to open an account online?
2022年流动式起重机司机考试题库及模拟考试
Have the bosses ever encountered such problems in the implementation of flinksql by Flink CDC mongdb?
沟通的艺术III:看人之间 之倾听
数据库中的范式:第一范式,第二范式,第三范式
PHP solves the problems of cache avalanche, cache penetration and cache breakdown of redis
Solution to the length of flex4 and Flex3 combox drop-down box
"Everyday Mathematics" serial 58: February 27
[paper reading] kgat: knowledge graph attention network for recommendation
csdn软件测试入门的测试基本流程
伪类元素--before和after
小程序中自定义行内左滑按钮,类似于qq和wx消息界面那种
ModuleNotFoundError: No module named ‘scrapy‘ 终极解决方式
What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
5g NR system architecture
Pseudo class elements -- before and after
How can PostgreSQL CDC set a separate incremental mode, debezium snapshot. mo
DOM//
Activity jump encapsulation