Alternative to Union from Codesys

Has B&R in Automation Studio with structured text something similar to Union from Codesys?

Hi,

union is not supported right now (maybe with the release of ST OOP, which is coming with AS Code mid of this year).

But if I got it right, it’s more or less a structure where the elements are aligned to the same size, right? So, if declaring a structure with for example 4 byte data types as elements only, it should have the same behavior as described … or I understood the explanation completely wrong, which is possible of course :wink:

Best regards!

Hi,

if the challenge is to save memory (e.g. a data base) one can simulate a union with a struct by storing also the data type and using dynamic variables to access the data.

TYPE
	myRecordTypeEnum : 
		(
		REC_DINT,
		REC_REAL
		);
	myRecord : 	STRUCT 
		type : myRecordTypeEnum;
		data : ARRAY[0..3]OF USINT;
	END_STRUCT;
END_TYPE
	IF write_dint THEN
		write_dint := FALSE;
		ddint ACCESS ADR(record.data);
		record.type := REC_DINT;
		ddint := 3;
	END_IF

	IF write_real THEN
		write_real := FALSE;
		dreal ACCESS ADR(record.data);
		record.type := REC_REAL;
		dreal := 3.1415;
	END_IF

	IF read THEN
		read := FALSE;
		CASE record.type OF
			REC_DINT: 
				ddint ACCESS ADR(record.data);
				(* do something with ddint *)
			REC_REAL: 
				dreal ACCESS ADR(record.data);
				(* do something with dreal *)
		ELSE
			(* invalid datatype *)
			
		END_CASE
	END_IF
VAR
	dreal : REFERENCE TO REAL;
	ddint : REFERENCE TO DINT;
	record : myRecord;
	write_dint : BOOL;
	write_real : BOOL;
	read : BOOL;
END_VAR
1 Like

Not currently as I don’t believe it’s a part of the IEC 61131 main standard. Per this blog post IEC 61131-3: Additional language extensions – Stefan Henneken, twin cat and codesys may support it in some fashion, and per Alex, it might be supported in ST OOP in Automation Studio Code. But currently it isn’t a part of the structured text language as implemented in Automation Studio. Automation Studio’s compliance with the IEC 61131-3 standard is documented in the AS help B&R Online Help. I don’t see it listed as part of the standard.

You may be able to implement something in a library in C to handle this and then wrap it up nicely for structure text usage.

For other’s not familiar, a union defines multiple ways to access the same memory address with different formats. i.e. I can define a UNION of BYTE[4] and a DWORD and then be able to access each of the bytes of the DWORD via the BYTE array without having to do any ACCESS or memcpy(). (It’s a commonly used in C and other languages, but from what I can tell not a part of the 61131-3 standard, edition 3.)

4 Likes

Finally I see someone question about structured text implementation in Automation Studio. :slightly_smiling_face:

I not sure UNION is covered in 61131-3: 2013 but I will say that it is better to have it.
Secondly, I think 61131-3 did covered OOP but ST in B&R Automation Studio does not have it. I glad to hear that we will get it soon(ST OOP).

Thank you for all the replies. I use union mostly for conversions, for example, Modbus registers (status and control words) and PLC internal flags. I think Matt Buck’s approach with the C function will help me.

this is called ‘type-punning’ and there is a nice article on stackoverflow about it:

TLDR: Using Unions for “type punning” is fine in C, and fine in gcc’s C++ as well (as a gcc [g++] extension). But, “type punning” via unions has hardware architecture endianness considerations

1 Like