server_v10.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*=============================================================================|
  2. | PROJECT SNAP7 1.0.0 |
  3. |==============================================================================|
  4. | Copyright (C) 2013, 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 <cstring>
  33. #include "snap7.h"
  34. TS7Server *Server;
  35. unsigned char DB1[512]; // Our DB1
  36. unsigned char DB2[128]; // Our DB2
  37. unsigned char DB3[1024]; // Our DB3
  38. // Here we use the callback to show the log, this is not the best choice since
  39. // the callback is synchronous with the client access, i.e. the server cannot
  40. // handle futher request from that client until the callback is complete.
  41. // The right choice is to use the log queue via the method PickEvent.
  42. void S7API EventCallBack(void *usrPtr, PSrvEvent PEvent, int Size)
  43. {
  44. // print the event
  45. printf("%s\n",SrvEventText(PEvent).c_str());
  46. };
  47. int main(int argc, char* argv[])
  48. {
  49. int Error;
  50. Server = new TS7Server;
  51. // Share some resources with our virtual PLC
  52. Server->RegisterArea(srvAreaDB, // We are registering a DB
  53. 1, // Its number is 1 (DB1)
  54. &DB1, // Our buffer for DB1
  55. sizeof(DB1)); // Its size
  56. // Do the same for DB2 and DB3
  57. Server->RegisterArea(srvAreaDB, 2, &DB2, sizeof(DB2));
  58. Server->RegisterArea(srvAreaDB, 3, &DB3, sizeof(DB3));
  59. // Set the event callback to show something : it's not strictly needed.
  60. // If you comment next line the server still works fine.
  61. Server->SetEventsCallback(EventCallBack, NULL);
  62. // Start the server onto the default adapter.
  63. // To select an adapter we have to use Server->StartTo("192.168.x.y").
  64. // Start() is the same of StartTo("0.0.0.0");
  65. Error=Server->Start();
  66. if (Error==0)
  67. {
  68. // Now the server is running ... wait a key to terminate
  69. getchar();
  70. }
  71. else
  72. printf("%s\n",SrvErrorText(Error).c_str());
  73. // If you got a start error:
  74. // Windows - most likely you ar running the server in a pc on wich is
  75. // installed step 7 : open a command prompt and type
  76. // "net stop s7oiehsx" (Win32) or
  77. // "net stop s7oiehsx64" (Win64)
  78. // And after this test :
  79. // "net start s7oiehsx" (Win32) or
  80. // "net start s7oiehsx64" (Win64)
  81. // Unix - you need root rights :-( because the isotcp port (102) is
  82. // low and so it's considered "privileged".
  83. Server->Stop(); // <- not strictly needed, every server is stopped on deletion
  84. // and every client is gracefully disconnected.
  85. delete Server;
  86. }
  87. // Finally, this is a very minimalist (but working) server :
  88. /*
  89. int main(int argc, char* argv[])
  90. {
  91. TS7Server *Server = new TS7Server;
  92. Server->Start();
  93. getchar();
  94. delete Server;
  95. }
  96. */