apartner.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*=============================================================================|
  2. | PROJECT SNAP7 1.4.0 |
  3. |==============================================================================|
  4. | Copyright (C) 2013, 2014 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. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "snap7.h"
  29. #ifdef OS_WINDOWS
  30. # define WIN32_LEAN_AND_MEAN
  31. # include <windows.h>
  32. #endif
  33. int cnt = 0;
  34. const int size = 256;
  35. byte Buffer[size];
  36. TS7Partner *Partner;
  37. //------------------------------------------------------------------------------
  38. // Usage syntax
  39. //------------------------------------------------------------------------------
  40. static void Usage()
  41. {
  42. printf("Usage\n");
  43. printf(" APartner <PassiveIP>\n");
  44. printf("Where\n");
  45. printf(" <PassiveIP> is the address of the passive partner that we want to connect.\n");
  46. printf("Note\n");
  47. printf("- Local Address is meaningless\n");
  48. printf("- Both Local TSAP and Remote TSAP are set to 0x1002\n");
  49. printf("- You can create multiple active partner in the same\n");
  50. printf(" program or across different programs.\n");
  51. }
  52. //------------------------------------------------------------------------------
  53. // Simply fills the buffer with a progressive number
  54. //------------------------------------------------------------------------------
  55. void PrepareBuffer()
  56. {
  57. cnt++;
  58. for (int i = 0; i < size; i++)
  59. Buffer[i] = cnt;
  60. }
  61. //------------------------------------------------------------------------------
  62. // SysSleep (copied from snap_sysutils.cpp)
  63. //------------------------------------------------------------------------------
  64. void SysSleep(longword Delay_ms)
  65. {
  66. #ifdef OS_WINDOWS
  67. Sleep(Delay_ms);
  68. #else
  69. struct timespec ts;
  70. ts.tv_sec = (time_t)(Delay_ms / 1000);
  71. ts.tv_nsec =(long)((Delay_ms - ts.tv_sec) * 1000000);
  72. nanosleep(&ts, (struct timespec *)0);
  73. #endif
  74. }
  75. //------------------------------------------------------------------------------
  76. // Main
  77. //------------------------------------------------------------------------------
  78. int main(int argc, char* argv[])
  79. {
  80. int SndError = 0;
  81. // Get Progran args
  82. if (argc!=2)
  83. {
  84. Usage();
  85. return 1;
  86. };
  87. // Create the ACTIVE partner
  88. Partner = new TS7Partner(true);
  89. // Start
  90. // Local Address for an active partner is meaningless, leave
  91. // it always set to "0.0.0.0"
  92. int Error=Partner->StartTo("0.0.0.0", argv[1], 0x1002, 0x1002);
  93. if (Error != 0)
  94. {
  95. printf("%s\n",ParErrorText(Error).c_str());
  96. return 1;
  97. }
  98. // Endless loop : Exit with Ctrl-C
  99. while (true)
  100. {
  101. while (!Partner->Linked())
  102. {
  103. printf("Connecting to %s ...\n",argv[1]);
  104. SysSleep(500);
  105. };
  106. do
  107. {
  108. PrepareBuffer();
  109. SndError = Partner->BSend(0x00000001, &Buffer, size);
  110. if (SndError == 0)
  111. printf("Succesfully sent %d bytes\n",size);
  112. else
  113. printf("%s\n",ParErrorText(SndError).c_str());
  114. SysSleep(500);
  115. } while (SndError == 0);
  116. }
  117. }