ppartner.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. | Passive Partner Example |
  28. | |
  29. |=============================================================================*/
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "snap7.h"
  33. #ifdef OS_WINDOWS
  34. # define WIN32_LEAN_AND_MEAN
  35. # include <windows.h>
  36. #endif
  37. TS7Partner *Partner;
  38. byte Buffer[65536]; // 64 K buffer
  39. //------------------------------------------------------------------------------
  40. // Usage syntax
  41. //------------------------------------------------------------------------------
  42. void Usage()
  43. {
  44. printf("Usage\n");
  45. printf(" PPartner <ActiveIP>\n");
  46. printf("Where\n");
  47. printf(" <ActiveIP> is the address of the active partner that we are waiting for.\n");
  48. printf("Note\n");
  49. printf("- Local Address is set to 0.0.0.0 (the default adapter)\n");
  50. printf("- Both Local TSAP and Remote TSAP are set to 0x1002\n");
  51. printf("- You can create multiple passive partners bound to the same\n");
  52. printf(" local address in the same program, but you cannot execute\n");
  53. printf(" multiple instance of this program.\n");
  54. getchar();
  55. }
  56. //------------------------------------------------------------------------------
  57. // SysSleep (copied from snap_sysutils.cpp)
  58. //------------------------------------------------------------------------------
  59. void SysSleep(longword Delay_ms)
  60. {
  61. #ifdef OS_WINDOWS
  62. Sleep(Delay_ms);
  63. #else
  64. struct timespec ts;
  65. ts.tv_sec = (time_t)(Delay_ms / 1000);
  66. ts.tv_nsec =(long)((Delay_ms - ts.tv_sec) * 1000000);
  67. nanosleep(&ts, (struct timespec *)0);
  68. #endif
  69. }
  70. //------------------------------------------------------------------------------
  71. // hexdump, a very nice function, it's not mine.
  72. // I found it on the net somewhere some time ago... thanks to the author ;-)
  73. //------------------------------------------------------------------------------
  74. #ifndef HEXDUMP_COLS
  75. #define HEXDUMP_COLS 16
  76. #endif
  77. void hexdump(void *mem, unsigned int len)
  78. {
  79. unsigned int i, j;
  80. for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++)
  81. {
  82. /* print offset */
  83. if(i % HEXDUMP_COLS == 0)
  84. {
  85. printf("0x%04x: ", i);
  86. }
  87. /* print hex data */
  88. if(i < len)
  89. {
  90. printf("%02x ", 0xFF & ((char*)mem)[i]);
  91. }
  92. else /* end of block, just aligning for ASCII dump */
  93. {
  94. printf(" ");
  95. }
  96. /* print ASCII dump */
  97. if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1))
  98. {
  99. for(j = i - (HEXDUMP_COLS - 1); j <= i; j++)
  100. {
  101. if(j >= len) /* end of block, not really printing */
  102. {
  103. putchar(' ');
  104. }
  105. else if(isprint((((char*)mem)[j] & 0x7F))) /* printable char */
  106. {
  107. putchar(0xFF & ((char*)mem)[j]);
  108. }
  109. else /* other char */
  110. {
  111. putchar('.');
  112. }
  113. }
  114. putchar('\n');
  115. }
  116. }
  117. }
  118. //------------------------------------------------------------------------------
  119. // Callback on data ready
  120. //------------------------------------------------------------------------------
  121. void S7API RecvCallback(void * usrPtr, int opResult, longword R_ID, void *pdata, int Size)
  122. {
  123. printf("R_ID : %d\n",R_ID);
  124. printf("Size : %d\n",Size);
  125. hexdump(pdata, Size);
  126. }
  127. //------------------------------------------------------------------------------
  128. // Main
  129. //------------------------------------------------------------------------------
  130. int main(int argc, char* argv[])
  131. {
  132. // Get Progran args
  133. if (argc != 2)
  134. {
  135. Usage();
  136. return 1;
  137. }
  138. // Create the PASSIVE partner
  139. Partner = new TS7Partner(false);
  140. // Set the BRecv callback
  141. Partner->SetRecvCallback(RecvCallback, NULL);
  142. // Start
  143. int Error=Partner->StartTo("0.0.0.0", argv[1], 0x1002, 0x1002);
  144. if (Error == 0)
  145. printf("Passive partner started\n");
  146. else
  147. printf("%s\n",ParErrorText(Error).c_str());
  148. // If you got a start error:
  149. // Windows - most likely you ar running the server in a pc on wich is
  150. // installed step 7 : open a command prompt and type
  151. // "net stop s7oiehsx" (Win32) or
  152. // "net stop s7oiehsx64" (Win64)
  153. // And after this test :
  154. // "net start s7oiehsx" (Win32) or
  155. // "net start s7oiehsx64" (Win64)
  156. // Unix - you need root rights :-( because the isotcp port (102) is
  157. // low and so it's considered "privileged".
  158. getchar();
  159. Partner->Stop();
  160. delete Partner;
  161. }