APartner.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. class ActivePartnerDemo
  34. {
  35. const int size = 256;
  36. static S7Partner Partner;
  37. static byte[] Buffer = new byte[size];
  38. static byte cnt = 0;
  39. //------------------------------------------------------------------------------
  40. // Usage syntax
  41. //------------------------------------------------------------------------------
  42. static void Usage()
  43. {
  44. Console.WriteLine("Usage");
  45. Console.WriteLine(" APartner <PassiveIP>");
  46. Console.WriteLine("Where");
  47. Console.WriteLine(" <PassiveIP> is the address of the passive partner that we want to connect.");
  48. Console.WriteLine("Note");
  49. Console.WriteLine("- Local Address is meaningless");
  50. Console.WriteLine("- Both Local TSAP and Remote TSAP are set to 0x1002");
  51. Console.WriteLine("- You can create multiple active partner in the same");
  52. Console.WriteLine(" program or across different programs.");
  53. Console.ReadKey();
  54. }
  55. //------------------------------------------------------------------------------
  56. // Simply fills the buffer with a progressive number
  57. //------------------------------------------------------------------------------
  58. static void PrepareBuffer()
  59. {
  60. cnt++;
  61. for (int i = 0; i < size; i++)
  62. Buffer[i] = cnt;
  63. }
  64. //------------------------------------------------------------------------------
  65. // Main
  66. //------------------------------------------------------------------------------
  67. static void Main(string[] args)
  68. {
  69. int SndError = 0;
  70. // Get Progran args
  71. if (args.Length != 1)
  72. {
  73. Usage();
  74. return;
  75. }
  76. // Create the ACTIVE partner
  77. Partner = new S7Partner(1);
  78. // Start
  79. // Local Address for an active partner is meaningless, leave
  80. // it always set to "0.0.0.0"
  81. int Error=Partner.StartTo("0.0.0.0", args[0], 0x1002, 0x1002);
  82. if (Error != 0)
  83. {
  84. Console.WriteLine(Partner.ErrorText(Error));
  85. return;
  86. }
  87. // Endless loop : Exit with Ctrl-C
  88. while (true)
  89. {
  90. while (!Partner.Linked)
  91. {
  92. Console.WriteLine("Connecting to " + args[0] + "...");
  93. System.Threading.Thread.Sleep(500);
  94. };
  95. do
  96. {
  97. PrepareBuffer();
  98. SndError = Partner.BSend(0x00000001, Buffer, size);
  99. if (SndError == 0)
  100. Console.WriteLine("Succesfully sent "+size.ToString()+" bytes");
  101. else
  102. Console.WriteLine(Partner.ErrorText(SndError));
  103. System.Threading.Thread.Sleep(300);
  104. } while (SndError == 0);
  105. }
  106. }
  107. }