当前位置:网站首页>在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无法传递值
边栏推荐
- What are the top ten securities companies? Is it safe to open an account online?
- How did automated specification inspection software develop?
- La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
- TSQL–标示列、GUID 、序列
- 《通信软件开发与应用》课程结业报告
- > Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
- [论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation
- 爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
- Interview: how does the list duplicate according to the attributes of the object?
- In the year of "mutual entanglement" of mobile phone manufacturers, the "machine sea tactics" failed, and the "slow pace" playing method rose
猜你喜欢

Today in history: the first e-book came out; The inventor of magnetic stripe card was born; The pioneer of handheld computer was born

Who is the "conscience" domestic brand?

> Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair

5G NR系统架构

学习笔记4--高精度地图关键技术(下)

LSTM应用于MNIST数据集分类(与CNN做对比)

Secteur non technique, comment participer à devops?

《天天数学》连载58:二月二十七日

How did automated specification inspection software develop?

Learning notes 5 - high precision map solution
随机推荐
Learning note 4 -- Key Technologies of high-precision map (Part 2)
官网给的这个依赖是不是应该为flink-sql-connector-mysql-cdc啊,加了依赖调
运算符、、
How can non-technical departments participate in Devops?
PHP solves the problems of cache avalanche, cache penetration and cache breakdown of redis
beego跨域问题解决方案-亲试成功
How to judge that the thread pool has completed all tasks?
SLAM 01.人类识别环境&路径的模型建立
Customize the left sliding button in the line in the applet, which is similar to the QQ and Wx message interface
想请教一下,十大券商有哪些?在线开户是安全么?
Timed disappearance pop-up
正则表达式
C语言活期储蓄账户管理系统
2022年流动式起重机司机考试题库及模拟考试
uniapp
5G NR系统架构
SAP ui5 objectpagelayout control usage sharing
C语言实现QQ聊天室小项目 [完整源码]
九度 1480:最大上升子序列和(动态规划思想求最值)
一个可以兼容各种数据库事务的使用范例