Totally Integrated
Automation Portal
Komora5_Marcin / komora5_plc [CPU 1214C DC/DC/Rly] / Program blocks / PB dodane
LGF_FIFO [FB10017]
LGF_FIFO Properties
General
Name            LGF_FIFO                   Number            10017                Type              FB                           Language        SCL
Numbering       Automatic
Information
Title                                      Author                                 Comment                                        Family
Version                                    User-defined ID
LGF_FIFO
Name                             Data type       Default value       Retain                 Accessible   Writ‐ Visible in Setpoint    Supervi‐   Comment
                                                                                            from         able HMI engi‐               sion
                                                                                            HMI/OPC      from neering
                                                                                            UA           HMI/
                                                                                                         OPC
                                                                                                         UA
   Input
       execute                   Bool            false               Non-retain             True         True True       False                   The instruction is executed
                                                                                                                                                 when a positive signal edge
                                                                                                                                                 is detected at the "request"
                                                                                                                                                 parameter.
       mode                      Bool            false               Non-retain             True         True True       False                   0 = The first entry in the ring
                                                                                                                                                 buffer is returned. 1 = An
                                                                                                                                                 entry is written at the last
                                                                                                                                                 position in the ring buffer.
       initialValue              Variant                                                    False        False False     False                   Value with which the ARRAY
                                                                                                                                                 of the ring buffer is initial‐
                                                                                                                                                 ized.
     resetBuffer                 Bool            false               Non-retain             True         True True       False
   Output
      done                       Bool            false               Non-retain             True         True   True     False
      error                      Bool            false               Non-retain             True         True   True     False
      statusID                   UInt            0                   Non-retain             True         True   True     False
      status                     Word            16#0                Non-retain             True         True   True     False                   Status information
   InOut
       item                      Variant                                                    False        False False     False                   The entry that is either re‐
                                                                                                                                                 turned from the ring buffer
                                                                                                                                                 or written to the ring buffer
       buffer                    Variant                                                    False        False False     False                   An ARRAY that is used as a
                                                                                                                                                 ring buffer.
   Static
       statEdgeupm               Bool            false               Non-retain             True         True True       False                   Edge memory bit in which
                                                                                                                                                 the RLO of the previous
                                                                                                                                                 query is saved.
       statFirstItemIndex        Int             -1                  Non-retain             True         True True       False                   Index of the oldest entry in
                                                                                                                                                 the ring buffer
       statNextEmptyItemIndex    Int             0                   Non-retain             True         True True       False                   Index of the next free entry
                                                                                                                                                 in the ring buffer
   Temp
       tempEdgeup                Bool                                                                                                            Result of edge evaluation
       tempInternalError         Int                                                                                                             Error information
       tempNewFirstItemIndex     Int                                                                                                             Variable index
       tempNewNextEmptyItemIn‐   Int                                                                                                             Variable index
       dex
       tempPufferSize            UDInt                                                                                                           Number of ARRAY elements
                                                                                                                                                 in the ring buffer
     tempSizeCounter             Int
   Constant
       NO_ERROR                  Word            16#0000
       NO_CURRENT_JOBS           Word            16#7000
       BUFFER_EMPTY              Word            16#8001
       BUFFER_FULL               Word            16#8002
       NO_ARRAY                  Word            16#8200
       WRONG_TYPE_1              Word            16#8201
       WRONG_TYPE_2              Word            16#8202
       INDEX_IN_ARRAY_LIMITS_1   Word            16#0
       INDEX_IN_ARRAY_LIMITS_2   Word            16#0
       ERROR_IN_THIS_BLOCK       UInt            1
       ERROR_MOVE_BLK_VARIANT    UInt            2
       NEXT_TASK_TO_EXECUTE      Bool            false
       BUFFER_IS_EMPTY           Int             -1
       BUFFER_CONTAINS_ARRAY     Int             0
       NO_INTERNAL_ERROR         Int             0
       BUFFER_INITIALIZED        Int             -1
       RESET                     Bool            true
0001    //=============================================================================
0002    // SIEMENS AG
0003    // (c)Copyright 2017
0004    //-----------------------------------------------------------------------------
0005    // Library: LGF (Library General Functions)
 Totally Integrated
 Automation Portal
0006   // Tested with: CPU1212C DC/DC/DC FW:V4.2
0007   // Engineering: TIA Portal V14
0008   // Restrictions: -
0009   // Requirements: PLC (S7-1200 / S7-1500)
0010   // Functionality: FIFO(First In First Out)-functionblock
0011   //-----------------------------------------------------------------------------
0012   // Change log table:
0013   // Version Date Expert in charge / Changes applied
0014   // 01.00.00 19.08.2015 Siemens Industry Online Support
0015   // First released version
0016   // 01.00.01 16.11.2015 Siemens Industry Online Support
0017   // Bug fix resetBuffer
0018   // 01.00.02 02.01.2017 Siemens Industry Online Support
0019   // Upgrade: TIA Portal V14 Update 1
0020   // 01.00.03 17.08.2018 Siemens Industry Online Support
0021   // Upgrade: TIA V15 Update 2
0022   //=============================================================================
0023
0024   //This program code section is only executed once after a negative signal edge.
0025   IF #execute = false AND #statEdgeupm = true THEN
0026     #done := false;
0027     // If no error occurred during program execution, the error code "0000" is output.
0028     #error := false;
0029     #statusID := #ERROR_IN_THIS_BLOCK;
0030     #status := #NO_CURRENT_JOBS;
0031   END_IF;
0032
0033   //This program code section is only executed once after a positive signal edge.
0034   //IF there is no change in the signal state OF the result OF logic operation,
0035   //the program processing OF the "FIFOQueue" FB is terminated.
0036
0037   IF #execute <> true OR #statEdgeupm <> false THEN
0038     #statEdgeupm := #execute;
0039     RETURN;
0040   END_IF;
0041   #statEdgeupm := #execute;
0042
0043   // ------Validation of whether all parameter inputs are valid.----
0044
0045   //This program code section checks whether the ring #buffer is an ARRAY.
0046   //IF so, the number OF the ARRAY elements is read out.
0047   //IF it is NOT an ARRAY, the program execution is terminated at this point
0048   //AND the status code "8200" is output.
0049
0050   IF NOT (IS_ARRAY(#buffer)) THEN
0051     #error := true;
0052     #statusID := #ERROR_IN_THIS_BLOCK;
0053     #status := #NO_ARRAY;
0054     RETURN;
0055   ELSE
0056     #tempPufferSize := CountOfElements(#buffer);
0057   END_IF;
0058
0059   //This program code section checks whether the data type OF the ARRAY elements matches
0060   //the data type OF the entry (tag #item). IF the data types DO NOT match,
0061   //the program execution is terminated at this point AND the error code "8201" is output.
0062
0063   IF NOT (TypeOf(#item) = TypeOfElements(#buffer)) THEN
0064     #error := true;
0065     #statusID := #ERROR_IN_THIS_BLOCK;
0066     #status := #WRONG_TYPE_1;
0067     RETURN;
0068   END_IF;
0069
0070   //This program code section checks whether the initial value OF the ring #buffer
0071   //matches the entry (tag #item). IF the data types DO NOT match,
0072   //the program execution is terminated at this point
0073   //AND the error code "8202" is output. *)
0074
0075   IF NOT (TypeOf(#item) = TypeOf(#initialValue)) THEN
0076     #error := true;
0077     #statusID := #ERROR_IN_THIS_BLOCK;
0078     #status := #WRONG_TYPE_2;
0079     RETURN;
0080   END_IF;
0081
0082   //This program code section checks whether the variable indices are within the ARRAY limits.
0083   //IF they are NOT, the program execution is terminated at this point
0084   //AND either error code "8601" OR "8602" is output depending on the index. *)
0085
0086   IF (#statNextEmptyItemIndex >= #tempPufferSize) THEN
0087     #error := true;
0088     #statusID := #ERROR_IN_THIS_BLOCK;
0089     #status := #INDEX_IN_ARRAY_LIMITS_1;
0090     RETURN;
0091   END_IF;
0092
0093   IF (#statFirstItemIndex >= #tempPufferSize) THEN
 Totally Integrated
 Automation Portal
0094     #error := true;
0095     #statusID := #ERROR_IN_THIS_BLOCK;
0096     #status := #INDEX_IN_ARRAY_LIMITS_2;
0097     RETURN;
0098   END_IF;
0099
0100
0101   //If reset is "TRUE" the buffer and their counters are resetted
0102   IF #resetBuffer = #RESET THEN
0103     #statFirstItemIndex := -1;
0104     #statNextEmptyItemIndex := 0;
0105
0106    FOR #tempSizeCounter := 0 TO UDINT_TO_INT(#tempPufferSize - 1) DO
0107
0108     #tempInternalError := MOVE_BLK_VARIANT(SRC := #initialValue,
0109                          COUNT := 1,
0110                          SRC_INDEX := 0,
0111                          DEST_INDEX := #tempSizeCounter,
0112                          DEST => #buffer);
0113     END_FOR;
0114     IF (#tempInternalError <> #NO_INTERNAL_ERROR) THEN
0115     #error := true;
0116     #statusID := #ERROR_MOVE_BLK_VARIANT;
0117     #status := INT_TO_WORD(#tempInternalError);
0118     END_IF;
0119     RETURN;
0120   END_IF;
0121
0122   //-----------Program code execution, depending on the Mode parameter-------------
0123   // The execution of the instructions depends on the signal state of the Mode parameter.
0124
0125   IF #mode = #NEXT_TASK_TO_EXECUTE THEN
0126
0127
0128    // If the Mode parameter has the signal state "0",
0129    // the first entry from the passed ring buffer is returned.
0130
0131    //this program code section checks whether the ring #buffer is empty.
0132    //IF this is the CASE, program execution is terminated at this point
0133    //AND the error code "8001" is output. *)
0134
0135    IF (#statFirstItemIndex = #BUFFER_IS_EMPTY) THEN
0136    #error := true;
0137    #statusID := #ERROR_IN_THIS_BLOCK;
0138    #status := #BUFFER_EMPTY;
0139    RETURN;
0140    END_IF;
0141
0142
0143    // This program code section returns the first entry of the ring buffer.
0144
0145    #tempInternalError := MOVE_BLK_VARIANT(SRC := #buffer,
0146                       COUNT := 1,
0147                       SRC_INDEX := #statFirstItemIndex,
0148                       DEST_INDEX := 0,
0149                       DEST => #item);
0150
0151    IF (#tempInternalError = #BUFFER_CONTAINS_ARRAY) THEN
0152
0153    //This program code section checks whether the ring #buffer contains ARRAY elements.
0154    //IF it does, the first entry is passed further on
0155    //AND the index is incremented BY 1. *)
0156
0157    #tempInternalError := MOVE_BLK_VARIANT(SRC := #initialValue,
0158                         COUNT := 1,
0159                         SRC_INDEX := 0,
0160                         DEST_INDEX := #statFirstItemIndex,
0161                         DEST => #buffer);
0162
0163    // This program code section calculates the new index of the first entry.
0164
0165    #tempNewFirstItemIndex := #statFirstItemIndex + 1;
0166    #tempNewFirstItemIndex := #tempNewFirstItemIndex MOD UDINT_TO_INT(#tempPufferSize);
0167
0168     // This program section checks whether the ring buffer is empty.
0169     IF (#statNextEmptyItemIndex = #tempNewFirstItemIndex) THEN
0170       // If the ring buffer is empty, the index is set to 0.
0171       #statFirstItemIndex := -1;
0172       #statNextEmptyItemIndex := 0;
0173     ELSE
0174       // The index of the first entry is changed.
0175       #statFirstItemIndex := #tempNewFirstItemIndex;
0176     END_IF;
0177     END_IF;
0178   ELSE
0179
0180    // If the Mode parameter has the signal state "1", the entry is written to the passed ring buffer.
0181
  Totally Integrated
  Automation Portal
0182     //this program code section checks whether the ring #buffer is full.
0183     //IF this is the CASE, program execution is terminated at this point
0184     //AND the error code "8002" is output. *)
0185
0186     IF (#statNextEmptyItemIndex = #statFirstItemIndex) THEN
0187     #error := true;
0188     #statusID := #ERROR_IN_THIS_BLOCK;
0189     #status := #BUFFER_FULL;
0190     RETURN;
0191     END_IF;
0192
0193     // This program code section writes the entry to the ring buffer.
0194     #tempInternalError := MOVE_BLK_VARIANT(SRC := #item,
0195                        COUNT := 1,
0196                        SRC_INDEX := 0,
0197                        DEST_INDEX := #statNextEmptyItemIndex,
0198                        DEST => #buffer);
0199
0200     IF (#tempInternalError = #NO_INTERNAL_ERROR) THEN
0201
0202     // This program code section increments the index by 1 and calculates the new empty entry index.
0203     #tempNewNextEmptyItemIndex := #statNextEmptyItemIndex + 1;
0204     #tempNewNextEmptyItemIndex := #tempNewNextEmptyItemIndex MOD UDINT_TO_INT(#tempPufferSize);
0205     #statNextEmptyItemIndex := #tempNewNextEmptyItemIndex;
0206
0207     //This program code section checks which index the "#firstItemIndex" tag has.
0208     //IF the number = -1, the ring buffer is initialized
0209     //AND the entry is written TO the ring #buffer.
0210     //Therefore, "0" must be assigned TO the tag.
0211
0212      IF (#statFirstItemIndex = #BUFFER_INITIALIZED) THEN
0213        #statFirstItemIndex := 0;
0214      END_IF;
0215      END_IF;
0216    END_IF;
0217
0218    //-------------------------Local error handling----------------------------
0219    //This program code section checks whether a "Local" #error has occurred.
0220    //IF This is the CASE, program execution is terminated at This point
0221    //AND the error code of MOV_BLK_VARIANT is output. *)
0222
0223    IF (#tempInternalError <> #NO_INTERNAL_ERROR) THEN
0224      #error := true;
0225      #statusID := #ERROR_MOVE_BLK_VARIANT;
0226      #status := INT_TO_WORD(#tempInternalError);
0227      RETURN;
0228    END_IF;
0229
0230    #done := true;
0231    #status := #NO_ERROR;
Symbol                       Address                Type                     Comment
#buffer                                             Variant                  An ARRAY that is used as a ring buffer.
#BUFFER_CONTAINS_ARRAY       0                      Int
#BUFFER_EMPTY                16#8001                Word
#BUFFER_FULL                 16#8002                Word
#BUFFER_INITIALIZED          -1                     Int
#BUFFER_IS_EMPTY             -1                     Int
#done                                               Bool
#error                                              Bool
#ERROR_IN_THIS_BLOCK         1                      UInt
#ERROR_MOVE_BLK_VARIANT      2                      UInt
#execute                                            Bool                     The instruction is executed when a positive signal edge is detected at the
                                                                             "request" parameter.
#INDEX_IN_ARRAY_LIMITS_1     16#0                   Word
#INDEX_IN_ARRAY_LIMITS_2     16#0                   Word
#initialValue                                       Variant                  Value with which the ARRAY of the ring buffer is initialized.
#item                                               Variant                  The entry that is either returned from the ring buffer or written to the ring
                                                                             buffer
#mode                                               Bool                     0 = The first entry in the ring buffer is returned. 1 = An entry is written at
                                                                             the last position in the ring buffer.
#NEXT_TASK_TO_EXECUTE        false                  Bool
#NO_ARRAY                    16#8200                Word
#NO_CURRENT_JOBS             16#7000                Word
#NO_ERROR                    16#0000                Word
#NO_INTERNAL_ERROR           0                      Int
#RESET                       true                   Bool
#resetBuffer                                        Bool
#statEdgeupm                                        Bool                     Edge memory bit in which the RLO of the previous query is saved.
#statFirstItemIndex                                 Int                      Index of the oldest entry in the ring buffer
#statNextEmptyItemIndex                             Int                      Index of the next free entry in the ring buffer
#status                                             Word                     Status information
#statusID                                           UInt
#tempInternalError                                  Int                      Error information
#tempNewFirstItemIndex                              Int                      Variable index
#tempNewNextEmptyItemIndex                          Int                      Variable index
#tempPufferSize                                     UDInt                    Number of ARRAY elements in the ring buffer
#tempSizeCounter                                    Int
 Totally Integrated
 Automation Portal
Symbol                Address   Type   Comment
#WRONG_TYPE_1         16#8201   Word
#WRONG_TYPE_2         16#8202   Word