array of struct

hapetter

Member
Join Date
Feb 2009
Location
Kristiansand
Posts
58
Hi.

I try to implement an array of struct in IEC61131-3 ST language. I use a beckhoff PLC.
The C code look like this:

Code:
Struct BDD
{
Unsigned char a1;
Unsigned int a2;
Unsigned int a3;
Unsigned int a4;
}
 
struct BDD BDD_table[4] = {
{'X', 1, 1, 2},
{'a', 0, 100, 0},
{'a', 1, 100, 2},
{'X', 2, 5, 4}
};
So:
How to declare array of struct and fill inn data in array?

Thanks!

Best Regards
Hans Pettersson
 
I tried this, but its not correct...

PROGRAM StateMachine
VAR
TYPE BDD_Table
STRUCT
N_Type: STRING;
N_Index: INT;
Successor0: INT;
Successor1: INT;
END_STRUCT
END_TYPE

BDD_Table: ARRAY [0..4] OF STRUCT;

END_VAR

I have no idea how to assign data in each struct in array...

Anyone?
 
This is how it would look like in Siemens SCL, which should be the same as IEC ST:

Code:
TYPE BDD
    STRUCT
        a1 : CHAR ;
        a2 : INT ;
        a3 : INT ;
        a4 : INT ;
    END_STRUCT
END_TYPE
    
DATA_BLOCK BDD_table
    STRUCT
        vals : ARRAY[0..3] OF BDD ; 
    END_STRUCT
BEGIN
    vals[0].a1 := 'X' ;
    vals[0].a2 := 1 ;
    vals[0].a3 := 1 ;
    vals[0].a4 := 2 ;
    vals[1].a1 := 'a' ;
    vals[1].a2 := 0 ;
    vals[1].a3 := 100 ;
    vals[1].a4 := 0 ;
    vals[2].a1 := 'a' ;
    vals[2].a2 := 1 ;
    vals[2].a3 := 100 ;
    vals[2].a4 := 2 ;
    vals[3].a1 := 'X' ;
    vals[3].a2 := 2 ;
    vals[3].a3 := 5 ;
    vals[3].a4 := 4 ;
END_DATA_BLOCK
 
The following code compiles in TwinCAT without errors.

In the "Data Types" section:

Code:
TYPE BDD :
STRUCT
 a1: STRING[1];
 a2:UINT;
 a3:UINT;
 a4:UINT;
END_STRUCT
END_TYPE

In the "Global variables" section:

Code:
VAR_GLOBAL
 BDD_table: ARRAY[0..3] OF BDD :=  (a1 := 'X', a2 := 1, a3 := 1, a4 := 5),
         (a1 := 'a', a2 := 0, a3 := 100, a4 := 0),
         (a1 := 'a', a2 := 1, a3 := 100, a4 := 2),
         (a1 := 'X', a2 := 2, a3 := 5, a4 := 4);
END_VAR

Not as compact as C version but still OK.
 
reply to array of struct

I dont understand how this works for you. I belive code is ok, but I get these strange errors.
Please see screenshot of how I do it and what errors

Task.jpg Global variables.jpg
 
You cannot declare a user data type within a program or among global variables in TwinCAT - you must declare it in "Data Types" section. This is not "C" with its liberties, after all. There are four tabs at the bottom of the project tree: POUs, Data Types, Visualizations and Resources; select "Data Types" and create new type.

The errors related to Global_Vars are caused by the improperly declared BDD structure: since the compiler does not recognize the structure, it objects to declaring the array of it.

A side note: I just noticed that the third tab is called "Visualizations", with 'z'. I am wondering if CoDeSys people thought they were spelling it in American English manner (realize/realise, digitize/digitise etc.)... wrong, of course. :)
 

Similar Topics

Hi there, I have an array of 44 words that contain the two parts of a set of real numbers. I want to copy these words over to my Struct which...
Replies
12
Views
3,663
I have an Array of Struct which contains 49 pairs of DWord points. How can I convert this entire array to Dint or Real numbers?
Replies
3
Views
2,879
Hi, I would like to know what good applications are for an array, struct, string and UDT? I pretty much know how to use them (moving the data...
Replies
2
Views
2,718
Using Step 7 5.5 400 PLC I had an idea recently of putting some data in an array. There can be several devices, like a Drive for a motor for...
Replies
9
Views
8,649
Hi, I'm having an issue in crimson 3.0 when I create a programme using a case statement referencing a fault word that each bit needs to change the...
Replies
5
Views
261
Back
Top Bottom