richw
I have created an Array in my main program. How can I read and write the elements of the Array? Do I need to create a function block with structured text or can I use the other languages? Once I have Access to the Array can I move memory location values, such as (%MD0) to the Array?


Thanks
Reply 1 0
Pascal34
Hello, you can access the elements of your array like this: 
MyArray[0] for the first element, MyArray[2] for the second and so on...Depending on the data type, you can transfer values ​​into this array using the Move function (Ladder diagram).
In ST like this: MyArray[0] := MyVariable;Sincerely.
Reply 1 0
Grz
Hello Richw and Pascal34,

Can you please explain the use of arrays and give a simple example with a program ?

Thanking you in advance....
 
Best regard....
 
Pierre
Reply 0 0
Pascal34
So, first you create a new array in the variable declaration (at the top),
[203128%2F8589612%2Fmceclip0]
then you can read and write the element of the array of your choice
as in the attached image.
[203128%2F8589616%2Fmceclip0]
Tell me if you understand otherwise don't hesitate . See you soon
Reply 1 0
thiagoralves
Just write the array element name inside the “expression” field when you are adding a contact, coil or variable
Reply 1 0
Grz
Hello,  thank you for your answers.
 
The variables in the table can only be binary please... can we retrieve them in a word?
 
Can you please give me one or more examples of use?
 
Best regard
Pierre
Reply 0 0
Pascal34
Hello Grz, you can declare a word array if you want, and the way to access it is the same as described above.
What exactly do you want to do? so that I can give you an example that suits you...
Reply 0 0
streatstyle
Hello Grz, you can declare a word array if you want, and the way to access it is the same as described above.
What exactly do you want to do? so that I can give you an example that suits you...
Could you show how to do a boolean array in LD like just something basic so I can get an understanding?
Reply 0 0
thiagoralves
Create a variable on the top variable list, and then on the Type column select Array. Fill in the size of the array, for example 0..9, which means an array with 10 positions numbered 0 to 9. IEC allows you to have indexes starting at any value, so both 0..9 and 1..10 will create the same sized array, but one starting at index 0 and another starting at index 1.
 
Once you have your array, you can access the value in it by creating a var (right click on LD -> add -> variable), and then on the expression text box you type
my_array[3]
to access the element at index 3 of an array called "my_array".
 
Reply 1 0
kortenbach
I believe I understand what is written above, but still I get errors when trying to compile:
This code:
[203128%2F9139046%2Fmceclip0]
 
Gives me these errors:
[203128%2F9139047%2Fmceclip0]
 
I seem to be doing something wrong(?)
 
Best regards,
Bert
 
P.S. PLC.st has the following code in it when moving to a non-array type:
[203128%2F9139098%2Fmceclip0]
 
When moving to an array type the type of TMP_MOVE1_OUT changes from INT to ANY, and that is where the error hits.
Reply 0 0
kortenbach
I found a work-around by creating a new function "myMove" in ST that moves an INT to an INT. I used that instead of the MOVE-any function from the library.
Reply 0 0
kortenbach
I found another quirk passing an array as function argument.
Turns out that I needed to use call by reference instead of call by value (inout type instead of input type)
Is this by design?
Reply 0 0
thiagoralves
Yes. Inout type uses pointers, and that’s the only way to “send” an array over.
Reply 0 0
kortenbach
Thanks for the confirmation Thiagor!
Reply 0 0
kortenbach
Another weird thing with array passing to a function:
I'm passing a 29 element array to a function.
In the function I move array element[9] to the function output --> Works
In the function I move array element[10] to the function output --> Returns 0
It seems like I can only access the first 10 elements of an array (elements 0 to 9 in this case)
Is this a bug?
Reply 0 0
Reply