FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
10-25-2022 11:42 PM - edited 10-25-2022 11:43 PM
Hi,
I just configured Modbus on my ctrlx and was wondering if there is a way of creating alias for array elements. Like in the I/O mapping of the data layer, is there a way of saying, for exemple, the variable "start_button" points to the first element of the holdingRegister array.
I could probably do it in a routine with pointers, but I'm sure there is a simpler way to do it as it is usually a base feature in most PLC.
Thanks in advance for your replies
Solved! Go to Solution.
10-26-2022 12:33 PM - edited 10-26-2022 12:36 PM
Hello
I expect that you use the library CXA_ModbusTCP, with the example included in the library.
I did it a little bit different than the example. Here is my idea behind.
The example uses a WORD array for all different data types.
arRegister: ARRAY[0..9] OF WORD;
I use a structure instead, with arrays for all all data types I need:
TYPE REGISTER_DATA :
STRUCT
arWord : ARRAY[0..20] OF WORD; (* %MW0000 - %MW0019 *)
arInt : ARRAY[0..10] OF INT; (* %MW0020 - %MW0029 *)
arUint : ARRAY[0..10] OF UINT; (* %MW0030 - %MW0039 *)
arDint : ARRAY[0..5] OF DINT; (* %MW0040 - %MW0049 *)
arUdint : ARRAY[0..5] OF UDINT; (* %MW0050 - %MW0059 *)
arReal : ARRAY[0..5] OF REAL; (* %MW0060 - %MW0069 *)
arString : ARRAY[0..10] OF STRING[19]; (* %MW0070 - %MW0169 *)
END_STRUCT
END_TYPE
// define register data variable
regData: REGISTER_DATA;
For easy usage in the program I define enumerations for all arrays like:
ENUM EN_arWord
enLength = 0;
enWidth = 1;
END ENUM
Which means that my program is easy to read.
regData.arWord[enLength] := 10;
Attached you find an Excel document which supports you to create the necessary PLC code. The generated code is displayed in notepad for copy & paste. See Excel sheet "Docu" how to use it. You have to enable the macros included in Excel.
Alternatively you can use a structure with single variables. But this means, that you have to calculate the addresses for the Modbus partner by yourself. Or you adapt the Excel program to the single variables in the strucure.
TYPE REGISTER_DATA :
STRUCT
wLen: WORD; /* %MW0 */
wWidth: WORD; /* %MW1 */
wUnused3: WORD; /* %MW2 */
wUnused4: WORD; /* %MW3 */
wUnused5: WORD; /* %MW4 */
iTest1: INT; /* %MW5 */
iTest2: INT; /* %MW6 */
iUnused3: INT; /* %MW7 */
iUnused4: INT; /* %MW8 */
iUnused5: INT; /* %MW9 */
END_STRUCT
END_TYPE
// define register data variable
regData: REGISTER_DATA;
// usage in program
regData.wLen := 10;
10-26-2022 02:07 PM - edited 10-26-2022 02:13 PM
Keep in mind the default alignment of variables in RAM which can cause unused bytes between the structure variables.
To remove that fill bytes, you can put {attribute 'pack_mode' := '1'} into the structure definition. => Because of the WORD based adress in Modbus, you should NOT use any data types with size 1. (as. BYTE, BOOLEAN,...)
For more info see PLC docu: pack_mode