Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Configuring a 2's compliment

vvora
2021-11-29
2021-12-05
  • vvora

    vvora - 2021-11-29

    Hi,

    I have an array of Bytes reading different messages from a COB-ID, one of them is a 16 bit signed INT, 2's compliment and other one is 16 bit unsigned INT, 2's compliment. What would be the best way to read them. Any function block for conversion? I assume I will have to take 2's compliment of the data read?

    Thanks

     
  • timvh

    timvh - 2021-12-05

    Possible solution is to define a DUT Type Union with Array of 2 bytes and an int:

    TYPE U_2B_I :
    UNION
        ab : ARRAY[0..1] OF BYTE;
        i : INT;
    END_UNION
    END_TYPE
    

    Then declare a variable of this type in your application, write the 2 bytes into the array and use the int

    VAR
        u1 : U_2B_I;
        iResult: INT;
    END_VAR
    
    u1.ab[0] := 2#0000_0000;
    u1.ab[1] := 2#1000_0000;
    iResult := u1.i;
    
     

Log in to post a comment.