Arrays in Structured Text

grum

Member
Join Date
Jul 2006
Location
Essex
Posts
4
Hi



I have just finished my first PLC program in ST and all is working fine, BUT

The customer now wants a fault display on an HMI the display itself no problem.

HOWEVER the current software uses BIT ANDing (50+ Inputs) 14 times this is fine for a

group result, now I need individual results if I was writing in assembler its easy index into a table

The only way I can think of doing it is using an array but the array has to be an array of bits

I have no idea how to implement this in ST.

Can Anybody help



Grum

 
If it's IEC ST the actual PLC shouldn't make that much difference.

Declare an array and an integer to act as the index e.g.
Code:
VAR
ALARMS : ARRAY of BOOL[50];
Index : INT;
END_VAR
Then in the code you simply address ALARMS[Index] to access each bit as required. You can OR bits explicitly or in a loop to test for combinations -
Code:
BigAlarm := ALARMS[1] OR ALARMS[2] OR ALARMS[3] OR ALARMS[4];
Alternatively ...
Code:
Test := false;
WHILE (Test=false) AND (Index < 50) DO
Test := Test OR ALARMS[Index];
Index := Index + 1;
END_WHILE;
There are many ways to tackling this according to your needs. Does your software support the AT declaration style to permit accessing the ARRAY via different data-types?

Regards

Ken
 
Hi Ken
Thanks for your response
Does your software support the AT declaration style to permit accessing the ARRAY via different data-types?
I have no idea what this means. (Running GX EIC Developer)
I nearly understand your code as I said I have never used arrays before
how do I fill the array with my input bits, is there an easy way.
The other problem is I have 30 System Labels left out of 300 so I am getting very tight for space.

Regards

Grum
 
Hi Grum

Writing to an element of an array is done as follows -

ALARMS[Index] := V1_LowLevel;

Whatever value 'Index' has will determine the bit in your array which receives the state of the variable 'V1_LowLevel'. I would assume you always want the presence of the V1_LowLevel alarm to be at the same position in the array so perhaps -

ALARMS[23] := V1_LowLevel;

Would be more appropriate. The 23rd bit of the array would have the status of V1_LowLevel mapped to it.

(Uh - I've just realised an error I made in declaring the array in my last message. It should be -

ALARMS : ARRAY[1..50] of BOOL;

You have to specify the upper and lower bounds of the array address rather than just the size. This gives you freedom to have an ARRAY[0..49] or ARRAY[1..50] or ARRAY[11..60] if you please.)

The AT construction isn't supported by all ST implementations. Normally you can only refer to a declared area of memory by one declaration name and treat it as one data-type. However with AT you can create things like -

VAR
Alarmbits : ARRAY[0..15] of BOOL;
Alarmword AT Alarmbits : WORD;
END_VAR

This means that the same 16-bit chunk of memory can be addressed as individual bits using the variable name Alarmbits[ ] or else treated as an entire word using the name Alarmword. You can have several different names and data-types for the same memory area. These extra 'views', as they're referred to, can be very useful especially with complex data-types like structures.

Regards

Ken
 
Hi Ken

As the Alarm bits change with each test I think I need 2 Arrays. 1 for the alarm bits and another with the alarm bits absolute referance using the same index, this way I can easily identify the failed bit for the HMI.
All of your comments have been very helpful and given me much food for thought. I understand what the AT construction means and I will try it.
Now I have to see if I can make the new code fit with the resources available.

Regards

grum
 

Similar Topics

Hi, 1) May I use arrays in Structured Text in CCW 12? Specifically, may I use a FOR...END_FOR statement and use the index (let's say, i) to point...
Replies
7
Views
2,294
I'm trying to do my first ever structured text routine and failing miserably. Just to make things interesting, it's also my first foray into UDTs...
Replies
4
Views
3,023
Hi. This is pretty basic but I'm struggling to find an efficient solution. I have a float value of let say 666.555 that I want to move / split...
Replies
3
Views
207
Hello. I've been using the answers in this forum for a while now, so thank you. Usually I can find the answer I'm looking for with a basic...
Replies
8
Views
750
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...
Replies
6
Views
717
Back
Top Bottom