server.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. 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. char text[1024];
  46. Srv_EventText(PEvent, text, 1024);
  47. printf("%s\n",text);
  48. };
  49. int main(int argc, char* argv[])
  50. {
  51. int Error;
  52. char text[1024];
  53. Server = Srv_Create();
  54. // Share some resources with our virtual PLC
  55. Srv_RegisterArea(Server,
  56. srvAreaDB, // We are registering a DB
  57. 1, // Its number is 1 (DB1)
  58. &DB1, // Our buffer for DB1
  59. sizeof(DB1)); // Its size
  60. // Do the same for DB2 and DB3
  61. Srv_RegisterArea(Server, srvAreaDB, 2, &DB2, sizeof(DB2));
  62. Srv_RegisterArea(Server, srvAreaDB, 3, &DB3, sizeof(DB3));
  63. // Set the event callback to show something : it's not strictly needed.
  64. // If you comment next line the server still works fine.
  65. Srv_SetEventsCallback(Server, EventCallBack, NULL);
  66. // Start the server onto the default adapter.
  67. // To select an adapter we have to use Srv_StartTo(Server, "192.168.x.y").
  68. // Start() is the same of StartTo("0.0.0.0");
  69. Error=Srv_Start(Server);
  70. if (Error==0)
  71. {
  72. // Now the server is running ... wait a key to terminate
  73. getchar();
  74. }
  75. else
  76. {
  77. Srv_ErrorText(Error, text, 1024);
  78. printf("%s\n", text);
  79. }
  80. // If you got a start error:
  81. // Windows - most likely you ar running the server in a pc on wich is
  82. // installed step 7 : open a command prompt and type
  83. // "net stop s7oiehsx" (Win32) or
  84. // "net stop s7oiehsx64" (Win64)
  85. // And after this test :
  86. // "net start s7oiehsx" (Win32) or
  87. // "net start s7oiehsx64" (Win64)
  88. // Unix - you need root rights :-( because the isotcp port (102) is
  89. // low and so it's considered "privileged".
  90. Srv_Stop(Server); // <- not strictly needed, every server is stopped on deletion
  91. // and every client is gracefully disconnected.
  92. Srv_Destroy(&Server);
  93. }
  94. // Finally, this is a very minimalist (but working) server :
  95. /*
  96. int main(int argc, char* argv[])
  97. {
  98. TS7Server *Server = new TS7Server;
  99. Server->Start();
  100. getchar();
  101. delete Server;
  102. }
  103. */