pro_to_be
pro_to_be

Reputation: 11

Performing a Shift Register operation on an array in TwinCAT3 using SHL/SHR

I am very new to PLC programming. Can anyone help with explaining how I can perform a SHR/SHL operation on an array of INT of lets say 10 elements using TwinCAT3. They way I thought I would do this is by moving the shift register one place so that Element 0 becomes Element 1, Element 1 becomes Element 2 ..etc. and the last Element is lost.

Thank you in advance for your help.

Upvotes: 1

Views: 3062

Answers (1)

Jacek Domański
Jacek Domański

Reputation: 640

Lets say you are working with:

VAR
  aiElements : ARRAY[0..uiElementsCount-1] OF INT;
  i : INT;
END_VAR

VAR CONSTANT
  uiElementsCount : INT := 10;
END_VAR

To move each value one index forward (0 -> 1, 1 -> 2, etc.) you can do the following:

FOR i := uiElementsCount-1 TO 1 BY -1 DO
  aiElements[i] := aiElements[i-1];
END_FOR 

To move each value one index backward (1 -> 0, 2 -> 1, etc.):

FOR i := 0 TO uiElementsCount-2 DO
  aiElements[i] := aiElements[i+1];
END_FOR 

Upvotes: 1

Related Questions