We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners who may combine it with other information that you've provided to them or that they've collected from your use of their services.

Reading complex variables (Struct)

So far I have described ways of reading BOOL, BYTE and WORD variables. It is the time to look into reaching structures i.e. variables build of a group of variables.

As a start I define a testStruct type in the ‘Data types’ tab:

TYPE testStruct :
	STRUCT
		id : BYTE;
		state : WORD;
		name : STRING[10];
		data : ARRAY [0..5] OF BYTE;
	END_STRUCT
END_TYPE

In the program I define a variable Structure of testsTruct type:

VAR
	Structure : testStruct;
END_VAR

Then I assing some values to the new variable:

Structure.id:=10;
Structure.state:=1000;
Structure.name:='testing';
Structure.data[0]:=11;
Structure.data[1]:=12;

Additionally I create a visualization ‘test_visu’ where I place 5 rectangles, showing values of the 5 elements of the Structure variable.

visu definitions

After compiling the program, uploading it to the controller I connect via FTP and in the ‘PLC’ directory I find ‘text_visu.xml’, where at the very end I see:

<variable name="PLC_PRG.Structure.id">4,176680,1,2</variable>
<variable name="PLC_PRG.Structure.state">4,176682,2,3</variable>
<variable name="PLC_PRG.Structure.name">4,176684,11,8</variable>
<variable name="PLC_PRG.Structure.data[0]">4,176695,1,2</variable>
<variable name="PLC_PRG.Structure.data[1]">4,176696,1,2</variable>

By placing all the elements of the Struct variable in a visualization accessible via web we can get the addresses of all those elements. Structure.id has 176680, Structure.state – 176682, Structure.name – 176684, Structure.data – 176695.

The structure of element addresing stays the same for a given data type. It means that if I define another variable Struct2 : testStruct and place in the visualization its first elementonly only, I can calculate the addresses of the remaining elements:

  • Structure2.state – 4,address of Structure2.id+2,2,3
  • Structure2.name – 4,address of Structure2.id+4,11,8
  • Structure2. Data[0] – 4,address of Structure2.id+15,1,2
  • Structure2.Data[1] – 4, address of Structure2.id+17,1,2 etc…..

Let’s try a real example. In my program I use the WAGO Scheduler_02.lib library and store calendar data in variables of typScheduleWeekly type.
In my program I have:

VAR
	Blind_Salon_Data1 : typScheduleWeekly;
END_VAR

In the library exploror (Resources->Library Manager->Data types) I can see that typScheduleWeekly is defined as:

TYPE typScheduleWeekly :
	STRUCT
		ON_hour : WORD;
		ON_minute : WORD;
		OFF_hour : WORD;
		OFF_minute : WORD;
		Weekday : WORD;
		Active : WORD;
	END_STRUCT
END_TYPE

When in a visualization accessible through the web an element is placed which shows the value of PLC_PRG.Blind_Salon_Data1.ON_hour, after compiling and uploading the program, in the ‘PLC’ directory in ‘visualization-name.xml’ I find:

<variable name="PLC_PRG.Blind_Salon_Data1.ON_hour">3,6232,2,3</variable>

Knowing the structure of typScheduleWeekly (all elements are WORD) and knowing that the first element element ON_hour has address 3,6232,2,3, I can calculate that:

  • PLC_PRG.Blind_Salon_Data1.ON_minute has address 3,6234,2,3  (6232+2)
  • PLC_PRG.Blind_Salon_Data1.OFF_hour has address 3,6236,2,3 
  • PLC_PRG.Blind_Salon_Data1.OFF_minute has address 3,6238,2,3
  • PLC_PRG.Blind_Salon_Data1.Weekday has address 3,6240,2,3, and
  • PLC_PRG.Blind_Salon_Data1.Active has address 3,6242,2,3

So, finally – is it possible to read complex variables (type STRUCT)?  Yes it is!  One needs to place in a visualization the variable's first element, read its address from the xml file, calculate the addresses of the remaining elements and read them one-by-one (how? read the previous article).

If we know the above we can read the values of complex variables via MODBUS and with READPI queries.  One would need to find out how the addresses presented in the visualizations (like 3,6232) transfer to the addresses used by MODBUS and the web server replying to READPI requests.

Now is the time to extend my web visualizations with schedulers control :) UPDATE:  See this article!