Access to ARRAY elements in STL

Supreame

Member
Join Date
Jul 2008
Location
Brasov
Posts
33
Hello,

I want to access the elements of an array with the aid of a variable (indirect accessing the elements of an array).

For example, an array of type byte is defined (byte_array [1..10] of BYTE).
I want to access the element nr. 3 from the array using a variable (let's say x, where x is defined as BYTE type), so to make something like byte_array[x] (instead of byte_array[3]).
This byte_array is defined as a static variable inside a FB.

How can I achieve this?
Thanks!
 
Call this FC from your FB.

Code:
FUNCTION FC 1 : BYTE
TITLE =
VERSION : 0.1

VAR_INPUT
  ArrayName : ANY ; 
  x : INT ; 
END_VAR
VAR_TEMP
  iDB : INT ; 
END_VAR
BEGIN
NETWORK
TITLE =
      L     P##ArrayName; 
      LAR1  ; 
      L     W [AR1,P#4.0]; 
      T     #iDB; 
      L     D [AR1,P#6.0]; 
      LAR1  ; 
      L     #x; 
      +     -1; 
      SLD   3; 
      +AR1  ; 
      OPN   DB [#iDB]; 
      L     B [AR1,P#0.0]; 
      T     #RET_VAL; 
      SET   ; 
      SAVE  ; 
END_FUNCTION

Here's the FB I used to test it:

Code:
FUNCTION_BLOCK FB 1
TITLE =
VERSION : 0.1

VAR
  TestArray : ARRAY  [1 .. 10 ] OF BYTE ; 
  byData : BYTE ; 
END_VAR
BEGIN
NETWORK
TITLE =
      L     B#16#67; 
      T     #TestArray[4]; 
      NOP   0; 
NETWORK
TITLE =
      CALL FC     1 (
           ArrayName                := #TestArray,
           x                        := 4,
           RET_VAL                  := #byData);
      NOP   0; 
END_FUNCTION_BLOCK
 
Thank you!

I noticed your previous post Step 7 using a variable as an array index.
From this I understand (please correct me if I'm wrong) that when you access different data types you must use SLD with different increments:
binary - SLD 0
byte - SLD 3
word - SLD 4
dword - SLD 5

In the Siemens Step7 documentation for ANY data type (Appendix A.3.4.5) it says that ANY covers 10 bytes & that the address is stored in the format Byte.Bit where the byte address is stored:
- in bits 0 to 2 of byte 7
- in bits 0 to 7 of byte 8
- and in bits 3 to 7 of byte 9
and the bit address is stored in bits 0 to 2 of byte 9

So from this I understood that the scope of SLD 3 instruction is to calculate the new byte address = prepare the increment (the offset) that is to be added to the base =; without overwriting the bit address = the 3 bits (0 to 2) of byte 9.

Question: why is it necessary to make SLD 4 & SLD 5 (preserve bites 0 to 3 or 0 to 4)? When working with word & dword the bit address of the ANY data type covers 4 & 5 bites?

Many thanks!
 
Euhm don't really understand your question SLD3 is the same like multipling by 8

What LD is doing is converting the Integer adress to a DWORD adress

There is a post about it, will look it up for you
 

Similar Topics

Having issues finding how to have a variable tag as an array offset, when that array is part of the FB STAT area and the array is being passed...
Replies
13
Views
2,389
Hi, I'm trying to get iFix 5.8 to access DINT and INT array elements from a ControlLogix. My tag: test_dint Data Type: DINT Array Length: 100...
Replies
6
Views
3,168
Hi Everyone! I'm currently working with an ABB RTU which has built-in PLC capabilities, I'm programming some systems using ST in Multiprog wt. I...
Replies
0
Views
1,628
Hello at everybody, I have a client opc that is connected to an opc server RSLinx, I would read an array, I can do it using the length, the...
Replies
0
Views
1,397
Hi I have a boolean array within my PLC inside a DB Array[0...32,0..100) of type BOOL In WinCC flex how would I address an array element...
Replies
3
Views
2,070
Back
Top Bottom