Moving Bytes into Integers and back

Hello,

I’m a beginner in B&R PLC.
In my project I am working with an IO Link storage device (IFM DSU100). From the device I get a data array of 28 Bytes to read/write . So I need to read/write some UINT variables on it.
How can I split UINT variables into this byte array and reverse how can I combine some bytes from this array into UINT variables ?
I’m coding in ST.

1 Like

Hello,
here are some examples of how to split or merge Data with different Data Types.
With the Operator ADR you get the Adress of an Variable, so you can work with the memory itself, without restrictions by the Datatype. While working with Pointers make sure that they have a correct target at any time.

VAR
	Data : ARRAY[0..27] OF USINT;
	pUINT : REFERENCE TO UINT;
	myUINT : UINT;
	myBOOL : BOOL;
END_VAR

PROGRAM _CYCLIC
	
	
	// ---------------- Working with Pointer
	// Write
	pUINT ACCESS ADR(Data[0]);
	pUINT := myUINT; 
	
	// Read 
	myUINT := pUINT; 
	
	// ---------------- Working with brsmemcopy of AsBrStr
	// write
	brsmemcpy(ADR(Data[0]),ADR(myUINT),SIZEOF(myUINT));
	
	// read
	brsmemcpy(ADR(myUINT),ADR(Data[0]),SIZEOF(myUINT));	
	

	// ---------------- Working with only Bits of an UINT 
	myBOOL := myUINT.0; 
	
	
END_PROGRAM

2 Likes

Thank you for your Solution Michael

I tried with brsmemcpy , It worked to write myUINT to DATA[0] but only if myUINT < 255. How can I handle it if myUINT > 255 to write it on multiple Bytes of the array ?

Hello,
The UINT is a 2 Byte Datatype and is written to Data[0] LowByte and Data[1] HightByte.

Value → High Byte + Low Byte
258 → 1 * 256 + 2

image

Please have a look at the length Parameter, to realy copy 2 Bytes. Do you have the correct Datatype for myUINT.

Do you need a different Behaviour?

Greetings
Michael

Actually MyData is an array of BYTES and myUNIT is an UINT. For example I would like to store a UINT or UDINT counter in my array of BYTES

Hello,
you mean your data has this Declaration

VAR
	Data : ARRAY[0..27] OF BYTE;
END_VAR
	// write
	brsmemcpy(ADR(Data[0]),ADR(myUINT),SIZEOF(myUINT));

image

This will result in the same View, as USINT and BYTE have the same Memory-Space.

Here as Binary were you can see how the Data is moved.
image

If you read back the Information, you have to use both Array Elements to generate the UINT-Data.

Greetings
Michael

1 Like

Ok, now I got It, thank you.

2 Likes