Help with TwinCat2 Modbus read float.

g.mccormick

Lifetime Supporting Member
Join Date
Jul 2012
Location
IN
Posts
961
I am trying to get a floating point value into a CX8090 via modbus. The value is being read, but I cannot seem to get it to convert correctly.

The value is 123.0, the CX8090 reads this value via modbus and stores it in a UDINT array. The value of the array element is:
1123418112 which in hex is 16#42F60000.

I am trying to get this back as a float in the CX, but when I use UDINT_TO_REAL function, it just changes it to 1.123E9. How do I get it to 123.0 in the CX?
 
There is a lesser known technique of converting by using overlapping memory space. See the attached picture that shows 3 variables all declared at %MB1000

TwinCATVarSHare.png
 
Modbus does not define in what order the 16bit words are sent when a value of 32bit is transmitted.

Some devices send the most significant word first and others do it reversed.

Probably your connected device sends the words in the reverse order than expected.

Now you need to swap the 16bit words in the PLC program
 
try different versions of byte and word swapping.

byte swap - source bytes: AB CD target bytes: BA DC
word swap - source bytes: AB CD target bytes: CD AB

and the dreaded byte/word swap - source bytes:AB CD target bytes: DC BA
 
There is a lesser known technique of converting by using overlapping memory space. See the attached picture that shows 3 variables all declared at %MB1000



That worked! Can you tell me why that worked and no ocnversion functions seemed to? Does the TwinCat not have "correct" converions functions to do this? This works, but it's not very handy.

8090Global.png
 
Can you tell me why that worked and no conversion functions seemed to?
The common conversion operators in TwinCAT work by interpreting the underlying bytes as the source type, then using the resulting value to calculate underlying byte values so the target data type is interpreted as the same value. In other words it changes the raw byte values so the value you see is the same for both data types.

In your case, you want to leave the underlying byte values the same, but interpret them as a different data type. A cleaner way than memory overlapping would be to create your own function to do this as shown in the attached picture

BitConvertUDINTtoReal.png
 
The common conversion operators in TwinCAT work by interpreting the underlying bytes as the source type, then using the resulting value to calculate underlying byte values so the target data type is interpreted as the same value. In other words it changes the raw byte values so the value you see is the same for both data types.

In your case, you want to leave the underlying byte values the same, but interpret them as a different data type. A cleaner way than memory overlapping would be to create your own function to do this as shown in the attached picture

Archie you are awesome. I created the function you have posted thank you.

So the function you posted does a byte by byte copy correct? If I then needed the change the endieness for some other use, I could change the byte order by doing the same sort of function? How would you move in byte 3 to out byte 1? Just curious.

Thanks for the help.
 
So the function you posted does a byte by byte copy correct? If I then needed the change the endieness for some other use, I could change the byte order by doing the same sort of function? How would you move in byte 3 to out byte 1? Just curious.
Yes, it is using memory address pointers to copy byte for byte. To change endianess you could start by adding 4 to one of the pointers, then subtracting 1 in each iteration of the loop.
 

Similar Topics

I am needing to convert the low DWORD of a timestamp into a LREAL. The DWORD is a 32bit unsigned number. In TwinCat2, I keep getting a negative...
Replies
3
Views
1,701
HI everyone, i am new to Siemens plc programming and i am in need of some help. yesterday we had an S7-1200 CPU 1214C fail to turn on an output to...
Replies
1
Views
21
I have an old Sentry Palletizer (S/O Number 3007 / Serial Number 1172) that has lost its program as the backup battery died years ago. I can...
Replies
0
Views
103
Hello, I need to write the following program in Ladder language, but I could not integrate the Fibonacci sequence into the ladder. Can you help...
Replies
22
Views
463
this a program to send data to barcode printer I want to integrate a new printer in a new machine and i wanted to adapt the old prgram on it but I...
Replies
4
Views
174
Back
Top Bottom