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

Converting UINT into bytes and converting 2Bytes into UINT

abjha1998
2023-12-07
2023-12-08
  • abjha1998 - 2023-12-07

    Hi,
    I have been working on Codesys V3.5 with PFC200 controller and Can Gateway 750-658. I want to convert the incoming CAN data from bytes into UINT and for outgoing data bytes to UINT. Does anyone has experience with this kind of data? I tried few different ways but it doesn't seem to be working.

     
  • voffi - 2023-12-07

    There are some ways. One is to use SHR and SHL and it depends on your byte order in the data array. For Motorola byte order:

    PROGRAM PLC_PRG
    VAR
        u : UINT;
        byte_array_in  : ARRAY [1..8] OF BYTE
                := [16#11, 16#12, 16#13, 16#14, 16#15, 16#16, 16#17];
        byte_array_out : ARRAY [1..8] OF BYTE;
    END_VAR
    
    u := SHL(TO_UINT(byte_array_in[2]), 8) + TO_UINT(byte_array_in[1]);
    
    byte_array_out[1] := TO_BYTE(u);
    byte_array_out[2] := TO_BYTE(SHR(u, 8));
    

    If it's Intel byte order just change 1 and 2 in the array indexes.

     

    Related

    Talk.ru: 1
    Talk.ru: 2


    Last edit: voffi 2023-12-07
  • kislov - 2023-12-08
     

    Last edit: kislov 2023-12-08
    • abjha1998 - 2023-12-08

      Thank you for the reply. I got an error saying that
      MEM.PackBytesToWord(GVL.Actual_Speed_DUT_high,GVL.Actual_Speed_DUT_low) = u; is not a valid statement. Am I missing some library?

       
    • abjha1998 - 2023-12-08

      Thank you for the reply. I got an error saying that
      MEM.PackBytesToWord(GVL.Actual_Speed_DUT_high,GVL.Actual_Speed_DUT_low) = u; is not a valid statement. Am I missing some library?

       

Log in to post a comment.