Help with TwinCat2 Modbus read float.

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...
3
Replies
3,256
Views
Hi, A few days ago, a GE 90-30 system from the late 90s/early 2000s fell under my maintenance. It’s a system for operating a railroad crossing...
4
Replies
104
Views
Help Needed: Modbus Register Reading – Flygt SRC 311 Hello everyone, I'm integrating three Flygt SRC 311 SmartRun VSDs with an Emerson...
7
Replies
167
Views
Hello everyone, I'm interested in understanding how PLC-based monitoring systems can help identify early warning signs of mechanical seal failure...
2
Replies
250
Views
Good day, experts! I am a fresh grad service engineer and I encounter a problem while uploading an existing program, it says I need...
0
Replies
128
Views
Top