I do not know whether the OP meant "3rd byte" as "last of 4" or "third of 4", but in the latter case, I think the mask should be "16#FF00FFFF" and the shift should be "2*8". I would probably use "OR" rather than "+", but I think in this case they are interchangeable.
π
1
Last edit: fajean 2022-07-25
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It does not matter which byte it is exactly. I just wanted to know if there is any simple way such as by accessing a bit.
That we enter the variable name, period and bit number.
for example:
varD.0: = true;
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, I am a beginner at codesys and I have a simple question.
How can I refer to a byte in a DWORD variable?
For example:
I have a dword variable "varD" and a byte variable "varB". I want to save varB variable to 3rd byte of varD. How to write it in codesys?
Hello,
I would do this :
varD := (varD AND 16#00FFFFFF) + SHL(TO_DWORD(varB), 3*8);
However there might be better ways to do this, I'm not sure...
I do not know whether the OP meant "3rd byte" as "last of 4" or "third of 4", but in the latter case, I think the mask should be "16#FF00FFFF" and the shift should be "2*8". I would probably use "OR" rather than "+", but I think in this case they are interchangeable.
Last edit: fajean 2022-07-25
It does not matter which byte it is exactly. I just wanted to know if there is any simple way such as by accessing a bit.
That we enter the variable name, period and bit number.
for example:
varD.0: = true;
Well if you want something similar, you can create a union type like so :
Then in your program you define varD as a DWORD_BYTES object and you can access its bytes like this :
Where byte_position can be 0, 1, 2 or 3.
And if you need to change varD value you can do this for example :
varD.value := 16#FFFFFFFF;