Help with TwinCat2 Modbus read float.

g.mccormick

Lifetime Supporting Member
Join Date
Jul 2012
Location
IN
Posts
960
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,680
I'm fairly new to Rockwell software, I've had some basic training in the past but nothing too advanced. My company and I use Reliable products for...
Replies
9
Views
237
Hi all I need help on this As in my project used Schneider TM241 controller. Now I want to communicate Elite energy meter with controller by...
Replies
3
Views
148
Hi all, I am having issues accessing my Cimplicity software - the site code changed after re-install and I am no longer able to attain a new key...
Replies
10
Views
156
Good day all! Can someone help me with the procedure to update Beijers E700 firmware? The Panel I am working on is firmware 2.04v and I would...
Replies
1
Views
62
Back
Top Bottom