跳到主要内容

SCL 函数与功能块

函数用于复用计算,功能块用于保存跨扫描状态。重复算法用方法;定时、计数、边沿和锁存用稳定实例。当前标准功能块分类只有 10 个可执行条目。

类别​

类别符号可用性说明
类方法class 中的方法可执行无状态计算,末尾返回结果
定时器TON、TOF、TP可执行接通延时、断开延时、定宽脉冲
计数器CTU、CTD、CTUD可执行加、减、加减计数
边沿检测R_TRIG、F_TRIG可执行上升沿、下降沿
双稳态SR、RS可执行置位优先、复位优先
TONR保持型定时器不可用不属于这 10 个标准功能块
CTU_DINT / CTD_DINT / CTUD_DINT宽整数计数器不可用当前没有独立宽整数计数块
系统功能块READ_CLK、WR_SYS_T、RD_SYS_T不可用墙钟读取走标准函数 RTC()
.Execute() / .Q / .ET / .CV成员接口不可用主结果是调用表达式的 bool

完整参数以 实例与扫描 为准。

访问​

形态访问当前合同
无修饰参数输入可传变量、字面量或表达式
out输出传入已声明的可写变量
ref输入输出传入已声明的可写变量
in只读输入作为输入使用
命名参数name: value名称必须来自当前签名
功能块输入调用实参优先命名参数,如 IN:、PT:
功能块主结果调用返回 bool不要读 .Q、.QU、.QD
功能块附加输出不提供不要读 .ET、.CV
执行入口直接调用实例不要写 Execute()

out var 或 out int value 内联声明当前不可用,应先声明变量。

class MathTools
{
public int Clamp(int value, int low, int high)
{
int result = value;

if (value < low)
{
result = low;
}
else if (value > high)
{
result = high;
}

return result;
}
}

MathTools tools = new MathTools();
int safeValue = tools.Clamp(120, 0, 100);

不要依赖方法中途的带值 return 提前跳出;先把结果写入局部变量,再在末尾返回。

class CounterTools
{
public void Update(int step, out int next, ref int total, in bool enabled)
{
next = total;

if (enabled)
{
total = total + step;
next = total;
}
}
}

CounterTools counterTools = new CounterTools();
int nextValue = 0;
int totalValue = 10;
bool updateEnabled = true;
counterTools.Update(2, out nextValue, ref totalValue, updateEnabled);
int result = tools.Clamp(
value: 120,
low: 0,
high: 100
);

功能块实例在声明处建立,并在固定周期路径每扫描调用一次。PT 使用毫秒整数。同一扫描多次调用同一实例会多次推进状态;跳过调用时实例保留上次实际调用后的状态。

TON startDelay = new TON();
bool startCommand = false;
bool startReady = startDelay(IN: startCommand, PT: 2000);
TON onDelay = new TON();
TOF offDelay = new TOF();
TP pulseTimer = new TP();

bool input = false;
bool onReady = onDelay(IN: input, PT: 1000);
bool offHold = offDelay(IN: input, PT: 500);
bool pulse = pulseTimer(IN: input, PT: 200);
CTU upCounter = new CTU();
CTD downCounter = new CTD();
CTUD stockCounter = new CTUD();

bool upPulse = false;
bool downPulse = false;
bool reset = false;
bool load = false;
int preset = 10;

bool reached = upCounter(CU: upPulse, PV: preset, R: reset);
bool empty = downCounter(CD: downPulse, PV: preset, LD: load);
bool full = stockCounter(CU: upPulse, CD: downPulse, PV: preset, LD: load, R: reset);

计数输入按假到真的边沿变化。高速脉冲应使用硬件高速计数通道。

R_TRIG risingEdge = new R_TRIG();
F_TRIG fallingEdge = new F_TRIG();

bool signal = false;
bool risePulse = risingEdge(CLK: signal);
bool fallPulse = fallingEdge(CLK: signal);
SR setPriority = new SR();
RS resetPriority = new RS();

bool setCommand = false;
bool resetCommand = false;
bool setState = setPriority(S1: setCommand, R: resetCommand);
bool resetState = resetPriority(S: setCommand, R1: resetCommand);

每个设备或工艺对象使用独立实例。输入相同就应得到相同结果时用方法;需要记住上次输入或累计值时用功能块实例。