server.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. | New Server Example (1.1.0) |
  28. | Here we set ReadEventCallback to get in advance which area the client needs |
  29. | then we fill this area with a counter. |
  30. | The purpose is to show how to modify an area before it be trasferred to the |
  31. | client |
  32. | |
  33. |=============================================================================*/
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <cstring>
  37. #include "snap7.h"
  38. TS7Server *Server;
  39. unsigned char DB21[512]; // Our DB1
  40. unsigned char DB103[1280]; // Our DB2
  41. unsigned char DB3[1024]; // Our DB3
  42. byte cnt = 0;
  43. // Here we use the callback to show the log, this is not the best choice since
  44. // the callback is synchronous with the client access, i.e. the server cannot
  45. // handle futher request from that client until the callback is complete.
  46. // The right choice is to use the log queue via the method PickEvent.
  47. void S7API EventCallBack(void *usrPtr, PSrvEvent PEvent, int Size)
  48. {
  49. // print the event
  50. printf("%s\n",SrvEventText(PEvent).c_str());
  51. };
  52. // The read event callback is called multiple times in presence of multiread var request
  53. void S7API ReadEventCallBack(void *usrPtr, PSrvEvent PEvent, int Size)
  54. {
  55. // print the read event
  56. printf("%s\n",SrvEventText(PEvent).c_str());
  57. if (PEvent->EvtParam1==S7AreaDB)
  58. {
  59. // As example the DB requested is filled before transferred
  60. // EvtParam1 contains the DB number.
  61. switch (PEvent->EvtParam2)
  62. {
  63. case 1 : memset(&DB21, ++cnt, sizeof(DB21));break;
  64. case 2 : memset(&DB103, ++cnt, sizeof(DB103));break;
  65. case 3 : memset(&DB3, ++cnt, sizeof(DB3));break;
  66. }
  67. }
  68. };
  69. int main(int argc, char* argv[])
  70. {
  71. int Error;
  72. Server = new TS7Server;
  73. // Share some resources with our virtual PLC
  74. Server->RegisterArea(srvAreaDB, // We are registering a DB
  75. 21, // Its number is 1 (DB1)
  76. &DB21, // Our buffer for DB1
  77. sizeof(DB21)); // Its size
  78. // Do the same for DB2 and DB3
  79. Server->RegisterArea(srvAreaDB, 103, &DB103, sizeof(DB103));
  80. Server->RegisterArea(srvAreaDB, 3, &DB3, sizeof(DB3));
  81. // We mask the read event to avoid the double trigger for the same event
  82. Server->SetEventsMask(~evcDataRead);
  83. Server->SetEventsCallback(EventCallBack, NULL);
  84. // Set the Read Callback
  85. Server->SetReadEventsCallback(ReadEventCallBack, NULL);
  86. // Start the server onto the default adapter.
  87. // To select an adapter we have to use Server->StartTo("192.168.x.y").
  88. // Start() is the same of StartTo("0.0.0.0");
  89. Error=Server->Start();
  90. if (Error==0)
  91. {
  92. // Now the server is running ... wait a key to terminate
  93. getchar();
  94. }
  95. else
  96. printf("%s\n",SrvErrorText(Error).c_str());
  97. // If you got a start error:
  98. // Windows - most likely you ar running the server in a pc on wich is
  99. // installed step 7 : open a command prompt and type
  100. // "net stop s7oiehsx" (Win32) or
  101. // "net stop s7oiehsx64" (Win64)
  102. // And after this test :
  103. // "net start s7oiehsx" (Win32) or
  104. // "net start s7oiehsx64" (Win64)
  105. // Unix - you need root rights :-( because the isotcp port (102) is
  106. // low and so it's considered "privileged".
  107. Server->Stop(); // <- not strictly needed, every server is stopped on deletion
  108. // and every client is gracefully disconnected.
  109. delete Server;
  110. }
  111. // Finally, this is a very minimalist (but working) server :
  112. /*
  113. int main(int argc, char* argv[])
  114. {
  115. TS7Server *Server = new TS7Server;
  116. Server->Start();
  117. getchar();
  118. delete Server;
  119. }
  120. */