You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

Reply
 
Thread Tools Display Modes
Old November 7th, 2017, 12:33 AM   #1
stinklizard
Member
United States

stinklizard is offline
 
Join Date: Jul 2017
Location: Martinsville, VA
Posts: 10
Question Array index within RSLogix 5000

I have really hit a wall in a few situations, and asked a professor at a college this question. He said no, but I will ask it here.


Can you use a variable as the index for an array?


I swear it seems insane if you cannot. Array_1(x) or so on. Maybe he was referring to just the ladder logic side. Can the ladder logic or structured text handle this? I am positive if I read the reference material, I could find this; but it would be a lot easier to just ask the pool of experts here. I've searched and found answers to people asking about general arrays for PLC5, but I can't seem to find a plain answer for this question in regards to the 1756 (5000) platform.
  Reply With Quote
Old November 7th, 2017, 01:19 AM   #2
tarik1978
Member
Morocco

tarik1978 is offline
 
Join Date: Mar 2010
Location: casablanca
Posts: 614
Yes of corse you can use a variable as index for an array
Be sure this variable does'nt exceed the lenght of the array
  Reply With Quote
Old November 7th, 2017, 02:00 AM   #3
jacoffey85
Member
United States

jacoffey85 is offline
 
Join Date: Nov 2016
Location: NC
Posts: 75
I use arrays with variables a lot inside of structured text with RSLogix 5000. They're typically tied together with FOR/IF statements though.
  Reply With Quote
Old November 7th, 2017, 05:12 AM   #4
azetou
Member
Algeria

azetou is offline
 
Join Date: Mar 2010
Location: Algeria
Posts: 20
yes, and you can loop on x to read/write to the array.
  Reply With Quote
Old November 7th, 2017, 05:56 AM   #5
Maxkling
Member
United States

Maxkling is offline
 
Join Date: Mar 2011
Location: Atlanta
Posts: 499
You can do it on ladder as well.

Array[100]

Index

Array[index]

If index end up higher than your array size it will trip a recoverable fault.
  Reply With Quote
Old November 7th, 2017, 08:27 PM   #6
daba
Lifetime Supporting Member + Moderator
United Kingdom

daba is offline
 
Join Date: Jul 2004
Location: uk
Posts: 5,377
Well this needs expanding on....

Any array index, (the thing that is enclosed in square brackets), such as Array[35] can be replaced by a tag, e.g. Array[Index]. At runtime the system reads the value of the tag "Index", and uses it as the index into the array "Array".

This is called "Indirect Addressing".

But Logix5000 takes this to a new level... You can put an expression inside the square brackets, which is evaluated at runtime into a single numeric value, which then becomes the index value into the array.

Suppose you had to look at the previous position in the array... You could address it as Array[Position - 1]. Or the next, Array[Position + 1], etc.

This is a very flexible addressing arrangement, limited only by the programmer's imagination. Any expression within the square brackets is allowed, it can be as complex as it needs to be for the application, but you just need to make sure it doesn't equate to a position outside of the array, or the processor will fault and shut down.
__________________
___________________________

Everything works with smoke. Let it out, and it stops working.

Nil Carborundem Illegitimi



  Reply With Quote
Old November 7th, 2017, 09:51 PM   #7
Geoff White
Member
Australia

Geoff White is offline
 
Join Date: Oct 2005
Location: Brisbane
Posts: 444
The array bounds variables are checked before the program starts so if you manage to get a subscript out of bounds, the processor will fault before running the main task.
  Reply With Quote
Old November 8th, 2017, 08:03 AM   #8
daba
Lifetime Supporting Member + Moderator
United Kingdom

daba is offline
 
Join Date: Jul 2004
Location: uk
Posts: 5,377
Quote:
Originally Posted by Geoff White View Post
The array bounds variables are checked before the program starts so if you manage to get a subscript out of bounds, the processor will fault before running the main task.
But if the program manipulates the variables.....
__________________
___________________________

Everything works with smoke. Let it out, and it stops working.

Nil Carborundem Illegitimi



  Reply With Quote
Old November 8th, 2017, 09:36 AM   #9
jstolaruk
Lifetime Supporting Member
United States

jstolaruk is offline
 
Join Date: Dec 2004
Location: Detroit, SE Michigan
Posts: 3,727
Quote:
Originally Posted by daba View Post
But if the program manipulates the variables.....
I always put a LIM instruction on the index variable before using it.
__________________
I don't always test my code but when I do, I do it in production.
  Reply With Quote
Old November 8th, 2017, 12:32 PM   #10
daba
Lifetime Supporting Member + Moderator
United Kingdom

daba is offline
 
Join Date: Jul 2004
Location: uk
Posts: 5,377
Quote:
Originally Posted by jstolaruk View Post
I always put a LIM instruction on the index variable before using it.
You can't do that if you are using an expression as the array index within the square brackets.

Perhaps a better way is to use a dummy tag as the destination for a CPT instruction, and use the dummy tag as the array index, trapped by LIM if required.
__________________
___________________________

Everything works with smoke. Let it out, and it stops working.

Nil Carborundem Illegitimi



  Reply With Quote
Old November 8th, 2017, 12:34 PM   #11
jstolaruk
Lifetime Supporting Member
United States

jstolaruk is offline
 
Join Date: Dec 2004
Location: Detroit, SE Michigan
Posts: 3,727
Quote:
Originally Posted by daba View Post
You can't do that if you are using an expression as the array index within the square brackets.

Perhaps a better way is to use a dummy tag as the destination for a CPT instruction, and use the dummy tag as the array index, trapped by LIM if required.
Thats exactly the way I do it.
__________________
I don't always test my code but when I do, I do it in production.
  Reply With Quote
Old November 8th, 2017, 04:48 PM   #12
ASF
Lifetime Supporting Member
Australia

ASF is offline
 
Join Date: Jun 2012
Location: Australia
Posts: 3,524
Quote:
Originally Posted by daba View Post
You can't do that if you are using an expression as the array index within the square brackets.

Perhaps a better way is to use a dummy tag as the destination for a CPT instruction, and use the dummy tag as the array index, trapped by LIM if required.
A quick warning on this.

Take the example of an array of DINT's, with 10 elements. Call it DINT_Array[0] through DINT_Array[9]. Let's say you call DINT_Array[Pointer].

If Pointer has a value of 10, your PLC will crash.

Now let's say that you check the value of Pointer with a LIM instruction and only execute your indirect address instruction if Pointer is in the range of 0-9. So your rung goes:

LIM 0 Pointer 9 MOV DINT_Array[Pointer] Some_Tag

If your Pointer value is 10, the LIM instruction will evaluate false, and so the MOV instruction will not be executed. However, the indirect address will still be evaluated, and your PLC will still crash.

Even if you put an AFI at the start of that rung, so that the MOV instruction can never, ever be executed, your PLC will still crash if the Pointer value reaches 10.

So, it's not enough to just check the limits, you need to enforce the limits - i.e. if Pointer is greater than 9, MOV 9 to Pointer, and if Pointer is less than 0, MOV 0 to Pointer.

This check must not be conditional. For example, it's no good to say "if I need to perform this operation, check and clamp my Pointer value to the range 0...9 and then perform the operation". You must perform the clamping unconditionally, because even if you're not intending to execute the indirect address in an instruction, it will get evaluated anyway, and crash your PLC if it's incorrect.

Interestingly, this doesn't happen on a SoftLogix PLC. The SoftLogix will keep running until you actually execute the offending instruction. On a Control Logix, it'll fault straight away whether your'e executing the instruction or not.

Of course, you could use JMP/LBL or end subroutines to avoid the issue, but if you use JMP and TND to avoid crashing a PLC you deserve to be slapped across the face with a large fish.
  Reply With Quote
Old November 8th, 2017, 05:00 PM   #13
Mispeld
Member
United States

Mispeld is offline
 
Mispeld's Avatar
 
Join Date: Feb 2017
Location: VA
Posts: 594
Quote:
Originally Posted by ASF View Post
Interestingly, this doesn't happen on a SoftLogix PLC. The SoftLogix will keep running until you actually execute the offending instruction. On a Control Logix, it'll fault straight away whether your'e executing the instruction or not.
It appears this has been "fixed" at some point in the revision progression. Earlier today I was getting ready to offer the same advice, having been burned by it in the past, and found I could not get a Rev 20 processor to fault using a similar example. When putting an out-of-range value in the index, the move instruction following the limit test showed "????" in Logix 5000 as the unresolved indirect value instead of faulting the processor.
  Reply With Quote
Old November 8th, 2017, 05:19 PM   #14
jstolaruk
Lifetime Supporting Member
United States

jstolaruk is offline
 
Join Date: Dec 2004
Location: Detroit, SE Michigan
Posts: 3,727
Quote:
Originally Posted by Mispeld View Post
It appears this has been "fixed" at some point in the revision progression. Earlier today I was getting ready to offer the same advice, having been burned by it in the past, and found I could not get a Rev 20 processor to fault using a similar example. When putting an out-of-range value in the index, the move instruction following the limit test showed "????" in Logix 5000 as the unresolved indirect value instead of faulting the processor.
I concur, same experience here, though I didn't check the minor fault log. I will tomorrow.
__________________
I don't always test my code but when I do, I do it in production.
  Reply With Quote
Old November 8th, 2017, 05:34 PM   #15
harryting
Lifetime Supporting Member
United States

harryting is offline
 
harryting's Avatar
 
Join Date: May 2002
Location: Puget Sound
Posts: 2,463
I know version 19 will fault on array out of bound. Happened on a redundant system too.
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
RSLogix 500 and 5000 licensing and versions ryangriggs LIVE PLC Questions And Answers 12 March 3rd, 2020 06:19 AM
Using RSEmulator and keep getting an error in RSLogix 5000 Forat LIVE PLC Questions And Answers 11 October 3rd, 2016 01:12 PM
Byte swapping large arrays in RSLogix 5000 gardarog LIVE PLC Questions And Answers 5 January 18th, 2016 04:06 AM
RSLogix 5000 array tags (I/O points) Vuli LIVE PLC Questions And Answers 2 May 4th, 2006 11:14 AM
RSLogix 5000 V15 Features Samneggs LIVE PLC Questions And Answers 18 April 20th, 2006 06:45 PM


All times are GMT -4. The time now is 03:54 AM.


.