1 /*=============================================================================|
2 |  PROJECT SNAP7                                                         1.0.0 |
3 |==============================================================================|
4 |  Copyright (C) 2013, Davide Nardella                                         |
5 |  All rights reserved.                                                        |
6 |==============================================================================|
7 |  SNAP7 is free software: you can redistribute it and/or modify               |
8 |  it under the terms of the Lesser GNU General Public License as published by |
9 |  the Free Software Foundation, either version 3 of the License, or           |
10 |  (at your option) any later version.                                         |
11 |                                                                              |
12 |  It means that you can distribute your commercial software linked with       |
13 |  SNAP7 without the requirement to distribute the source code of your         |
14 |  application and without the requirement that your application be itself     |
15 |  distributed under LGPL.                                                     |
16 |                                                                              |
17 |  SNAP7 is distributed in the hope that it will be useful,                    |
18 |  but WITHOUT ANY WARRANTY; without even the implied warranty of              |
19 |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
20 |  Lesser GNU General Public License for more details.                         |
21 |                                                                              |
22 |  You should have received a copy of the GNU General Public License and a     |
23 |  copy of Lesser GNU General Public License along with Snap7.                 |
24 |  If not, see  http://www.gnu.org/licenses/                                   |
25 |==============================================================================|
26 |                                                                              |
27 |  Active Partner Example                                                      |
28 |                                                                              |
29 |=============================================================================*/
30 using System;
31 using System.Text;
32 using Snap7;
33 
34 class ActivePartnerDemo
35 {
36     const int size = 256;
37     static S7Partner Partner;
38     static byte[] Buffer = new byte[size];
39     static byte cnt = 0;
40     //------------------------------------------------------------------------------
41     // Usage syntax
42     //------------------------------------------------------------------------------
Usage()43     static void Usage()
44     {
45         Console.WriteLine("Usage");
46         Console.WriteLine("  APartner <PassiveIP>");
47         Console.WriteLine("Where");
48         Console.WriteLine("  <PassiveIP> is the address of the passive partner that we want to connect.");
49         Console.WriteLine("Note");
50         Console.WriteLine("- Local Address is meaningless");
51         Console.WriteLine("- Both Local TSAP and Remote TSAP are set to 0x1002");
52         Console.WriteLine("- You can create multiple active partner in the same");
53         Console.WriteLine("  program or across different programs.");
54         Console.ReadKey();
55     }
56     //------------------------------------------------------------------------------
57     // Simply fills the buffer with a progressive number
58     //------------------------------------------------------------------------------
PrepareBuffer()59     static void PrepareBuffer()
60     {
61         cnt++;
62         for (int i = 0; i < size; i++)
63             Buffer[i] = cnt;
64     }
65     //------------------------------------------------------------------------------
66     // Main
67     //------------------------------------------------------------------------------
Main(string[] args)68     static void Main(string[] args)
69     {
70         int SndError = 0;
71         // Get Progran args
72         if (args.Length != 1)
73         {
74             Usage();
75             return;
76         }
77         // Create the ACTIVE partner
78         Partner = new S7Partner(1);
79         // Start
80         // Local Address for an active partner is meaningless, leave
81         // it always set to "0.0.0.0"
82         int Error=Partner.StartTo("0.0.0.0", args[0], 0x1002, 0x1002);
83         if (Error != 0)
84         {
85             Console.WriteLine(Partner.ErrorText(Error));
86             return;
87         }
88         // Endless loop : Exit with Ctrl-C
89         while (true)
90         {
91             while (!Partner.Linked)
92             {
93                 Console.WriteLine("Connecting to " + args[0] + "...");
94                 System.Threading.Thread.Sleep(500);
95             };
96             do
97             {
98                 PrepareBuffer();
99                 SndError = Partner.BSend(0x00000001, Buffer, size);
100                 if (SndError == 0)
101                     Console.WriteLine("Succesfully sent "+size.ToString()+" bytes");
102                 else
103                     Console.WriteLine(Partner.ErrorText(SndError));
104                 System.Threading.Thread.Sleep(300);
105             } while (SndError == 0);
106         }
107     }
108 }
109