Studio 5000: Arrays - referencing specific array element

DzL9

Member
Join Date
Oct 2023
Location
Louisville, KY
Posts
3
Hello, first time poster here! A belated "thank you" for the direction I've already received from this forum! So, can I do this?

Move value 1 into array[x].1
Move value 2 into array[x].2
Move value 3 into array[x].3

Where X is defined elsewhere in the program, so I don't have to type this out for every single array element? Rather just define X somehow, and use the same MOVs to move the data into array[X]?

Thank you in advance, let me know if what I'm asking is as clear as mud.
 
Not used to Studio5000, but in most systems you have an array i.e. My_array 0..100 of integer for example or in the variables you define it as
Tag: My_Array, type array[0..100] integer
Then to directly address you can either use
My_Array[0] = 1
My_Array[1] = 4
etc.
Or use a variable to point to it indirectly.

For pointer = 0 to 5 do
My_Array[Pointer] = Pointer * 5
Next

So in effect the array values = 0,5,10,15 (just for clarity used the pointer times 5 to get differnet values into the array.
 
Short answer: Yes.

Long answer: Depends on exactly what you're doing and how you're trying to do it.

You say 'use the same MOVs' but your examples all end in .# indicating they are bits, not words so you wouldn't use MOV

The following is valid ladder logic which will place 'someValue' into the array at whatever position 'arrayIndex' is when it is scanned; you just need to update someValue and arrayIndex and then trigger the logic as needed:
Code:
                                   --------------------------------- 
  Trigger                          | MOV                           |
----| |----------------------------| Source              someValue |----------
                                   |                             3 |
                                   | Dest         array[arrayIndex]|
                                   |                          ???? |
                                   ---------------------------------

Note that you cannot nest brackets, eg in the above example we could not make arrayIndex an array and then use array[arrayIndex[1]].

If the position is outside the bounds of the array when scanned (in the above example 'arrayIndex' being negative or >= the array size) then the processor will trip a major fault; iirc Type 4 Code 20 specifically.
 
If the position is outside the bounds of the array when scanned (in the above example 'arrayIndex' being negative or >= the array size) then the processor will trip a major fault; iirc Type 4 Code 20 specifically.


+1M
This is a really important point. I've gotten into the habit of LIMit checking the array index variable immediately before every use. I've seen some...weird behavior caused by array indices going out of range at random times.


OT, but I also make sure the divisor in DIV instructions is non-zero before execution.


"But the index could never be invalid!"...famous last words....
 
Thanks for getting back to me guys. I figured it out, so let me know if I could have worded the question better....
What I'm actually doing: I have an overhead power & free with 20 carriers, carrying a stack of aluminum into dip tanks and whathaveyou for processing. I have a barcode scanner at the first stop that scans for the carrier number (1-20). I also have data being entered via HMI that needs to pair with the carrier number. HMI data and scanned carrier number need to correspond, so I made a UDT with those mentioned data fields (as tags), so the idea was to populate the entire UDT from the two different sources (HMI and scanner) so I could send it to SQL server.

So: It turns out that SHORT ANSWER, YES.
Scan barcode, gives me Carrier #"2". Store "2" into MyArray[2].CarrierNumber
Operator inputs info, store that into MyArray[2].Temperature, MyArray[2].OperatorInfo...etc
I want to store the data in the array element [2] that corresponds to the carrier number [2] just to keep things sorted.
Apparently, due to the syntax, you can use a tag name (and its data) inside those array brackets. I wonder what else you can use in those brackets...

So: I can do this -
Create "F" as a DINT
MOV "2" into "F"
and now I can actually reference
MyArray[F].whatever
and the program will see it as MyArray[2].whatever

Make sense? Because it's brand new to me lol.
But that's what I was after... basically can I use a changing variable inside those array brackets (and reference it that way too). What do they call that... could that be referred to as indirect addressing?

Thanks again guys, hopefully i can one day return the favor.
 
Its really direct addressing but using a variable to specify the index into an array.

"An indirect address is an absolute, relative, or symbolic address, (or a general register containing an address) of a location that contains another address."

Caution, its a good idea to compare the variable that is used for indexing into an array against minimum and maximum values. Otherwise there may be a fault or writing data outside of the array. For instance, the index tag should never be negative and never greater than the size of the array minus 1.
 
Last edited:

Similar Topics

I have a situation... I have a Fanuc robot talking to an AB CompactLogix through Ethernet/IP. 100 Fanuc registers (or 100 DINTs) are designated...
Replies
2
Views
2,570
Hi Everyone. Not posted on here for a long time, but I am hoping someone can help me. I am doing a differential pressure calculation in a L27ERM...
Replies
15
Views
288
Hi, how do I convert 2x Integer registers to a Real? The two integers are from Modbus registers that contain a floating point value, I need to...
Replies
2
Views
123
What is the best way to extract the hour and minute from the register that comes from Yaskawa VFD. In studio 5000 I have a register saved as INT...
Replies
3
Views
127
I have an Allen Bradley temperature switch that I am trying to use in studio 5000. I am getting the message "Unable to interpret the IODD file"...
Replies
0
Views
78
Back
Top Bottom