Hello experts,
I have configured x20 CPU as modbus master and communicating with modbus slave with ModbusTCP_Any. I wanted to read the floating point value from the slave.
For this:
- I have defined two consecutive 16 bit register to hold the floating values.
- in TCPAny configuration, created block 1 for FC 3 with 2 channels: UNIT
I can see the values forced by the slave in the configured channels but don’t know how to realize/get the final floating point value.
Please guide!
1 Like
You could replace the two UINT channels by using a single REAL channel directly. As an example from register 3 this would then use registers 3 and 4 and combine those to a REAL value. This only works if the byte order is the same with the X20 CPU and the modbus slave.
If it is different then you need to take the two UINT values, change their byte order to match and combine these to a REAL variable in the code. An approach I use quite often is to put the UINT values to an array and then create a REAL pointer to the array.
Variables:
VAR
UINTArray : ARRAY[0..1] OF UINT;
realValue : REFERENCE TO REAL;
END_VAR
Code
PROGRAM _INIT
UINTArray[0] := 59769;
UINTArray[1] := 17142;
END_PROGRAM
PROGRAM _CYCLIC
realValue ACCESS ADR(UINTArray);
END_PROGRAM
Result:

2 Likes
Hi Tommi Piiparinen,
Thank you for the prompt response!
I appreciate your assistance.
Hi Tommi Piiparinen,
Just one query regarding pointer pointing array.
I have an array of 6 element in which every 2 elements are holding register values.
should i create 3 real pointers pointing to every two array elements to generate 3 floating point number?? If yes then how.
Single real pointers
realValue1 ACCESS ADR(UINTArray[0]) // 0 and 1
realValue2 ACCESS ADR(UINTArray[2]) // 2 and 3
realValeu3 ACCESS ADR(UINTArray[4]) // 4 and 5
or as an array of real pointers
realArray ACCESS ADR(UINTArray)
This would mean realArray[0…2] = UINTArray[0…5]
1 Like