ppartner.c 6.6 KB

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