Converting REAL to Byte

SilverShotBee

Member
Join Date
May 2011
Location
England
Posts
46
Hi, I'm faced with a rather annoying issue. I'm using CoDeSys 2.3 on an IFM CR0401 PLC which is talking to a CR0451 HMI via a CANfox network. The network is only capable of transmitting BYTES, which is hindering my accuraccy of information. For example, I want to send a number higher than 255, or I want to send a number of 23.56.

In both of the above situations I end up with errors in the transmitted value. When converting a REAL to BYTE, if the REAL value is 256 then the BYTE value is 1... If I send 23.56 as the REAL value then the BYTE value is 24 as it is rounded up.

Is there anyway I can deconstruct the REAL value and reconstruct later to keep my accuracy?

Thanks
 
All networks transmit bytes. For your floats, you send the 4 consecutive (or eight for a double) that make up the float, and reassemble them at the receiving end.
Depending on the platform, they might arrive in the correct order, or they might high word/low word swapped, or even backwards, but that depends on your platforms.
 
All networks transmit bytes. For your floats, you send the 4 consecutive (or eight for a double) that make up the float, and reassemble them at the receiving end.
Depending on the platform, they might arrive in the correct order, or they might high word/low word swapped, or even backwards, but that depends on your platforms.

I'm quite new to this and am having difficulty in actually splitting up the floats (or reals?) in to the 4 consecutive bytes, how would I go about this?
 
Something like this (syntax not checked):

Code:
VAR
bytARR : ARRAY[0..3] OF BYTE;
ptrValue : POINTER TO REAL;
rValue : REAL;
END_VAR

ptrValue := ADR(bytARR);   
ptrValue^ := rValue;
 
Thanks LD, that works a treat! :)

Once I have sent these bytes from the array over the CAN network though, how would I reconstruct the number? Is there a reverse of ADR?
 
To "copy" the array of 4 bytes to a Real var then use
Code:
rValue:=ptrValue^;

Hmm, I'm not sure i follow here. I am sending the array over the CAN network, when it arrives at the its destination, it is still in an array of bytes, I cant seem to copy this array back in to a real value with the method your using, I get conversion type errors
 

Similar Topics

Hi all, I've got a bit of a need to convert an integer (and a real in another situation) to a string with a particular format. This seems possible...
Replies
11
Views
2,448
Hello all, We need to convert ASCII data into Real Raw data, here we use Trimble GPS and need to get the position data. We use SCADAPack 357...
Replies
4
Views
2,837
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,685
I am having problems expressing an ANALOG OUTPUT 16bit INT word (0-10V Proportional Valve Voltage) as a REAL decimal number. From my...
Replies
6
Views
9,168
im using DVP-04pts and here is the download link to see its output...
Replies
5
Views
1,867
Back
Top Bottom