Step 7 using a variable as an array index

JohnW

Member
Join Date
Mar 2004
Location
Cornwall
Posts
487
How do I use a variable as an index for an array? I can't seem to do it.

I have tried
L variable
T "P1_DATA_LOG".DATA[#index] // array of real numbers

but it is giving me a syntax error.

What am I doing wrong here?
 
This syntax ("P1_DATA_LOG".DATA[#index]) is allowed only in the SCL editor (not STL).
In STL you can only use indirect addressing. It may look like this:

L p##"P1_DATA_LOG".DATA - get address of the array begining
LAR1 - load it to the address register
L #index - your index
SLD 5 - it aligns index with address format and taks care of data lenght
L D[AR1,p#0.0] - it loads DATA[#index] into accumulator

BTW, playing around with address registers can harm your program (and mental health). Don't do it at home (and running machine). :)
 
Here's a generalised array access function that you could use:

Code:
FUNCTION FC 6 : VOID
TITLE =Generalised Array element access, B[n]:=A[m];
VERSION : 0.1
 
VAR_INPUT
A : ANY ; 
m : INT ; 
n : INT ; 
END_VAR
VAR_OUTPUT
B : ANY ; 
END_VAR
VAR_TEMP
iType : INT ; 
iDBNumber : INT ; 
dwAreaPointer : DWORD ; 
iSize : INT ; 
bBOOL : BOOL ;	
wWORD : WORD ;	
dwDWORD : DWORD ; 
byBYTE : BYTE ; 
END_VAR
BEGIN
NETWORK
TITLE =de-construct any pointer for input array
	 L	 P##A; 
	 LAR1 ; 
	 L	 B [AR1,P#0.0]; 
	 L	 B [AR1,P#1.0]; 
	 T	 #iType; 
	 L	 W [AR1,P#2.0]; 
	 T	 #iSize; 
	 L	 W [AR1,P#4.0]; 
	 T	 #iDBNumber; 
	 L	 D [AR1,P#6.0]; 
	 LAR1 ; 
NETWORK
TITLE =
//open db if required
	 L	 #iDBNumber; 
	 L	 0; 
	 ==I ; 
	 JC	indb; 
	 OPN DB [#iDBNumber]; 
indb: NOP 0; 
	 L	 #iType; 
	 JL	iype; 
	 JU	inon; 
	 JU	iBol; //type 01= bool
	 JU	iByt; //type 02= byte
	 JU	iByt; //type 03= char (process as byte)
	 JU	iwrd; //type 04= word
	 JU	iwrd; //type 05= int (process as word)
	 JU	idwd; //type 06= dword
	 JU	idwd; //type 07= dint (process as dword)
	 JU	idwd; //type 08= real (process as dword)
iype: JU	inon; 
iBol: NOP 0; 
	 L	 #m; 
	 +AR1 ; 
	 A	 [AR1,P#0.0]; 
	 =	 #bBOOL; 
	 JU	op; 
iByt: L	 #m; 
	 SLD 3; 
	 +AR1 ; 
	 L	 B [AR1,P#0.0]; 
	 T	 #byBYTE; 
	 JU	op; 
iwrd: L	 #m; 
	 SLD 4; 
	 +AR1 ; 
	 L	 W [AR1,P#0.0]; 
	 T	 #wWORD; 
	 JU	op; 
idwd: L	 #m; 
	 SLD 5; 
	 +AR1 ; 
	 L	 D [AR1,P#0.0]; 
	 T	 #dwDWORD; 
	 JU	op; 
inon: JU	exit; 
NETWORK
TITLE =de-construct any pointer for output array
op: L	 P##B; 
	 LAR1 ; 
	 L	 B [AR1,P#0.0]; 
	 L	 B [AR1,P#1.0]; 
	 T	 #iType; 
	 L	 W [AR1,P#2.0]; 
	 T	 #iSize; 
	 L	 W [AR1,P#4.0]; 
	 T	 #iDBNumber; 
	 L	 D [AR1,P#6.0]; 
	 LAR1 ; 
NETWORK
TITLE =
//open db if required
	 L	 #iDBNumber; 
	 L	 0; 
	 ==I ; 
	 JC	ondb; 
	 OPN DB [#iDBNumber]; 
ondb: NOP 0; 
	 L	 #iType; 
	 JL	oype; 
	 JU	onon; 
	 JU	oBol; //type 01= bool
	 JU	oByt; //type 02= byte
	 JU	oByt; //type 03= char (process as byte)
	 JU	owrd; //type 04= word
	 JU	owrd; //type 05= int (process as word)
	 JU	odwd; //type 06= dword
	 JU	odwd; //type 07= dint (process as dword)
	 JU	odwd; //type 08= real (process as dword)
oype: JU	onon; 
oBol: NOP 0; 
	 L	 #n; 
	 +AR1 ; 
	 A	 #bBOOL; 
	 =	 [AR1,P#0.0]; 
	 JU	exit; 
oByt: L	 #n; 
	 SLD 3; 
	 +AR1 ; 
	 L	 #byBYTE; 
	 T	 B [AR1,P#0.0]; 
	 JU	exit; 
owrd: L	 #n; 
	 SLD 4; 
	 +AR1 ; 
	 L	 #wWORD; 
	 T	 W [AR1,P#0.0]; 
	 JU	exit; 
odwd: L	 #n; 
	 SLD 5; 
	 +AR1 ; 
	 L	 #dwDWORD; 
	 T	 D [AR1,P#0.0]; 
onon: JU	exit; 
NETWORK
TITLE =
exit: SET ; 
	 SAVE ; 
END_FUNCTION
 
Thanks guys,

As a dyed-in-the-wool Allen Bradley programmer I find this stuff all very irritating. I am basically lazy I think and I still haven't really got my head around STL.

L D[AR2,P#0.0] - thanks for your previous post on the data logging, I don't have to time stamp it now, therefore this post.

I thought that there might be an easier way.... some hope!
 
So, I have had a go at it, cant test it at the moment so its just theory but will this work?

L "VARIABLES_DB".P1_AVERAGE_FLOW // GET DATA
T #TEMP_DATA // STORE IT IN LOCAL MEMORY

OPN "P1_LOG_DATA" // OPEN LOG DB

L DBW 0
T #TEMP_POINTER // LOAD DATA POINTER INTO LOCAL MEMORY

LAR1 P#DBX2.0 // START OF RECORDS

L #TEMP_POINTER
SLD 5

+AR1
L #TEMP_DATA
T D [AR1,P#0.0]
 
Last edited:
Hooray!

Thanks for the help.

Do you use SCL?, I don't have that package or the simulator but I have been reading up on SCL and as I am an experienced PASCAL programmer and my customer is asking for IEC 1131 - 3 compatablity I shall get it. I think it will generaly make my life easier.
 
I do use SCL, and it has really made a big difference. It is worth the expense (regardless that it ought to be part of the standard package).

The great thing is that you can program array indexing AND program symbolically with ease.
 

Similar Topics

So I know that in RSLogix 5000 you can use a tag in place of a static number in an argument of any instruction. For example... If I wanted to...
Replies
14
Views
3,477
I have a basic understanding of truth tables and the addresses. My issue is that for every letter that I have to make, I need a truth table that...
Replies
12
Views
1,443
I have for the longest time avoided using the S7-1500 because everyone warned me that TIA portal was a stinking pile of garbage to be avoided at...
Replies
25
Views
12,162
Good Afternoon , I have a project that I am beginning , that I will be using 3) PowerFlex 525's drives. I took notice of the StepLogic feature...
Replies
1
Views
3,913
Dear Experts. I have one project using HMI Ktp 1500 comfort and S7-300 CPU 315-2DP. The HMI KTP is using Software TIA porttal for programming, and...
Replies
2
Views
2,032
Back
Top Bottom