Pack TWO INT variable to one DINT variable

2019-06-27
2024-05-07
  • jagdishradadiya - 2019-06-27

    Hello,

    I want to pack two INT variable into One DINT variable. The other PLC sending DINT data to Modbus device though Modbus TCPIP which is splitting data into Two INT.

    How would i do Packing of Two INT into One DINT on Codesys.

    please share thoughts.

     
  • josepmariarams - 2019-06-27

    Create an union with an array of two ints and one dint.

    Write in the two ints of the union, and in the dint you will have the value.

    Salut!

    Sent from my Moto G (5S) Plus using Tapatalk

     
  • jagdishradadiya - 2019-06-28

    Thank you Josep.
    First of all, I didn't mentioned..I am working on IEC61311-3 platform for PLC programming. so more comfortable with LD,FBD,ST language.

    The problem solved with MEM Library Block "Mem.PackWordsTODWord". the Block Expected Words as Input and DWORD as Output Result. But as Word and DWORD are Unsigned it can only take Positive numbers. what i wanted to have is to get any Integer number -2147483648..+2147483647.

    i would like to mentioned i got result just putting DINT type variable as Output and giving correct result. However it shows warning as Implicit Conversion from unsigned type 'DWORD' to Signed Type 'DINT': Possible change of Sign.

    However still working on TWO WORD or INT to One Real conversion.

    Please share thought if any.

     
  • somlioy - 2019-06-28

    Can also do:

    {warning disable C0308}
    diHighLow := iLow OR SHL(iHigh, 16);
    {warning restore C0308}
    
     
  • Anonymous - 2019-06-30

    Originally created by: scott_cunningham

    Alternative to disabling a compiler warning is to specifically tell the compiler you want to do that. Use conversion DWORD_TO_DINT():

    MyVar := DWORD_TO_DINT(dwVar).

    No compiler warning and it is very clear to anyone looking at your code that you wish to get the signed value back.

     
  • aris-k - 2024-05-07

    You can try this setup:

    DINTVar := SHL(INT_TO_DINT (Low_Word_INT_Var),16) + INT_TO_DINT(High_Word_INT_Var);

     

Log in to post a comment.