之前一直使用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自身的循環掃描特性就保證了資料的一緻性)。而如果我們讀取配置設定的實參,則保證了讀取數值的有效性(非計算過程值)。
是以,為了避免後期可能出現的問題,我們在函數内部還是應該遵守,輸入類型參數隻讀,輸出類型參數隻寫,且隻寫一次。