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.
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
π
2
Last edit: gised-link 2021-09-08
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 differTYPE Clinet_Tx1_t :STRUCT header : DINT; address : UDINT; value : DINT;END_STRUCTEND_TYPE
Then instead of
Clinet_Tx1 : ARRAY [0..gvlSetting.gc_wMaxTelegram] OF BYTE;
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
Hi,
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
note NBS.TCP_Write.pData should be either ADR( my_arr_value ) or ADR( my_arr_value[0] )
Arf good point. C programer habbits...
I have edited my post
Thank you so much. I could solve the problem.
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
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
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):
Then instead 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...
Related
Talk.ru: 8
Last edit: gised-link 2021-09-09