cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

tags or reference to an item in an Array

tags or reference to an item in an Array

kovaley
Member

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

2 REPLIES 2

HmiGuide
Community Moderator
Community Moderator

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;

 

HmiGuide
Community Moderator
Community Moderator

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

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist