Beckhoff#Convert the byte array to variables in TwinCAT3

I will Explain how to convert a byte array data to any PLC data type in this post. 

For many reasons, we need to process the byte arrays(for example TCP Communications),

receive the byte array and use the program to convert the data to PLC data .the first 2 bytes is INT , the next 4 bytes is Real Variable…etc.

We can use this website to convert the variable from Float to Hex.

https://gregstoll.com/~gregstoll/floattohex/

3.41242=0x405a6517.

Please be careful that the sequence of bytes is changed depending on the Small or Big.

Method1-UNION DUT 

UNION is a special DUT in which all the variables inside have the same memory offset.

Let’s explain with some examples.

Step

DUT>Add>DUT.

Choose Union and OK.

Let’s define the DUT_8Bits First. It is a DUT that includes 8 BIT Devices.

This is a normal data type but not union DUT Types.

TYPE DUT_8Bits :
STRUCT
    bBit1  : BIT;
    bBit2  : BIT;
    bBit3  : BIT;
    bBit4  : BIT;
    bBit5  : BIT;
    bBit6  : BIT;
    bBit7  : BIT;
    bBit8  : BIT;    
END_STRUCT

END_TYPE

After that we will define a Union DUT that is named uDUT_16Bits.

You can see there is a UNION Keyword inside and we have 3 variables in this DUT.

But, all these 3 devices have “Same” memory offset.

Finally,iValues=bBytes=arrBool.

TYPE uDUT_16Bits :
UNION
iValues :UINT;
bBytes :ARRAY[0..1] OF BYTE;
arrBool :ARRAY[0..1] OF DUT_8Bits;
END_UNION
END_TYPE


PROGRAM MAIN_UNION_SAMPLE
VAR
u:uDUT_16Bits;
END_VAR

Let’s make some sample programs to test it.

you can see iValue=0, arrBool and bBytes are also 0.

While iValues is changed to 1, arrBool[0].bBit1 becomes true, and also the value of bBytes[0] also becomes 1 .

Next, we changed Bytes[0]=255,bBytes[1]=3, the values of iValues and arrBool were changed also.

Example

Finally, Let’s make a 32bit Real variable conservation program. 

Defines a Union Data type with a 4 Bytes length array and Real variable.

DUT

TYPE DUT_Bytes2LReal :
UNION
arrBytes :ARRAY[0..3] OF BYTE;
rReal :REAL;

END_UNION
END_TYPE

VAR

PROGRAM MAIN_UsingUNION

VAR

rMyRealValue :REAL;

data :Dut_Bytes2LReal;

cLSB :BOOL:=TRUE;

END_VAR

Code

IF NOT cLSB THEN
data.arrBytes[0]:=16#40;
data.arrBytes[1]:=16#5A;
data.arrBytes[2]:=16#65;
data.arrBytes[3]:=16#17;
ELSE
data.arrBytes[0]:=16#17;
data.arrBytes[1]:=16#65;
data.arrBytes[2]:=16#5A;
data.arrBytes[3]:=16#40;

END_IF

rMyRealValue:=data.rReal;

Result

you can see the 4 Byte array is converted to a 32 bit Real Variable.

Method2-Use Pointer

Using ADR() Keyword to read the Memory address of the Byte array , and then read the Byte array as a Real variable by using “^” keyword. This is a very formal operation in the Beckhoff Function Block.

Example

VAR

PROGRAM MAIN_UsingPointer
VAR

rMyRealValue:REAL;
arrBytes :ARRAY[0..3] OF BYTE;
cLSB :BOOL:=TRUE;
pReal :POINTER TO REAL;

END_VAR

Code

IF NOT cLSB THEN
arrBytes[0]:=16#40;
arrBytes[1]:=16#5A;
arrBytes[2]:=16#65;
arrBytes[3]:=16#17;
ELSE
arrBytes[0]:=16#17;
arrBytes[1]:=16#65;
arrBytes[2]:=16#5A;
arrBytes[3]:=16#40;

END_IF

pReal:=ADR(arrBytes);

rMyRealValue:=pReal^ ;

Result

you can get the same result.

Please download the sample code by this link:

https://github.com/soup01Threes/TwinCAT3/blob/main/TwinCAT%20Project_ConvertBytesArray2Real.tnzip

Footer_Basic

Please Support some devices for my blog

Amazon Gift List

Find ME

Twitter:@3threes2
Email:soup01threes*gmail.com (* to @)
YoutubeChannel:https://www.youtube.com/channel/UCQ3CHGAIXZAbeOC_9mjQiWQ

シェアする

  • このエントリーをはてなブックマークに追加

フォローする