当前位置:网站首页>在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无法传递值
边栏推荐
- 非技术部门,如何参与 DevOps?
- DOM//
- 流程控制、
- 小程序框架Taro
- 【Vite】1371- 手把手开发 Vite 插件
- Excerpt from "sword comes" (VII)
- Golang应用专题 - channel
- Go项目实战—参数绑定,类型转换
- Today in history: the first e-book came out; The inventor of magnetic stripe card was born; The pioneer of handheld computer was born
- What is the most suitable book for programmers to engage in open source?
猜你喜欢
In the year of "mutual entanglement" of mobile phone manufacturers, the "machine sea tactics" failed, and the "slow pace" playing method rose
[observation] with the rise of the "independent station" model of cross-border e-commerce, how to seize the next dividend explosion era?
到底谁才是“良心”国产品牌?
Idea create a new sprintboot project
A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
“军备竞赛”时期的对比学习
Redis如何实现多可用区?
Learning note 4 -- Key Technologies of high-precision map (Part 2)
How do programmers live as they like?
Today in history: the first e-book came out; The inventor of magnetic stripe card was born; The pioneer of handheld computer was born
随机推荐
C language QQ chat room small project [complete source code]
AtCoder Beginner Contest 258「ABCDEFG」
变量///
微信核酸检测预约小程序系统毕业设计毕设(7)中期检查报告
flink cdc不能监听mysql日志,大家遇到过这个问题吧?
Workmanager learning 1
dsPIC33EP 时钟初始化程序
beego跨域问题解决方案-亲试成功
一个可以兼容各种数据库事务的使用范例
Have the bosses ever encountered such problems in the implementation of flinksql by Flink CDC mongdb?
Today in history: the first e-book came out; The inventor of magnetic stripe card was born; The pioneer of handheld computer was born
SQL Server 监控统计阻塞脚本信息
flex4 和 flex3 combox 下拉框长度的解决办法
What is the most suitable book for programmers to engage in open source?
Who is the "conscience" domestic brand?
A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
How to write high-quality code?
微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
IDEA新建sprintboot项目
微信核酸检测预约小程序系统毕业设计毕设(8)毕业设计论文模板