Convert Byte into Array

Anonymous
2019-01-17
2019-01-21
  • Anonymous - 2019-01-17

    Originally created by: Le_Baloo

    Hello all,

    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

     
  • dFx

    dFx - 2019-01-17

    Byte into an bit array ? Then declare a union datatype with Byte and your bits (named into a DUT or in array)

     
  • Anonymous - 2019-01-17

    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

    I'd just:

    Bit0 := (MyByte AND 16#1) <> 0;
    Bit1 := (MyByte AND 16#2) <> 0;
    Bit2 := (MyByte AND 16#4) <> 0;
    
     
  • Lo5tNet - 2019-01-17

    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.
    https://help.codesys.com/webapp/unpack;product=codesys;version=3.5.11.0

    Or you could do it by hand.

        BoolArray[0] := Byte.0;
        BoolArray[1] := Byte.1;
        BoolArray[2] := Byte.2;
        BoolArray[3] := Byte.3;
        BoolArray[4] := Byte.4;
        BoolArray[5] := Byte.5;
        BoolArray[6] := Byte.6;
        BoolArray[7] := Byte.7;
    
     

    Related

    Talk.ru: 1
    Talk.ru: 2
    Talk.ru: 3
    Talk.ru: 5
    Talk.ru: 7

  • dFx

    dFx - 2019-01-18

    So if you want to name it, declarations :

    TYPE MyPumpCmd_STRUCT :
    STRUCT
       Bit0 : BIT;
       Bit1 : BIT;
       Bit2 : BIT;
       Bit3 : BIT;
       Bit4 : BIT;
       Bit5_Run : BIT;
       Bit6_Reverse : BIT;
       Bit7_QuickStop : BIT;
    END_STRUCT
    END_TYPE
    
    TYPE MyPumpCmd_UNION :
    UNION
       aByte : BYTE;
       aStruct : MyPumpCmd_STRUCT;
    END_UNION
    END_TYPE
    
    VAR_GLOBAL
       MyPumpCmd : MyPumpCmd_UNION;
    END_VAR
    

    Then, usage :

    MyPumpCmd.Bit5_Run := TRUE
    %OB5 := MyPumpCmd.aByte
    

    EDIT 2019/01/21: Correct "Bit3 : BOOL;" to "Bit3 : BIT;"

     
  • Lo5tNet - 2019-01-18

    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.

     
  • dFx

    dFx - 2019-01-21

    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.