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,581
Hello everyone, I'm new here, I hope someone can help me. I've been trying to install EDS files to my Studio 5000 with the EDS tool installer but...
Replies
3
Views
118
Hi Everyone, I am facing an issue while installing the STUDIO 5000 in my windows 10 PC. During installation I am getting an error that " Error...
Replies
4
Views
185
I am connecting to a remote device. When attempting to upload I receive an error that states: Error: Auto_Functions: The Import was aborted due...
Replies
3
Views
188
Back
Top Bottom