TwinCAT convert two Modbus 16-bit registers to REAL

mhismail

Member
Join Date
Mar 2016
Location
egypt
Posts
30
Hello,

First, please excuse my question I am new to Modbus.

I have a CX-5140 control unit from Beckhoff and an instrumentation device that measures (Temperature, Humidity, etc.).

The device can function as a Modbus slave and hence I am using EL6021 EtherCAT terminal & TwinCAT Modbus RTU library to receive data from the device. The device has 36 registers of data all are 16-bit. In the data sheet of the device, each two registers represents a measured parameter ( for example: register 1 and register 2 are temperature....register 3 and register 4 are humidity ....and so on).

I was able to read the data from the device successfully in an array like so:

arrReceivedData : Array [1..40] of WORD;


Now my question is:
How can I combine any two registers such as arrReceivedData[2] and arrReceivedData[3] into another REAL variable??

In C++ world, I could easily use functions and macros such as HIWORD(something) & LOWORD(something) to re-write a 32-bit data type like "int" by using two 16-bit data type like "short" ....but there is no such function blocks in PLC or CX world to do so.

Thank you all.
 
In classic C this is a case where you would use a union. I'm not sure if TwinCAT has a system function to produce a REAL from two 16-bit WORDs but I'm pretty sure Codesys supports unions.

Keith
 
There are probably several ways to do this in TwinCAT. I haven't worked with with unions as mentioned by kamenges, but it appears to be supported:
https://infosys.beckhoff.com/englis..._intro/2529421195.html&id=2192070974948030363

You could also do a straight byte copy:
Code:
MEMCPY(ADR(destReal), ADR(arrReceivedData[2]), 2);  // low word
MEMCPY(ADR(destReal)+2, ADR(arrReceivedData[3]), 2);  // high word
This assumes that 'destReal' is defined as a 4-byte REAL variable, and the data in the two array words is already in REAL format but just split into 16-bit groups. Be careful with Memcpy because you can crash your controller if your offsets aren't right.
 
CODESYS

Code:
TYPE IntToReal :
UNION
Int16L: INT;
Int16H: INT;
Real32: REAL;
END_UNION
END_TYPE

Code:
PROGRAM PLC_PRG
VAR
CombinedInts: REAL;
convert: IntToReal;
END_VAR


Code:
convert.Int16L := (16bit number here in dec or hex etc) eg 16#41F9;
convert.Int16H := (16bit number here in dec or hex etc) eg 16#2489;
CombinedInts := convert.Real32;
 
Last edited:

Similar Topics

Hi! I am trying to run the 'SimpleSample' (https://infosys.beckhoff.com/content/1033/TF5100_TC3_NC_I/Resources/3438746891/.zip ) to understand the...
Replies
2
Views
98
I am developing a library in twincat and one of the function uses IID_ITcVnAccess_TcVnPoint2_DINT and the definition of this type is defined in...
Replies
0
Views
69
Sorry if this has been asked before, and apologies if this seems like a trivial issue, but I am new to Beckhoff and have been banging my head...
Replies
2
Views
142
Hi everyone, This is my first time posting, so please forgive any omissions or mistakes. I am attempting to control the velocity of a stepper...
Replies
18
Views
959
Back
Top Bottom