Codesys 3.5, STRUCT and UNION

leon78

Member
Join Date
Jul 2004
Posts
65
I declare a structure:
Code:
TYPE sdoName:
STRUCT
    i1: INT;
    i2: INT;  
END_STRUCT
END_TYPE
I declare a UNION:
Code:
TYPE uName:
UNION
    di : DINT;
    n : sdoName;
END_UNION
END_TYPE
I declare a variable:
Code:
VAR
	v: uName;
END VAR
Now I can access variable fields:
Code:
diX := v.di;
iX1 := v.n.i1; 
iX2 := v.n.i2;
But I want do it without "n":
Code:
diX := v.di;
iX1 := v.i1; 
iX2 := v.i2;
How I can declarate UNION for it? I thought:
Code:
TYPE uName:
UNION
    di : DINT;
   STRUCT
    i1: INT;
    i2: INT;  
   END_STRUCT
END_UNION
END_TYPE
But it is wrong.
 
I declare a structure:
Code:
TYPE sdoName:
STRUCT
    i1: INT;
    i2: INT;  
END_STRUCT
END_TYPE
I declare a UNION:
Code:
TYPE uName:
UNION
    di : DINT;
    n : sdoName;
END_UNION
END_TYPE
I declare a variable:
Code:
VAR
	v: uName;
END VAR
Now I can access variable fields:
Code:
diX := v.di;
iX1 := v.n.i1; 
iX2 := v.n.i2;
But I want do it without "n":
Code:
diX := v.di;
iX1 := v.i1; 
iX2 := v.i2;
How I can declarate UNION for it? I thought:
Code:
TYPE uName:
UNION
    di : DINT;
   STRUCT
    i1: INT;
    i2: INT;  
   END_STRUCT
END_UNION
END_TYPE
But it is wrong.

Yes, You should be able to do what you want to do but what you are doing is not right. How would access i2?

I think you will have better luck if you define the structure out side of the union as its own type then include that type in the union or giving the structure a name so you can specify the structure type so the compiler will know the offset of the i2 within the structure.
 
From scoping considerations, it should not be possible.


Consider the following:


Code:
 TYPE sdoName:
   STRUCT
     [COLOR=Red][B]i1[/B][/COLOR]:    INT;
     i2:    INT;
   END_STRUCT
END_TYPE
Then

Code:
 TYPE uName:
  UNION
    [COLOR=red][B]i1[/B][/COLOR] : DINT;
    n  : sdoName;
  END_UNION
END_TYPE
Then
Code:
VAR
   v: uName;
END VAR
If we could refer to the struct v.n's members without the intervening .n, then how could the code distinguish between v.n.i1 and v.i1?
 
Last edited:
Interesting! And it turns out to be an old problem ( I didn't read the whole thing) and not limited with CodeSys.

I think it has to do with memory allocation.

I am not sure why the compiler only produces a warning when the result is such a mess.


Edit: More to come............ I am testing it and I think it works quite well.
 
Last edited:
It appears to work well; see this .gif.


The the members of a UNION share the same memory and so only one member can be use at a time. You can see that from the memory address of the UNION members and compare that to the local variables with each have a separate memory area allocated to them.


Was that what you wanted to achieve?
 
Cool! di is supposed to be a DINT, according to OP, but it should work the same.


What happens if you use the name [i1] or [i2] for the name of the variable with the name [di] now?
 
With di as a DINT, try inserting the following ELSIFs before the END_IF, and the following TON after the END_IF:

ELSIF doRollover THEN
v.di := 65535;
ELSIF timer.Q THEN
v.di := v.i1 + 1;
doRollover := FALSE;
END_IF;

TON(timer,IN=doRollover,PT=T#5s);



 
It appears to work well; see this .gif.

Why do you have same address for v.i1 and v.di?

My code:
Code:
TYPE sdoName :
STRUCT
    i1: INT;
    i2: INT; 
END_STRUCT
END_TYPE

TYPE uName EXTENDS sdoName :
STRUCT
	di : DINT;
END_STRUCT
END_TYPE

PROGRAM prgLogic1
VAR
	test: uName;
	i1Address: POINTER TO INT;
	i2Address: POINTER TO INT;
	diAddress: POINTER TO DINT;
END_VAR

i1Address := ADR (test.i1);
i2Address := ADR (test.i2);
diAddress := ADR (test.di);

изображение_2022-08-30_1337424.png
 
I got it!
Code:
TYPE uName EXTENDS sdoName :
UNION
	di : DINT;
END_UNION
END_TYPE

Thank you very much!

изображение_2022-08-30_1346326.png
 

Similar Topics

Hello, I am using a Hitachi Micro EHV+ for a small project, and I wanted to have a Web visu, done with Codesys V3.5 SP13 Patch 2. I test the...
Replies
6
Views
296
Hello, I have a requirement to manage the text alignment dynamically. So, for example: 1. English Texts should be displayed from Left in...
Replies
0
Views
90
Hello, I am new to Codesys, and am trying to learn about it for a project we're developing. I've got a couple questions, but first a little...
Replies
1
Views
161
Hi everyone, as this is my first experience with Rockwell Software i would like to know what's the best way to make Enumerations?
Replies
10
Views
511
I am trying to get Codesys to work with a couple of Moxa ioLogik E1200 series DIO devices (E1210 and E1211). I am able to write to the E1211 DOs...
Replies
2
Views
174
Back
Top Bottom