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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.</some>
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
shooter hat geschrieben:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.</some>
thanks you reply, this is great!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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”
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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)
The function fills the STRUCT and returns the correct decimal number from the byte array with use of a pointer.
(* The function assumes that the 4 bytes are allready formatted according to IEEE Standard 754 Short Real Number *)FUNCTIONBYTES_TO_REAL:REALVAR_INPUTB3:BYTE;B2:BYTE;B1:BYTE;B0:BYTE;END_VARVARCONVERT: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!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
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.
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.
The most robust way is to use a UNION!
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.</some>
Are there Unions in V2? My brain doesn't think in V3 yet
thanks your reply, REAL need 4 bytes, so i need fill in a BYTE[0..3], NOT into a single BYTE.
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.</some>
thanks you reply, this is great!
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”
union does exist in V3 not in V2.3
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.
l viewtopic.php?f=2&t=5045#p8228 l
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)
The function fills the STRUCT and returns the correct decimal number from the byte array with use of a pointer.
The function can be adapted long reals (8 Bytes) or any other user specific need.
Good luck!
the problem was in 2013, however it is a solution.
have a look at oscat.de for more functions.