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

Assign a 12 Byte Hex

  • David.sa198

    David.sa198 - 2021-09-07

    Hello every one.
    Does anybody know how I can define a Variable and assign a 12 Byte Hex Value?
    I have defined a LWord variable but I can assign only a 8 byte Hex.
    I need to send a 12 Byte Hex number.
    Thanks a lot.


    Last edit: David.sa198 2021-09-07
  • gised-link

    gised-link - 2021-09-08


    The simpliest method is to consider your 12 bytes value as "not null terminated" string.

    When this case appears to me, I declare a byte array of the needed size (in your case 12 bytes) this way:
    my_arr_value : ARRAY[0..11] OF BYTE; // some array

    Then when I will write/copy to/from this array, I simply use MEMCPY(dest, source, n) standard function:
    // copy TCP_Write_0 in my_arr_value
    // in this case, SIZEOF(TCP_Write_0) must be greater or equal than SIZEOF(my_arr_value)
    MEMCPY(ADR(my_arr_value), TCP_Write_0.pData, SIZEOF(my_arr_value));

    You always can access any byte of your array simply by dereferences it using [] -> my_arr_value[0] := 16#AF;

    Hope it will help


    Last edit: gised-link 2021-09-08
    • i-campbell

      i-campbell - 2021-09-08

      note NBS.TCP_Write.pData should be either ADR( my_arr_value ) or ADR( my_arr_value[0] )

      • gised-link

        gised-link - 2021-09-08

        Arf good point. C programer habbits...
        I have edited my post

  • David.sa198

    David.sa198 - 2021-09-08

    Thank you so much. I could solve the problem.

    Clinet_Tx1         : ARRAY [0..gvlSetting.gc_wMaxTelegram] OF 
        Clinet_Tx1[0] := 128;
        Clinet_Tx1[1] := 9;
        Clinet_Tx1[2] := 0;
        Clinet_Tx1[3] := 0;
        Clinet_Tx1[4] := 140;
        Clinet_Tx1[5] := 16;
        Clinet_Tx1[6] := 0;
        Clinet_Tx1[7] := 0;
        Clinet_Tx1[8] := 70;
        Clinet_Tx1[9] := 0;
        Clinet_Tx1[10] := 0;
        Clinet_Tx1[11] := 0;
        // 0...3 Massage.Header 
        // 4..7 Address
        //8 ..11 Value
        // Data uses little-endian format

    But now I want to send a Double data. (For example 70.000) but Codesys send it in form of int. (70)
    And the device (Server) can not accept it.

    How can I fix it?

    Sorry for a maybe simple question. I'm learning Codesys and I don't have any experience.


    Last edit: David.sa198 2021-09-08
    • aliazzz

      aliazzz - 2021-09-08

      A small tip,

      Do not post two separate questions under a single topic, neither post questions double.

      The best way is to create a separate new topic for each question once. This way, your question stands out more.

      Good luck! ;-)


      Last edit: aliazzz 2021-09-08
    • gised-link

      gised-link - 2021-09-09

      Float is 32 bit or 4 bytes and is called REAL in the PLC world.
      Double is 64 bit or 8 bytes and is calle LREAL in PLC world.

      with your Value size is too small for a Double, so I'm assuming you will send a float/REAL instead.

      Simply do:

      MEMCPY(ADR(Clinet_Tx1[8]), ADR(my_REAL_type_value), SIZEOF(my_REAL_type_value));

      /// Going deeper on the topic ///

      With what I see, instead of an array, the best to do is to create a STRUCT type with all your needed fields in it. Like this (create a new DUT file with this inside):

      {attribute 'pack_mode' := '1'} // this is used to force 1 byte allignement, may differ
      TYPE Clinet_Tx1_t :
          header : DINT;
          address : UDINT;
          value : DINT;

      Then instead of

          Clinet_Tx1 : ARRAY [0..gvlSetting.gc_wMaxTelegram] OF 

      Use this declaration:

          Clinet_Tx1 : Clinet_Tx1_t;

      You can now use the MEMCPY function like this:

      MEMCPY(ADR(Clinet_Tx1.value), ADR(my_REAL_type_value), SIZEOF(my_REAL_type_value));

      Do not use a cast like:

      Clinet_Tx1.value := REAL_TO_DINT(my_REAL_type_value);

      it will convert the REAL value into a DINT
      example : 12.25 -> 12 / 10058.25489 -> 10058 / etc...



      Talk.ru: 8

      Last edit: gised-link 2021-09-09

Log in to post a comment.