You are on page 1of 11

SIMATIC SCL_Book\Message 08/15/2019 12:52:42 PM

Frame Example\Source Files\...\Message Frame

1 TYPE Header
2 STRUCT //Frame header
3 ID : WORD := 16#0000; //Identifier
4 Num : INT := 0; //Number
5 Tim : TOD := TOD#00:00:00; //Time-of-day
6 END_STRUCT
7 END_TYPE
8
9

Page 1 of 11
SIMATIC SCL_Book\Message 08/15/2019 12:52:42 PM
Frame Example\Source Files\...\Message Frame

10 TYPE Message_frame
11 STRUCT //Message frame
12 Head : Header; //Frame header
13 Meas : ARRAY [1..4] OF INT; //Measured value field
14 Check: DINT := L#0; //Checksum
15 END_STRUCT
16 END_TYPE
17
18

Page 2 of 11
SIMATIC SCL_Book\Message 08/15/2019 12:52:42 PM
Frame Example\Source Files\...\Message Frame

19 DATA_BLOCK Send_mailb
20 TITLE = 'Send mailbox for the Message Frame example'
21 //The DB "Send_mailb" contains a variable with the data structure
22 //"Message_frame"
23
24 AUTHOR : Berger
25 FAMILY : SCL_Book
26 NAME : S_mailb
27 VERSION : 1.0
28
29 STRUCT
30 Data : Message_frame; //Variable with the data structure "Message_frame"
31 END_STRUCT
32
33 BEGIN
34 Data.Head.ID := 16#F100; //Default for this data block
35 END_DATA_BLOCK
36
37

Page 3 of 11
SIMATIC SCL_Book\Message 08/15/2019 12:52:42 PM
Frame Example\Source Files\...\Message Frame

38 DATA_BLOCK Receive_mailb
39 TITLE = 'Receive mailbox for the Message Frame example'
40 //The DB "Receive_mailb" contains a variable with the data structure
41 //"Message_frame"
42
43 AUTHOR : Berger
44 FAMILY : SCL_Book
45 NAME : R_mailb
46 VERSION : 1.0
47
48 STRUCT
49 Data : Message_frame; //Variable with data structure "Message_frame"
50 END_STRUCT
51
52 BEGIN
53 Data.Head.ID := 16#A003; //Default for this data block
54 END_DATA_BLOCK
55
56

Page 4 of 11
SIMATIC SCL_Book\Message 08/15/2019 12:52:42 PM
Frame Example\Source Files\...\Message Frame

57 DATA_BLOCK Meas_values
58 TITLE = 'Global data block for the measured values'
59 //The DB contains 4 fields with 4 measured values each in data format INT
60
61 AUTHOR : Berger
62 FAMILY : SCL_Book
63 NAME : M_values
64 VERSION : 1.0
65
66 STRUCT
67 M1 : ARRAY [1..4] OF INT;
68 M2 : ARRAY [1..4] OF INT;
69 M3 : ARRAY [1..4] OF INT;
70 M4 : ARRAY [1..4] OF INT;
71 END_STRUCT
72
73 BEGIN
74 END_DATA_BLOCK
75
76

Page 5 of 11
SIMATIC SCL_Book\Message 08/15/2019 12:52:42 PM
Frame Example\Source Files\...\Message Frame

77 FUNCTION Time_check : TOD


78 TITLE = 'Fetching the time-of-day from the CPU-internal real-time clock'
79 //The block uses SFC 1 READ_CLK to read the date and time-of-day from the CPU.
80 //With FC 8 DT_TOD, the time-of-day is then extracted from this variable.
81
82 AUTHOR : Berger
83 FAMILY : SCL_Book
84 NAME : CPU_Time
85 VERSION : '1.0'
86
87 VAR_TEMP
88 SFC_error : INT;
89 CPU_Time : DT;
90 END_VAR
91
92 LABEL
93 Err;
94 END_LABEL
95
96 BEGIN
97 //Reading the date/time-of-day from the CPU
98 SFC_error := READ_CLK(CDT := CPU_Time);
99 IF SFC_error <0 THEN GOTO err; END_IF;
100
101 //Convert date/time-of-day to time-of-day
102 Time_check := DT_TOD(IN := CPU_Time);
103 IF OK THEN RETURN; END_IF;
104
105 //Error occurred
106 Err:
107 Time_check := DINT_TO_TOD(DWORD_TO_DINT(16#0));
108 END_FUNCTION
109
110

Page 6 of 11
SIMATIC SCL_Book\Message 08/15/2019 12:52:42 PM
Frame Example\Source Files\...\Message Frame

111 FUNCTION_BLOCK Generate_frame


112 TITLE = 'Generating the Message Frame example'
113 //The block assembles a message frame
114
115 AUTHOR : Berger
116 FAMILY : SCL_Book
117 NAME : FramGen
118 VERSION : '1.0'
119
120 VAR_INPUT
121 Mx : INT; //Selection of the measured value field (1 to 4)
122 END_VAR
123
124 VAR
125 ID : WORD := 16#F200; //Identifier for the message frame
126 ConsecNumber : INT := 0; //Consecutive number (incremented by +1)
127 END_VAR
128
129 VAR_TEMP
130 Error : INT; //Error message of the system function calls
131 i : INT; //Runtime variable
132 END_VAR
133
134 BEGIN
135 //Enter ID and consecutive number
136 Send_mailb.Data.Head.ID := ID;
137 ConsecNumber := ConsecNumber + 1;
138 Send_mailb.Data.Head.Num := ConsecNumber;
139
140 //Enter time-of-day
141 Send_mailb.Data.Head.Tim := Time_check();
142
143 //Enter measured values
144 CASE Mx OF
145 1 : Error := BLKMOV(SRCBLK := Meas_values.M1,
146 DSTBLK := Send_mailb.Data.Meas);
147 2 : Error := BLKMOV(SRCBLK := Meas_values.M2,
148 DSTBLK := Send_mailb.Data.Meas);
149 3 : Error := BLKMOV(SRCBLK := Meas_values.M3,
150 DSTBLK := Send_mailb.Data.Meas);
151 4 : Error := BLKMOV(SRCBLK := Meas_values.M4,
152 DSTBLK := Send_mailb.Data.Meas);
153 ELSE : RETURN;
154 END_CASE;
155
156 //Generating the checksum
157 Send_mailb.Data.Check := 0;
158 FOR i := 0 TO 15 DO
159 Send_mailb.Data.Check := Send_mailb.Data.Check
160 + WORD_TO_INT(Send_mailb.DB[i]);
161 END_FOR;
162
163 END_FUNCTION_BLOCK
164
165

Page 7 of 11
SIMATIC SCL_Book\Message 08/15/2019 12:52:42 PM
Frame Example\Source Files\...\Message Frame

166 DATA_BLOCK DB_Generate


167 TITLE = 'Instance data block for FB "Generate_frame"'
168
169 AUTHOR : Berger
170 FAMILY : SCL_Book
171 NAME : FramG_DB
172 VERSION : 1.0
173 Generate_frame
174 BEGIN
175 END_DATA_BLOCK
176
177

Page 8 of 11
SIMATIC SCL_Book\Message 08/15/2019 12:52:42 PM
Frame Example\Source Files\...\Message Frame

178 FUNCTION_BLOCK Store_frame


179 TITLE = 'Entry of the received message frame in a buffer'
180
181 AUTHOR : Berger
182 FAMILY : SCL_Book
183 NAME : FramStor
184 VERSION : '1.0'
185
186 VAR_INPUT
187 Input : Message_frame;
188 END_VAR
189
190 VAR
191 Number : INT;
192 Location : INT := 7;
193 SFCError : INT;
194 Entry : ARRAY [0..7] OF Message_frame;
195 END_VAR
196
197 BEGIN
198 //Check message frame number
199 IF Input.Head.Num = Number THEN RETURN; END_IF;
200 Number := Input.Head.Num;
201
202 //Copy message frame
203 Location := (Location + 1) MOD 8;
204 SFCError := BLKMOV(
205 SRCBLK := Input,
206 DSTBLK := Entry[Location]);
207
208 END_FUNCTION_BLOCK
209
210

Page 9 of 11
SIMATIC SCL_Book\Message 08/15/2019 12:52:42 PM
Frame Example\Source Files\...\Message Frame

211 DATA_BLOCK DB_Store


212 TITLE = 'Instance data block for FB "Store_frame"'
213
214 AUTHOR : Berger
215 FAMILY : SCL_Book
216 NAME : FramS_DB
217 VERSION : 1.0
218 Store_frame
219 BEGIN
220 END_DATA_BLOCK
221
222

Page 10 of 11
SIMATIC SCL_Book\Message 08/15/2019 12:52:42 PM
Frame Example\Source Files\...\Message Frame

223 ORGANIZATION_BLOCK Cycle


224 TITLE = ';Main program'
225 //Calling the blocks for the Message Frame example.
226
227 AUTHOR : Berger
228 FAMILY : SCL_Book
229 NAME : Cycle
230 VERSION : '1.0'
231
232 VAR_TEMP
233 SINFO : ARRAY [1..20] OF BYTE;
234 END_VAR
235
236 BEGIN
237 //Generate message frame
238 //A message frame comprising ID, consecutive number, time-of-day and data
239 //(first field) is generated in the send mailbox in the event of a rising
240 //edge at the input.
241
242 IF Generate AND NOT EM_Generate
243 THEN Generate_frame.DB_Generate();
244 END_IF;
245 EM_Generate := Generate;
246
247 //Store message frame
248 //A message frame arriving in the receive mailbox is entered in the next
249 //location in the ring buffer in the event of a rising edge.
250
251 IF Store AND NOT EM_Store
252 THEN Store_frame.DB_Store(Input := Receive_mailb.Data);
253 END_IF;
254 EM_Store := Store;
255
256 END_ORGANIZATION_BLOCK
257

Page 11 of 11

You might also like