srv_resourceless.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. | Resourceless Server example (1.4.0) |
  28. | The Server ha not shared resources. On every Read/Write request a callback |
  29. | is invoked. |
  30. | To the callback are passed the Tag (Area, Start...) and a pointer to a data |
  31. | that you can read/write. |
  32. | |
  33. |=============================================================================*/
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include "snap7.h"
  37. #ifdef OS_WINDOWS
  38. # define WIN32_LEAN_AND_MEAN
  39. # include <windows.h>
  40. #endif
  41. TS7Server *Server;
  42. typedef byte TRWBuffer[1024];
  43. typedef byte *PRWBuffer;
  44. byte cnt = 0;
  45. //------------------------------------------------------------------------------
  46. // hexdump, a very nice function, it's not mine.
  47. // I found it on the net somewhere some time ago... thanks to the author ;-)
  48. //------------------------------------------------------------------------------
  49. #ifndef HEXDUMP_COLS
  50. #define HEXDUMP_COLS 16
  51. #endif
  52. void hexdump(void *mem, unsigned int len)
  53. {
  54. unsigned int i, j;
  55. for (i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++)
  56. {
  57. /* print offset */
  58. if (i % HEXDUMP_COLS == 0)
  59. {
  60. printf("0x%04x: ", i);
  61. }
  62. /* print hex data */
  63. if (i < len)
  64. {
  65. printf("%02x ", 0xFF & ((char*)mem)[i]);
  66. }
  67. else /* end of block, just aligning for ASCII dump */
  68. {
  69. printf(" ");
  70. }
  71. /* print ASCII dump */
  72. if (i % HEXDUMP_COLS == (HEXDUMP_COLS - 1))
  73. {
  74. for (j = i - (HEXDUMP_COLS - 1); j <= i; j++)
  75. {
  76. if (j >= len) /* end of block, not really printing */
  77. {
  78. putchar(' ');
  79. }
  80. else if (isprint((((char*)mem)[j] & 0x7F))) /* printable char */
  81. {
  82. putchar(0xFF & ((char*)mem)[j]);
  83. }
  84. else /* other char */
  85. {
  86. putchar('.');
  87. }
  88. }
  89. putchar('\n');
  90. }
  91. }
  92. }
  93. //------------------------------------------------------------------------------
  94. // Read/Write callback
  95. //------------------------------------------------------------------------------
  96. int S7API RWAreaCallBack(void *usrPtr, int Sender, int Operation, PS7Tag PTag, void *pUsrData)
  97. {
  98. PRWBuffer PBuffer = PRWBuffer(pUsrData);
  99. if (Operation == OperationRead)
  100. printf("Read Request\n");
  101. else
  102. printf("Write Request\n");
  103. switch (PTag->Area)
  104. {
  105. case S7AreaPE: printf("Area : PE, ");
  106. break;
  107. case S7AreaPA: printf("Area : PA, ");
  108. break;
  109. case S7AreaMK: printf("Area : MK, ");
  110. break;
  111. case S7AreaCT: printf("Area : CT, ");
  112. break;
  113. case S7AreaTM: printf("Area : TM, ");
  114. break;
  115. case S7AreaDB: printf("Area : DB%d, ", PTag->DBNumber);
  116. break;
  117. default: printf("Unknown area %d, ", PTag->Area);
  118. }
  119. printf("Start : %d, ", PTag->Start);
  120. printf("Size : %d\n", PTag->Size);
  121. if (Operation == OperationWrite)
  122. hexdump(pUsrData, PTag->Size);
  123. else
  124. {
  125. for (int c = 0; c < 1024; c++)
  126. PBuffer[c] = cnt;
  127. cnt++;
  128. }
  129. printf("\n");
  130. return 0;
  131. };
  132. // Here we use the callback to show the log, this is not the best choice since
  133. // the callback is synchronous with the client access, i.e. the server cannot
  134. // handle futher request from that client until the callback is complete.
  135. // The right choice is to use the log queue via the method PickEvent.
  136. void S7API EventCallBack(void *usrPtr, PSrvEvent PEvent, int Size)
  137. {
  138. // print the event
  139. printf("%s\n", SrvEventText(PEvent).c_str());
  140. };
  141. int main(int argc, char* argv[])
  142. {
  143. int Error;
  144. Server = new TS7Server;
  145. // Filter a bit of noise
  146. Server->SetEventsMask(0x3ff);
  147. // Set the Read/Write callback
  148. Server->SetRWAreaCallback(RWAreaCallBack, NULL);
  149. // Set the event callback to show something : it's not strictly needed.
  150. // If you comment next line the server still works fine.
  151. Server->SetEventsCallback(EventCallBack, NULL);
  152. // Start the server onto the default adapter.
  153. // To select an adapter we have to use Server->StartTo("192.168.x.y").
  154. // Start() is the same of StartTo("0.0.0.0");
  155. Error = Server->Start();
  156. if (Error == 0)
  157. {
  158. // Now the server is running ... wait a key to terminate
  159. getchar();
  160. }
  161. else
  162. printf("%s\n", SrvErrorText(Error).c_str());
  163. // If you got a start error:
  164. // Windows - most likely you ar running the server in a pc on wich is
  165. // installed step 7 : open a command prompt and type
  166. // "net stop s7oiehsx" (Win32) or
  167. // "net stop s7oiehsx64" (Win64)
  168. // And after this test :
  169. // "net start s7oiehsx" (Win32) or
  170. // "net start s7oiehsx64" (Win64)
  171. // Unix - you need root rights :-( because the isotcp port (102) is
  172. // low and so it's considered "privileged".
  173. Server->Stop(); // <- not strictly needed, every server is stopped on deletion
  174. // and every client is gracefully disconnected.
  175. delete Server;
  176. }
  177. // Finally, this is a very minimalist (but working) server :
  178. /*
  179. int main(int argc, char* argv[])
  180. {
  181. TS7Server *Server = new TS7Server;
  182. Server->Start();
  183. getchar();
  184. delete Server;
  185. }
  186. */