Mapping of PLC and IEC process data
Here you can unpack the complete PLC sources: TutorialSample.zip
The TwinCAT PLC process data are cyclically mapped (copied) into the IEC process data (application objects) and vice versa at program runtime. For mapping of the IEC<->PLC process data up to 4 process data areas (IO inputs, IO outputs, memory area, data area) can be declared as buffer variables in the PLC program. The byte size of the buffers is freely selectable and may be different for each area. Its not necessary to declare unused buffer areas.
In our introductory example we declare 4 PLC process data areas with 3000 bytes each:
PROGRAM MAIN
VAR
AODB : ARRAY[0..49] OF ST_IEC870_5_101AODBEntry;
hTable : T_HAODBTable;
init : BOOL := TRUE;
initError : UDINT;
asduAddr : UDINT := 7;
inputs AT%IB0 : ARRAY[0..2999] OF BYTE;
outputs AT%QB0 : ARRAY[0..2999] OF BYTE;
memory AT%MB0 : ARRAY[0..2999] OF BYTE;
data : ARRAY[0..2999] OF BYTE;
END_VAR
How the process data are to be mapped at runtime is specified during configuration of the application objects via the F_iecAddTableEntry function.
See also in: Declaration and configuration of application objects.
The buffer variables were now declared as byte arrays. To improve access to the required data we define the individual variables a second time and allocate them to the corresponding byte/bit offset addresses. In the event of a change in the byte array the corresponding variable is changed at the same time and vice versa, although this is not compulsory. The bytes/bits of the byte array buffer variables can be accessed directly.
VAR_GLOBAL
(* Memory offset 0..99 unused *)
(* Single points *)
msgSingle_0 AT%MX100.0 : BOOL;
msgSingle_1 AT%MX100.1 : BOOL;
msgSingle_2 AT%MX100.2 : BOOL;
(* Double points *)
(* Bit 0..1 = first double point,
Bit 2..3 = second double point,
Bit 4..5 = third double point,
Bit 6..7 = fourth double point *)
msgDouble_0 AT%MB200 : BYTE;
(* Regulating step values *)
msgStep_0 AT%MB300 : BYTE;
msgStep_1 AT%MB301 : BYTE;
msgStep_2 AT%MB302 : BYTE;
(* 32 bit strings *)
msgBitStr_0 AT%MD400 : DWORD;
msgBitStr_1 AT%MD404 : DWORD;
msgBitStr_2 AT%MD408 : DWORD;
(* Measured values, normalized values *)
msgNormalized_0 AT%MW500 : WORD;
msgNormalized_1 AT%MW502 : WORD;
msgNormalized_2 AT%MW504 : WORD;
(* Mesured values, scaled values *)
msgScaled_0 AT%MW600 : INT;
msgScaled_1 AT%MW602 : INT;
msgScaled_2 AT%MW604 : INT;
(* Measured values, short floating point values *)
msgFloating_0 AT%MD700 : REAL;
msgFloating_1 AT%MD704 : REAL;
msgFloating_2 AT%MD708 : REAL;
(* Integrated totals *)
msgTotal_0 AT%MD800 : UDINT;
msgTotal_1 AT%MD804 : UDINT;
msgTotal_2 AT%MD808 : UDINT;
(* Single commands *)
cmdSingle_0 AT%MX2100.0 : BOOL;
cmdSingle_1 AT%MX2100.1 : BOOL;
cmdSingle_2 AT%MX2100.2 : BOOL;
(* Double commands *)
(* Bit 0..1 = first double command,
Bit 2..3 = second double command,
Bit 4..5 = third double command,
Bit 6..7 = fourth double command *)
cmdDouble_0 AT%MB2200 : BYTE;
(* 32 bit string commands *)
cmdBitStr_0 AT%MD2400 : DWORD;
cmdBitStr_1 AT%MD2404 : DWORD;
cmdBitStr_2 AT%MD2408 : DWORD;
(* Set point, normalized values *)
cmdNormalized_0 AT%MW2500 : WORD;
cmdNormalized_1 AT%MW2502 : WORD;
cmdNormalized_2 AT%MW2504 : WORD;
(* Set point, scaled values *)
cmdScaled_0 AT%MW2600 : INT;
cmdScaled_1 AT%MW2602 : INT;
cmdScaled_2 AT%MW2604 : INT;
(* Set point, short floating point values *)
cmdFloating_0 AT%MD2700 : REAL;
cmdFloating_1 AT%MD2704 : REAL;
cmdFloating_2 AT%MD2708 : REAL;
END_VAR
Mapping of the IEC<->PLC process data in the controlling station
Process data in monitoring direction (slave->master information)
Example 1
Single point information (M_SP_NA_1) with the IOA = 100, PLC memory area, byte offset = 100, bit offset = 0.
Controlled station -> ... -> Controlling station FB -> memory[100].0 == msgSingle_0
Example 2
Measured value, short floating point value (M_ME_NC_1) with the IOA = 700, PLC memory area, byte offset = 700, bit offset = 0 (irrelevant).
Controlled station -> ... -> Controlling station FB -> memory[700..703] == msgFloating_0
Process data in control direction (master->slave commands)
Example 1
Single command state (C_SC_NA_1) with the IOA = 10, PLC memory area, byte offset = 2100, bit offset = 0.
cmdSingle_0 == memory[2100].0 -> Controlling station FB -> ... -> Controlled station
Example 2
Set point, short floating point value (C_SE_NC_1) with the IOA = 70, PLC memory area, byte offset = 2700, bit offset = 0 (irrelevant).
cmdFloating_0 == memory[2700..2703] -> Controlling station FB -> ... -> Controlled station