Originally created by: Le_Baloo
Have you got any ideas of how i can convert byte into an array in order to read and access to the datas ?
Thanks for your help
Byte into an bit array ? Then declare a union datatype with Byte and your bits (named into a DUT or in array)
Originally created by: ph0010421
I don't think that will work.
You can't make an array of BIT, and a BOOL occupies 8 bits, so it won't align.
There's probably an OSCAT block, but I can never find anything I want in OSCAT
Bit0 := (MyByte AND 16#1) <> 0;
Bit1 := (MyByte AND 16#2) <> 0;
Bit2 := (MyByte AND 16#4) <> 0;
A union would be nice but believe this doesn't work because of what Le_Baloo said. Been a while since I have tested it though.
What if you just use the UNPACK function. Input would be your byte and the output could tie each bit to your array.
Or you could do it by hand.
BoolArray := Byte.0;
BoolArray := Byte.1;
BoolArray := Byte.2;
BoolArray := Byte.3;
BoolArray := Byte.4;
BoolArray := Byte.5;
BoolArray := Byte.6;
BoolArray := Byte.7;
So if you want to name it, declarations :
TYPE MyPumpCmd_STRUCT :
Bit0 : BIT;
Bit1 : BIT;
Bit2 : BIT;
Bit3 : BIT;
Bit4 : BIT;
Bit5_Run : BIT;
Bit6_Reverse : BIT;
Bit7_QuickStop : BIT;
TYPE MyPumpCmd_UNION :
aByte : BYTE;
aStruct : MyPumpCmd_STRUCT;
MyPumpCmd : MyPumpCmd_UNION;
Then, usage :
MyPumpCmd.Bit5_Run := TRUE
%OB5 := MyPumpCmd.aByte
EDIT 2019/01/21: Correct "Bit3 : BOOL;" to "Bit3 : BIT;"
Thanks dFx. I learned something new but there was one issue I ran into when testing this. When you specified BOOL for Bit3 it causes it to not work. All data types appear to have to be BIT in order for the union to work.
Just a typo mistake. Edited my previous post.
BIT datatype is a bit length in memory, while BOOL is 8 bit length (a Byte).
Log in to post a comment.