apartner.c 5.6 KB

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