之前一直使用TIA V14(再早还有V13),新版本发布后也更新过,今年更新了V16版,发现了一个情况,在之前版本中,FB函数块中定义的输出类型变量是不能够在函数中读取使用的。
比如我们在参数接口处定义了两个参数
Output_1: Int类型的Output接口参数;
Static_1:Static静态 Int类型的参数;
在V14中,如下图

上面这样每次编译的时候都会有报警,为了避免这种情况我们常用的方法是再声请一个static类型的Int变量,用于代替Output_1作为程序中间运算,而在程序的最后才将这个static类型的Int数值赋值给Output_1最终输出。
而V16版本则没有相关的报警,即默认可以读取输出类型的变量。
后来查阅帮助资料,发现如下
Input parameters (Input)
Input parameters are only read once before each block call. Therefore, the rule is that writing an input parameter within the block does not affect the actual parameter. Only the formal parameter is written.
Output parameters (Output)
Output parameters are only read once after each block call. Therefore, the rule is that output parameters should not be read within the block. If you nevertheless read an output parameter, please note that only the value of the formal parameter is read. The value of the actual parameter cannot be read within the block.
总结如下:
输入类型的变量,仅在调用FB的时候读入一次,即将实参赋值给形参。
输出类型的变量,仅在FB被调用结束后刷新输出一次,即将形参赋值给实参。
在函数内部,形参(包括输入和输出)其实都可以读取和写入,只不过这些函数内部的读取和写入都不会影响实参。
如果我们外部程序(PG,HMI等以时间片来通信的程序)直接读取函数内部的输出类型形参,可能是函数内部计算的中间量而非最终输出的数值(当然PLC程序内部即便读取的是形参也没有问题,因为PLC自身的循环扫描特性就保证了数据的一致性)。而如果我们读取分配的实参,则保证了读取数值的有效性(非计算过程值)。
所以,为了避免后期可能出现的问题,我们在函数内部还是应该遵守,输入类型参数只读,输出类型参数只写,且只写一次。