simple_components_4.68.0_da9b0f3a/test_components/test_modbus_client.adb

  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
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 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
133
134
135
136
137
138
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
--                                                                    --
--  procedure Test_MODBUS_Client    Copyright (c)  Dmitry A. Kazakov  --
--  MODBUS client test                             Luebeck            --
--                                                 Spring, 2015       --
--                                                                    --
--                                Last revision :  09:02 17 Oct 2020  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either version  2  of  --
--  the License, 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  --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  --
--  General  Public  License  for  more  details.  You  should  have  --
--  received  a  copy  of  the GNU General Public License along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--____________________________________________________________________--

with Ada.Exceptions;               use Ada.Exceptions;
with Ada.Text_IO;                  use Ada.Text_IO;
with Ada.Text_IO.Text_Streams;     use Ada.Text_IO.Text_Streams;
with GNAT.Sockets.Server;          use GNAT.Sockets.Server;
with GNAT.Sockets.Server.Handles;  use GNAT.Sockets.Server.Handles;
with Interfaces;                   use Interfaces;
with Strings_Edit.Integers;        use Strings_Edit.Integers;
with Strings_Edit.Streams;         use Strings_Edit.Streams;

with GNAT.Sockets.Connection_State_Machine.MODBUS_Client.Synchronous;
with GNAT.Serial_Communications;
with GNAT.Sockets.Server.Blocking;

procedure Test_MODBUS_Client is
   Timeout : constant Duration := 1.0;

   function Image
            (  Values : GNAT.Sockets.Connection_State_Machine.
                        MODBUS_Client.Bit_Array
            )  return String is
      Result  : String (1..Values'Length + (Values'Length - 1) / 5);
      Pointer : Integer := Result'First;
   begin
     for Index in Values'Range loop
        if Pointer > 1 and then (Pointer mod 5) = 1 then
           Result (Pointer) := ' ';
           Pointer := Pointer + 1;
        end if;
        if Values (Index) then
           Result (Pointer) := '1';
        else
           Result (Pointer) := '0';
        end if;
        Pointer := Pointer + 1;
     end loop;
     return Result;
   end Image;

   function Image
            (  Values : GNAT.Sockets.Connection_State_Machine.
                        MODBUS_Client.Word_Array
            )  return String is
      Result  : String (1..Values'Length * 5 - 1);
      Pointer : Integer := Result'First;
   begin
     for Index in Values'Range loop
        if Pointer /= 1 then
           Result (Pointer) := ' ';
           Pointer := Pointer + 1;
        end if;
        Put
        (  Destination => Result,
           Pointer     => Pointer,
           Value       => Integer (Values (Index)),
           Base        => 16,
           Field       => 4,
           Justify     => Strings_Edit.Right,
           Fill        => '0'
        );
     end loop;
     return Result;
   end Image;

   procedure Test_Asynchronous is
      use GNAT.Sockets.Connection_State_Machine.MODBUS_Client;
      Factory   : aliased Connections_Factory;
      Server    : aliased Connections_Server (Factory'Access, 0);
      Reference : Handle;
   begin
      Trace_On
      (  Factory  => Factory,
         Received => GNAT.Sockets.Server.Trace_Decoded,
         Sent     => GNAT.Sockets.Server.Trace_Decoded
      );
      Set
      (  Reference,
         new MODBUS_Client (Server'Unchecked_Access, 140)
      );
      declare
         Client : MODBUS_Client renames
                  MODBUS_Client (Ptr (Reference).all);
      begin
         Connect
         (  Server,
            Client'Unchecked_Access,
            "127.0.0.1", -- "192.168.2.100", -- "127.0.0.1",
            MODBUS_Port
         );
         while not Is_Connected (Client) loop -- Busy waiting
            delay Timeout;
         end loop;
         Put_Line ("MODBUS client connected");
         Send_FC5 (Client, 100, 1, True);
         delay Timeout;
         Send_FC15 (Client, 101, (5..15=>True, 16=>False, 17=>True));
         delay Timeout;
         Send_FC1 (Client, 102, 1, 3);
         delay Timeout;
         Send_FC2 (Client, 103, 10, 20);
         delay Timeout;
         Send_FC6 (Client, 104, 0, 16#100#);
         delay Timeout;
         Send_FC16 (Client, 105, (2=>16#101#,3=>16#102#,4=>16#103#));
         delay Timeout;
         Send_FC23
         (  Client,
            105,
            0, 4,
            (5=>16#201#,6=>16#202#,7=>16#203#)
         );
         delay Timeout;
         Send_FC24 (Client, 106, 16#6000#);
         delay Timeout;
         Send_FC7 (Client, 106);
         delay Timeout;
      end;
   end Test_Asynchronous;

   procedure Test_RTU (COM_Port : String) is
      use GNAT.Sockets.Connection_State_Machine.MODBUS_Client;
      use Synchronous;
      use GNAT.Serial_Communications;
      Factory   : aliased Connections_Factory;
      Port      : aliased Serial_Port;
      Reference : Handle;
   begin
      Open (Port, Port_Name (COM_Port));
      Set
      (  Port    => Port,
         Block   => False,
         Flow    => RTS_CTS,
         Timeout => 2.0
      );
      Trace_On
      (  Factory  => Factory,
         Received => GNAT.Sockets.Server.Trace_Decoded,
         Sent     => GNAT.Sockets.Server.Trace_Decoded
      );
      declare
         Server : aliased GNAT.Sockets.Server.Blocking.Blocking_Server
                          (  Factory'Access,
                             Port'Unchecked_Access,
                             Port'Unchecked_Access,
                             80
                          );
      begin
         Set
         (  Reference,
            new MODBUS_Synchronous_Client
                (  Server'Unchecked_Access
         )      );
         declare
            Client : MODBUS_Synchronous_Client renames
                     MODBUS_Synchronous_Client (Ptr (Reference).all);
         begin
            Set_RTU_Checksum_Mode (Client, True); -- Activate checksum
            Set_RTU_Silence_Time (Client, 0.01);  -- 10ms silence time
            GNAT.Sockets.Server.Blocking.Connect
            (  Server,
               Client'Unchecked_Access
            );
            while not Is_Connected (Client) loop -- Busy waiting
               delay Timeout;
            end loop;
            Put_Line ("MODBUS RTU client connected");
            FC5 (Client, 100, 1, True, 1);
            Put_Line ("FC1 > OK");
            FC15 (Client, 101, (5..15=>True, 16=>False, 17=>True), 1);
            Put_Line ("FC15 > OK");
            Put_Line
            (  "FC1 > "
            &  Image
               (  FC1 (Client'Access, 102, 1, 3, 1)
            )  );
            Put_Line
            (  "FC2 > "
            &  Image
               (  FC2 (Client'Access, 103, 10, 20, 1)
            )  );
            FC6 (Client, 104, 0, 16#100#, 1);
            Put_Line ("FC6 > OK");
            FC16 (Client, 105, (2=>16#101#,3=>16#102#,4=>16#103#), 1);
            Put_Line
            (  "FC23 > "
            &  Image
               (  FC23
                  (  Client'Access,
                     105,
                     0, 4,
                     (5=>16#201#,6=>16#202#,7=>16#203#),
                     1,
                     3.0
            )  )  );
            Put_Line
            (  "FC24 > "
            &  Image (FC24 (Client'Access, 106, 16#6000#, 1, 5.0))
            );
            Put_Line
            (  "FC7 > " & Unsigned_8'Image (FC7 (Client'Access, 106, 1))
            );
            delay 3.0;
            Close (Port);
         end;
      end;
   end Test_RTU;

   procedure Test_Synchronous is
      use GNAT.Sockets.Connection_State_Machine.
          MODBUS_Client.Synchronous;
      Factory   : aliased Connections_Factory;
      Server    : aliased Connections_Server (Factory'Access, 0);
      Reference : Handle;
   begin
      Trace_On
      (  Factory  => Factory,
         Received => GNAT.Sockets.Server.Trace_Decoded,
         Sent     => GNAT.Sockets.Server.Trace_Decoded
      );
      Set
      (  Reference,
         new MODBUS_Synchronous_Client (Server'Unchecked_Access)
      );
      declare
         Client : MODBUS_Synchronous_Client renames
                  MODBUS_Synchronous_Client (Ptr (Reference).all);
      begin
         Connect (Client, "192.168.2.100");
         Put_Line ("MODBUS client connected");
         FC5 (Client, 100, 1, True);
         Put_Line ("FC1 > OK");
         FC15 (Client, 101, (5..15=>True, 16=>False, 17=>True));
         Put_Line ("FC15 > OK");
         Put_Line ("FC1 > " & Image (FC1 (Client'Access, 102, 1, 3)));
         Put_Line ("FC2 > " & Image (FC2 (Client'Access, 103, 10, 20)));
         FC6 (Client, 104, 0, 16#100#);
         Put_Line ("FC6 > OK");
         FC16 (Client, 105, (2=>16#101#,3=>16#102#,4=>16#103#));
         Put_Line
         (  "FC23 > "
         &  Image
            (  FC23
               (  Client'Access,
                  105,
                  0, 4,
                  (5=>16#201#,6=>16#202#,7=>16#203#)
         )  )  );
         Put_Line
         (  "FC24 > "
         &  Image (FC24 (Client'Access, 106, 16#6000#))
         );
         Put_Line
         (  "FC7 > " & Unsigned_8'Image (FC7 (Client'Access, 106))
         );
      end;
   end Test_Synchronous;
begin
--   Test_Asynchronous;
   Test_RTU ("\\.\COM4");
--   Test_Synchronous;
exception
   when Error : others =>
      Put_Line ("Error: " & Exception_Information (Error));
end Test_MODBUS_Client;