Skip to main content

SA0043:仅在一个 POU 中使用全局变量

仅在单个 POU 中检测全局变量的使用

理由:仅在一个位置使用的全局变量也只能在该位置声明。

重要性:中等

PLCopen规则:CP26

52. 例子
VAR_GLOBAL
    g_xVar AT %QX0.0 : BOOL ;
    g_iTest AT %QW1 : INT ;
    g_wTest AT %QW2 : WORD;
END_VAR
PROGRAM prog1
VAR
    iCondition : INT;
    bTemp : BOOL;
END_VAR
iCondition := iCondition + INT#1;
IF iCondition < INT#0 THEN
    bTemp := g_xVar;    // SA0043 - g_xVar only read in this POU
    ELSIF iCondition = INT#0 THEN
        bTemp := g_xVar;    // SA0043 - g_xVar only read in this POU
ELSE
    bTemp := g_xVar;    // SA0043 - g_xVar only read in this POU
    g_wTest := WORD#4;    // g_WTest used also in prog2 -> OK
END_IF
PROGRAM prog2
VAR
    iCondition : INT;
END_VAR
iCondition := iCondition + INT#1;
CASE iCondition OF
    INT#1:
        g_iTest := WORD_TO_INT(g_wTest);    // SA0043 - g_iTest only written in this POU
    INT#2:
        g_iTest := INT#2;    // SA0043 - g_iTest only written in this POU
    ELSE
        g_iTest := INT#3;    // SA0043 - g_iTest only written in this POU
END_CASE

输出在 留言 看法:

  • sa_icon_message.png SA0043:全局变量“g_xVar”仅在“prog1”中使用

  • sa_icon_message.png SA0043:全局变量“g_iTest”仅在“prog2”中使用