# LGF_ShiftRegister (FB / 4.0.0)

Overview

Author: Siemens Digital Industry Support

## Short description

The Function represents a shift register for any kind of Datatype (using `variant`).
It is possible to shift the elements in the array at `bufferRegister` to the left (index `array[n]:=array[n+1]`) or right (index `array[n]:=array[n-1]`).
It could be used for material tracking trough a machine or a process, e.g. for a rotary indexing table.

Note
  As this is a real shift operation, it may cause some runtime effects while using big array sizes to move at the input `bufferRegister`.
  Please consider that a FIFO or LIFO storage, based on indexes, could be used as well for most applications.

### Block Interface

| LGF_ShiftRegister (FB) |
--- | --- | ---
Bool |  |  | enable |  | valid |  |  | Bool
Bool |  |  | shiftLeft |  | busy |  |  | Bool
Bool |  |  | shiftRight |  | error |  |  | Bool
UInt |  |  | shiftRange |  | status |  |  | Word
Bool |  |  | reset |  | leftShiftCount |  |  | DInt
Bool |  |  | clear |  | rightShiftCount |  |  | DInt
Bool |  |  | fill |  | shiftOffsetCounter |  |  | DInt
Variant |  |  | initialItem |  | diagnostics |  |  | LGF_typeDiagnostics
Variant |  |  | bufferRegister |  |  | Variant

### Input parameter

Identifier | Data type | Default value | Description
--- | --- | --- | ---
enable | Bool | FALSE | TRUE: Enable functionality of FB
shiftLeft | Bool | FALSE | Rising edge: Elements in the array `bufferRegister` shifted left. Elements moved from index `N` to `N - 1`. The element at index `N = 0`is overwritten
shiftRight | Bool | FALSE | Rising edge: Elements in the array `bufferRegister` shifted right. Elements moved from index `N` to `N + 1`. The element at index `N = lastIndex` is overwritten
shiftRange | UInt | 1 | Number of places to be shifted in the `bufferRegister` input array
reset | Bool | FALSE | Initializing the buffer (reset the index and the counter)
clear | Bool | FALSE | Clear buffer elements in `bufferRegister` with `initalItem`
fill | Bool | FALSE | Overwrite buffer elements after shift operation. * `shiftLeft` - the most right element/s * `shiftRight` - the most left element/s overwritten by `initalItem`.
initialItem | Variant | --- | Value with which the ARRAY of the buffer is initialized (usually: `0` / default value)

### Output parameter

Identifier | Data type | Description
--- | --- | ---
valid | Bool | TRUE: Valid set of output values available at the FB
busy | Bool | TRUE: FB is active and new output values can be expected
error | Bool | FALSE: No error TRUE: An error occurred during the execution of the FB
status | Word | 16#0000-16#7FFF: Status of the FB 16#8000-16#FFFF: Error identification (see following Table)
leftShiftCount | DInt | Number of left shift operations since clearing
rightShiftCount | DInt | Number of right shift operations since clearing
shiftOffsetCounter | DInt | Offset of shift operations. `rightShift` - `leftShift`
diagnostics | LGF_typeDiagnostics | Diagnostic structure to store and transfer diagnostic information from blocks through the interface.

### In/Out parameter

Identifier | Data type | Description
--- | --- | ---
bufferRegister | Variant | Buffer / Register memory as ARRAY, which keeps the data. The data in the register is shifted left or right depending on the command.

### Status & Error codes

Code / Value | Identifier / Description
--- | ---
16#0000 | STATUS_NO_ERROR Status: Execution finished without errors
16#7000 | STATUS_NO_CURRENT_JOBS Status: No current jobs, initial state
16#7001 | STATUS_FIRST_CALL First call after incoming new job (rising edge 'enable')
16#7002 | STATUS_SUBSEQUENT_CALL Status: Subsequent call during active processing without further details
16#8001 | ERR_BUFFER_EMPTY Error: The buffer is empty
16#8002 | ERR_BUFFER_FULL Error: The buffer is full
16#8200 | ERR_NO_ARRAY Error: No array is present at the input `bufferRegister`
16#8201 | ERR_CLEARING_WITHOUT_INITIAL_ITEM Error: Clearing `bufferRegister` without an `initialItem` is not possible.
16#8202 | ERR_FILL_WITHOUT_INITIAL_ITEM Error: Option `fill` the buffer after shift operation without an `initialItem` is not possible.
16#8203 | ERR_WRONG_TYPE_INITIAL_ITEM Error: The data type of the input `initialItem` does not correspond to the data type of the array at the InOut parameter `bufferRegister`.
16#8204 | ERR_BOOL_NOT_SUPPORTED Error: Boolean variables and arrays are not supported by `MOVE_BLOCK_VARIANT`. (Use a PLC Datatype instead)
16#8401 | ERR_MULTIPLE_COMMANDS_DEDECTED Error: More than one command present at the same time. Only one of the inputs `shiftLeft`, `shiftRight` or `clear` is allowed.
16#8402 | ERR_IN_SHIFT_RANGE Error: The value at `shiftRange` must not exceed the maximum size of the Array at `bufferRegister`.
16#8600 | ERR_UNDEFINED_STATE Error: Due to an undefined state in state machine
16#8610 | ERR_CLEAR_BUFFER Error: While clearing buffer in block `MOVE_BLK_VARIANT``. Check `subFunctionStatus` code.
16#8611 | ERR_SHIFT_BUFFER_LEFT Error: While shift buffer left in block `MOVE_BLK_VARIANT` - check `subFunctionStatus` code
16#8612 | ERR_SHIFT_BUFFER_LEFT_FILL Error: While fill buffer after shift left in block `MOVE_BLK_VARIANT` - check `subFunctionStatus` code
16#8621 | ERR_SHIFT_BUFFER_RIGHT Error: While shift buffer right in block `MOVE_BLK_VARIANT` - check `subFunctionStatus` code
16#8622 | ERR_SHIFT_BUFFER_RIGHT_FILL Error: While fill buffer after shift right in block `MOVE_BLK_VARIANT` - check `subFunctionStatus` code

## User defined datatype(s)

### LGF_typeDiagnostics (UDT / V1.0.1)

Diagnostic structure to store and transfer diagnostic information from blocks through the interface.

Identifier | Data type | Default value | Description
--- | --- | --- | ---
status | Word | 16#0000 | Status of the Block or error identification when error occurred
subfunctionStatus | Word | 16#0000 | Status or return value of called FB's, FC's and system blocks
stateNumber | DInt | 0 | State in the state machine of the block where the error occurred

## Change log

Version & Date | Change description
--- | ---
| 1.0.0 | Siemens Industry Online Support
| 03.07.2018 | First released version
| 3.0.0 | Simatic Systems Support
| 09.04.2021 | Refactoring and alignment to Datatype Variant Insert documentation
| 4.0.0 | Simatic Systems Support
| 04.09.2024 | Rework to PLC Open `Enable` behavior Add `reset` input / counter outputs Fix Bug while filling after left shift operation Rework to diagnostic output datatype