srv_resourceless.c 7.2 KB

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