test_libsnap7_client.adb 14.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

------------------------------------------------------------------------------
--                            Ada for Automation                            --
--                                                                          --
--                   Copyright (C) 2012-2021, Stephane LOS                  --
--                                                                          --
-- This library is free software;  you can redistribute it and/or modify it --
-- under terms of the  GNU General Public License  as published by the Free --
-- Software  Foundation;  either version 3,  or (at your  option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE.                            --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Text_IO; use Ada.Text_IO;
26
with Ada.Characters.Latin_1;
27
28
29
30
31
32
33
34
35
36
37

with Interfaces.C; use Interfaces.C;

with A4A; use A4A;
with A4A.Protocols.LibSnap7; use A4A.Protocols.LibSnap7;
with A4A.Library.Conversion; use A4A.Library.Conversion;

procedure Test_libsnap7_client is

   --  Should work on Linux
   --  On Windows, not supported until W10...
38
39
40
   RED_CONSOLE    : constant String := Ada.Characters.Latin_1.ESC & "[41m";
   GREEN_CONSOLE  : constant String := Ada.Characters.Latin_1.ESC & "[42m";
   NORMAL_CONSOLE : constant String := Ada.Characters.Latin_1.ESC & "[0m";
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

   Client       : aliased S7Object;
   Result       : C.int;
   Connected    : Boolean := False;
   MyUsrDataIn  : aliased Byte_Array (0 .. 19) := (others => 0);
   MyUsrDataOut : Byte_Array (0 .. 19) := (others => 0);
   MW0          : Word  := 0;
   MW2          : Word  := 0;
   MW4          : Word  := 0;
   MD8          : DWord := 0;
   MD8F         : Float := 0.0;
   EW0          : Word  := 0;
   AW0          : Word  := 0;
   DB1_W0       : Word  := 0;
   T1           : Word  := 0;
   C1           : Word  := 0;

   procedure Byte_String
     (Item : Byte; Buffer : in out String; Pos : in out Integer);

   procedure Byte_String
     (Item : Byte; Buffer : in out String; Pos : in out Integer) is

      Hex   : constant array
        (Byte range 0 .. 15) of Character := "0123456789ABCDEF";

   begin

      Buffer (Pos)     := Hex (Item / 16);
      Buffer (Pos + 1) := Hex (Item mod 16);

      Pos := Pos + 2;

   end Byte_String;

   function Data_String (DataIn : Byte_Array) return String;

   function Data_String (DataIn : Byte_Array) return String is

      Data_Count : constant Natural := DataIn'Length;

      --  We want to display the data bytes in hex
      --  00, ff, ...
      --  00 is 2
      --  00, ff is 6
      --  00, aa, ff is 10

      Result : String (1 .. (Data_Count * 4) - 2) := (others => ' ');

      Pos   : Integer := Result'First;

      Index : Integer := DataIn'First;

   begin

      Byte_String
        (Item   => DataIn (Index),
         Buffer => Result,
         Pos    => Pos);

      loop

         Index := Index + 1;

         exit when Index > DataIn'First + Data_Count - 1;

         if (Index - 1) mod 10 = 9 then
            Result (Pos .. Pos + 1) := CRLF;
         else
            Result (Pos .. Pos + 1) := ", ";
         end if;

         Pos := Pos + 2;

         Byte_String
           (Item   => DataIn (Index),
            Buffer => Result,
            Pos    => Pos);

      end loop;

      return Result;

   end Data_String;

------------------------------------------------------------------------------
--  Show_Data                                                               --
------------------------------------------------------------------------------
   procedure Show_Data (DataIn : Byte_Array);

   procedure Show_Data (DataIn : Byte_Array) is

133
      What : constant String := CRLF & GREEN_CONSOLE
134
135
136
137
        & "***********************************************" & CRLF
        & "          MyUsrData in Hex" & CRLF & CRLF
        & Data_String (DataIn => DataIn) & CRLF
        & "***********************************************"
138
        & NORMAL_CONSOLE & CRLF;
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
   begin
      Put (What);
   end Show_Data;

------------------------------------------------------------------------------
--  Mementos Read / Write tests                                             --
------------------------------------------------------------------------------
   procedure Test_Mementos;

   procedure Test_Mementos is

   begin

      MyUsrDataIn := (others => 0);

      Result := Cli_MBRead (Client  => Client,
                            Start   => 0,
                            Size    => MyUsrDataIn'Length,
                            UsrData => MyUsrDataIn);

      if Result = 0 then
         Show_Data (DataIn => MyUsrDataIn);

         Bytes_To_Word (LSB_Byte => MyUsrDataIn (1),
                        MSB_Byte => MyUsrDataIn (0),
                        Word_out => MW0);
         Put_Line ("Read MW0 : " & MW0'Img & " ! :-)");

         Bytes_To_Word (LSB_Byte => MyUsrDataIn (3),
                        MSB_Byte => MyUsrDataIn (2),
                        Word_out => MW2);
         Put_Line ("Read MW2 : " & MW2'Img & " ! :-)");

         Bytes_To_Word (LSB_Byte => MyUsrDataIn (5),
                        MSB_Byte => MyUsrDataIn (4),
                        Word_out => MW4);
         Put_Line ("Read MW4 : " & MW4'Img & " ! :-)");

         Bytes_To_DWord (Byte0 => MyUsrDataIn (11),
                         Byte1 => MyUsrDataIn (10),
                         Byte2 => MyUsrDataIn (9),
                         Byte3 => MyUsrDataIn (8),
                         DWord_out => MD8);
         Put_Line ("Read MD8 : " & MD8'Img & " ! :-)");

         MD8F := DWord_To_Float (MD8);
         Put_Line ("Read MD8F : " & MD8F'Img & " ! :-)");

      else
         Put_Line ("Read Mementos failed ! :-(");
      end if;

      MyUsrDataOut (0 .. 3) := MyUsrDataIn (8 .. 11);

      Result := Cli_MBWrite (Client  => Client,
                             Start   => MyUsrDataIn'Length,
                             Size    => MyUsrDataOut'Length,
                             UsrData => MyUsrDataOut);

      if Result = 0 then
         Put_Line ("Write MB succeeded! :-)");
      else
         Put_Line ("Write MB failed ! :-(");
      end if;

   end Test_Mementos;

------------------------------------------------------------------------------
--  Inputs Read / Write tests                                             --
------------------------------------------------------------------------------
   procedure Test_Inputs;

   procedure Test_Inputs is

   begin

      MyUsrDataIn := (others => 0);

      Result := Cli_EBRead (Client  => Client,
                            Start   => 0,
                            Size    => MyUsrDataIn'Length,
                            UsrData => MyUsrDataIn);

      if Result = 0 then
         Bytes_To_Word (LSB_Byte => MyUsrDataIn (1),
                        MSB_Byte => MyUsrDataIn (0),
                        Word_out => EW0);
         Put_Line ("Read EW0 : " & EW0'Img & " ! :-)");

         MyUsrDataOut (0 .. 3) := MyUsrDataIn (0 .. 3);
      else
         Put_Line ("Read Inputs failed ! :-(");
      end if;

      Result := Cli_EBWrite (Client  => Client,
                             Start   => 10,
                             Size    => MyUsrDataOut'Length,
                             UsrData => MyUsrDataOut);

      if Result = 0 then
         Put_Line ("Write EB succeeded! :-)");
      else
         Put_Line ("Write EB failed ! :-(");
      end if;

   end Test_Inputs;

------------------------------------------------------------------------------
--  Outputs Read / Write tests                                             --
------------------------------------------------------------------------------
   procedure Test_Outputs;

   procedure Test_Outputs is

   begin

      MyUsrDataIn := (others => 0);

      Result := Cli_ABRead (Client  => Client,
                            Start   => 0,
                            Size    => MyUsrDataIn'Length,
                            UsrData => MyUsrDataIn);

      if Result = 0 then
         Bytes_To_Word (LSB_Byte => MyUsrDataIn (1),
                        MSB_Byte => MyUsrDataIn (0),
                        Word_out => AW0);
         Put_Line ("Read AW0 : " & AW0'Img & " ! :-)");

         MyUsrDataOut (0 .. 3) := MyUsrDataIn (0 .. 3);
      else
         Put_Line ("Read Outputs failed ! :-(");
      end if;

      Result := Cli_ABWrite (Client  => Client,
                             Start   => 10,
                             Size    => MyUsrDataOut'Length,
                             UsrData => MyUsrDataOut);

      if Result = 0 then
         Put_Line ("Write AB succeeded! :-)");
      else
         Put_Line ("Write AB failed ! :-(");
      end if;

   end Test_Outputs;

------------------------------------------------------------------------------
--  Data Blocks Read / Write tests                                          --
------------------------------------------------------------------------------
   procedure Test_DBs;

   procedure Test_DBs is

   begin

      MyUsrDataIn := (others => 0);

      Result := Cli_DBRead (Client   => Client,
                            DBNumber => 1,
                            Start    => 0,
                            Size     => MyUsrDataIn'Length,
                            UsrData  => MyUsrDataIn);

      if Result = 0 then
         Bytes_To_Word (LSB_Byte => MyUsrDataIn (1),
                        MSB_Byte => MyUsrDataIn (0),
                        Word_out => DB1_W0);
         Put_Line ("Read DB1.DBW0 : " & DB1_W0'Img & " ! :-)");

         MyUsrDataOut (0 .. 3) := MyUsrDataIn (0 .. 3);
      else
         Put_Line ("Read Data Block failed ! :-(");
      end if;

      Result := Cli_DBWrite (Client   => Client,
                             DBNumber => 1,
                             Start    => 10,
                             Size     => MyUsrDataOut'Length,
                             UsrData  => MyUsrDataOut);

      if Result = 0 then
         Put_Line ("Write Data Block succeeded! :-)");
      else
         Put_Line ("Write Data Block failed ! :-(");
      end if;

   end Test_DBs;

------------------------------------------------------------------------------
--  Timers Read / Write tests                                             --
------------------------------------------------------------------------------
   procedure Test_Timers;

   procedure Test_Timers is

   begin

      MyUsrDataIn := (others => 0);

      Result := Cli_TMRead (Client  => Client,
                            Start   => 0,
                            Amount  => MyUsrDataIn'Length / 2,
                            UsrData => MyUsrDataIn);

      if Result = 0 then
         Bytes_To_Word (LSB_Byte => MyUsrDataIn (3),
                        MSB_Byte => MyUsrDataIn (2),
                        Word_out => T1);
         Put_Line ("Read T1 in Hex: "
                   & Data_String (MyUsrDataIn (2 .. 3)) & " ! :-)");

         MyUsrDataOut (0 .. 3) := MyUsrDataIn (0 .. 3);
         MyUsrDataOut (3) := MyUsrDataIn (3) and 16#F0#; -- Let's play !
      else
         Put_Line ("Read Timers failed ! :-(");
      end if;

      Result := Cli_TMWrite (Client  => Client,
                             Start   => 0,
                             Amount  => MyUsrDataOut'Length / 2,
                             UsrData => MyUsrDataOut);

      if Result = 0 then
         Put_Line ("Write Timers succeeded! :-)");
      else
         Put_Line ("Write Timers failed ! :-(");
      end if;

   end Test_Timers;

------------------------------------------------------------------------------
--  Counters Read / Write tests                                             --
------------------------------------------------------------------------------
   procedure Test_Counters;

   procedure Test_Counters is

   begin

      MyUsrDataIn := (others => 0);

      Result := Cli_CTRead (Client  => Client,
                            Start   => 0,
                            Amount  => MyUsrDataIn'Length / 2,
                            UsrData => MyUsrDataIn);

      if Result = 0 then
         Bytes_To_Word (LSB_Byte => MyUsrDataIn (3),
                        MSB_Byte => MyUsrDataIn (2),
                        Word_out => C1);
         Put_Line ("Read C1 in Hex: "
                   & Data_String (MyUsrDataIn (2 .. 3)) & " ! :-)");

         MyUsrDataOut (0 .. 2) := MyUsrDataIn (0 .. 2);
         MyUsrDataOut (3) := MyUsrDataIn (3) and 16#F0#; -- Let's play !
      else
         Put_Line ("Read Counters failed ! :-(");
      end if;

      Result := Cli_CTWrite (Client  => Client,
                             Start   => 0,
                             Amount  => MyUsrDataOut'Length / 2,
                             UsrData => MyUsrDataOut);

      if Result = 0 then
         Put_Line ("Write Counters succeeded! :-)");
      else
         Put_Line ("Write Counters failed ! :-(");
      end if;

   end Test_Counters;

begin

   Client := Cli_Create;

   Result := Cli_ConnectTo (Client     => Client,
                            IP_Address => "192.168.253.240",
                            Rack       => 0,
                            Slot       => 3);

   if Result = 0 then
422
      Put_Line (GREEN_CONSOLE & "Connected ! :-)" & NORMAL_CONSOLE);
423
424
      Connected := True;
   else
425
      Put_Line (RED_CONSOLE & "Not Connected ! :-(" & NORMAL_CONSOLE);
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
   end if;

   if Connected then
      for Index in 1 .. 10 loop

         Test_Mementos;

         Test_Inputs;

         Test_Outputs;

         Test_DBs;

         Test_Timers;

         Test_Counters;

         delay 1.0;
      end loop;

      New_Line;

   end if;

   if Connected then
      Result := Cli_Disconnect (Client => Client);

      if Result = 0 then
454
455
         Put_Line (GREEN_CONSOLE & "Disconnected ! :-)" & NORMAL_CONSOLE);
         Connected := True;
456
      else
457
         Put_Line (RED_CONSOLE & "Not Disconnected ! :-(" & NORMAL_CONSOLE);
458
459
460
461
462
463
464
465
      end if;

   end if;

   Cli_Destroy (Client'Access);

   Put_Line ("Test_libsnap7_client finished !");
end Test_libsnap7_client;