CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Sat Nov 18, 2017 7:11 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Sat May 11, 2013 9:37 am 
Offline

Joined: Sat May 04, 2013 11:50 am
Posts: 17
hello, all

how i can convert and save REAL to Byte[]? also from byte[] to REAL?
I tested with SysMemCpy(), it goes in, but if the address of Byte[] elements are not continuous, then it might problem.

br/
listenyang


Top
   
PostPosted: Mon May 13, 2013 9:07 am 
Offline
Frequent User
Frequent User

Joined: Thu Jun 14, 2007 9:49 am
Posts: 985
you are talking about two different things
real to byte is just REAL_TO_BYTE
however you want to have the bytes this is done with pointers.

_________________
shooter@home.nl
computer shooter
paul deelen
jacob van wassenaerstraat 29
NL 5224 GG 's-Hertogenbosch
skype shooter paul.deelen
phone +31736139136


Top
   
PostPosted: Tue May 21, 2013 12:30 pm 
Offline
Frequent User
Frequent User

Joined: Fri Dec 23, 2005 7:36 pm
Posts: 170
If you are just looking to pack the real data into an array of bytes, the easiest path seems like it would be to create your real and your byte array at the same marker address. Then they are actually at the same place in memory.

_________________
nOrM
=====
nOrM = Norman Dziedzic Jr.


Top
   
PostPosted: Wed May 22, 2013 9:59 am 
Offline

Joined: Sun Feb 24, 2013 10:47 am
Posts: 29
The most robust way is to use a UNION!
Code:
TYPE uRealBytes :
  UNION
    rValue : REAL;    // Needs 4 Bytes in memory
    abValue:  ARRAY [0..3] OF BYTE;
  END_UNION
END_TYPE

Then you kann fill in a real by "uRealByte.rValue := <some real>;
In "uRealByte.abValue[n] you have the Bytes to copy to ...
The conversion back is done in the same way.


Top
   
PostPosted: Wed May 22, 2013 1:20 pm 
Offline
Frequent User
Frequent User

Joined: Fri Dec 23, 2005 7:36 pm
Posts: 170
Are there Unions in V2? My brain doesn't think in V3 yet

_________________
nOrM

=====

nOrM = Norman Dziedzic Jr.


Top
   
PostPosted: Thu May 23, 2013 3:23 am 
Offline

Joined: Sat May 04, 2013 11:50 am
Posts: 17
shooter wrote:
you are talking about two different things
real to byte is just REAL_TO_BYTE
however you want to have the bytes this is done with pointers.


thanks your reply, REAL need 4 bytes, so i need fill in a BYTE[0..3], NOT into a single BYTE.


Top
   
PostPosted: Thu May 23, 2013 3:23 am 
Offline

Joined: Sat May 04, 2013 11:50 am
Posts: 17
RudolfAtITD wrote:
The most robust way is to use a UNION!
Code:
TYPE uRealBytes :
  UNION
    rValue : REAL;    // Needs 4 Bytes in memory
    abValue:  ARRAY [0..3] OF BYTE;
  END_UNION
END_TYPE

Then you kann fill in a real by "uRealByte.rValue := <some real>;
In "uRealByte.abValue[n] you have the Bytes to copy to ...
The conversion back is done in the same way.


thanks you reply, this is great! :D


Top
   
PostPosted: Tue Jun 11, 2013 5:23 pm 
Offline

Joined: Tue Sep 28, 2010 4:14 pm
Posts: 5
I’m not familiar with UNION. But I like to test this.
Where do I define this Union type?
I tried under “Data Types” but then I get the error “unknown type:UNION”


Top
   
PostPosted: Tue Jun 11, 2013 5:29 pm 
Offline
Frequent User
Frequent User

Joined: Thu Jun 14, 2007 9:49 am
Posts: 985
union does exist in V3 not in V2.3

_________________
shooter@home.nl
computer shooter
paul deelen
jacob van wassenaerstraat 29
NL 5224 GG 's-Hertogenbosch
skype shooter paul.deelen
phone +31736139136


Top
   
PostPosted: Thu Jun 20, 2013 1:44 am 
Offline
User avatar

Joined: Tue Aug 30, 2011 1:21 am
Posts: 92
This can easily be done be using fixed mapping to your variables. Most CoDeSys controllers have fixed memory address. If you map a REAL and Byte array to the same memory location it will all you read the REAL as individual bytes. This method will work in all versions of CoDeSys. Below is an example how:

myReal AT %MD0 : REAL;
myByteArary AT %MB0 : ARRAY[0..3] OF BYTE;

To give you a better understanding how the memory map works refer to my other post for an excel sheet.

viewtopic.php?f=2&t=5045#p8228

_________________
The Original SoMachine Ninja


Top
   
PostPosted: Thu Feb 23, 2017 4:11 pm 
Offline

Joined: Wed Dec 28, 2016 11:00 pm
Posts: 48
I have made a simple yet flexible function which uses the pointer to memory trick, it uses a user defined struct for it to work.
The code is made in CodesysV2.3 but will also work in Codesys V3 (but in V3 a Union is shorter though)
Code:
TYPE ST_BYTE_TO_REAL :
STRUCT
   B0:   BYTE;
   B1:   BYTE;
   B2:   BYTE;
   B3:   BYTE;
   REAL_OUT : POINTER TO REAL;
END_STRUCT
END_TYPE

The function fills the STRUCT and returns the correct decimal number from the byte array with use of a pointer.
Code:
(* The function assumes that the 4 bytes are allready formatted according to IEEE Standard 754 Short Real Number *)
FUNCTION BYTES_TO_REAL : REAL
VAR_INPUT
   B3:   BYTE;
   B2:   BYTE;
   B1:   BYTE;
   B0:   BYTE;
END_VAR
VAR
   CONVERT: ST_BYTE_TO_REAL;
END_VAR

(* Initialise POINTER to address of B0 *)
CONVERT.REAL_OUT := ADR(CONVERT.B0);

(* Fill bytes *)
CONVERT.B0:=B0;
CONVERT.B1:=B1;
CONVERT.B2:=B2;
CONVERT.B3:=B3;

(* Return ptr^ (value at adress) *)
BYTES_TO_REAL := CONVERT.REAL_OUT^;

(* END *)

The function can be adapted long reals (8 Bytes) or any other user specific need.
Good luck!


Top
   
PostPosted: Thu Feb 23, 2017 7:49 pm 
Offline
Frequent User
Frequent User

Joined: Thu Jun 14, 2007 9:49 am
Posts: 985
the problem was in 2013, however it is a solution.
have a look at oscat.de for more functions.

_________________
shooter@home.nl
computer shooter
paul deelen
jacob van wassenaerstraat 29
NL 5224 GG 's-Hertogenbosch
skype shooter paul.deelen
phone +31736139136


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 12 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited