Join us today!

Converting REAL to ...
 
Notifications
Clear all

Converting REAL to 2 WORDS

1 Posts
1 Users
0 Reactions
953 Views
TwinControls
Posts: 71
Admin
Topic starter
(@beckhoffsupport)
Member
Joined: 2 years ago

Some of the Modbus configurations only accept WORD type variables. If you need to convert a REAL value to 2 WORDS, you can use UNION data type to achieve this easily. Different data types in the UNION share the same memory space. 

 

Create a DUT called ST_REAL_TO_aWORD:

TYPE ST_REAL_TO_aWORD:
UNION
	fREAL : REAL;
	aWord : ARRAY[1..2] OF WORD;
END_UNION
END_TYPE

 

Declare the variables in your program: 

fRealValue  	: REAL := 0.81;	
stData		: ST_REAL_TO_aWORD;

 

You will see that the REAL value is converted to 2 WORDS automatically in the UNION type. 

 

realtoword

 

You can also apply this method to other data conversions (BYTE to REAL, INT to BITS, WORD to BYTE etc...) as well. 

 

Reply
Share: