server.dpr 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. (*=============================================================================|
  2. | PROJECT SNAP7 1.4.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. program server;
  31. {$APPTYPE CONSOLE}
  32. uses Snap7;
  33. Var
  34. Srv : TS7Server;
  35. DB1 : packed array[0..511] of byte; // Our DB1
  36. DB2 : packed array[0..511] of byte; // Our DB1
  37. DB3 : packed array[0..511] of byte; // Our DB1
  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 (see
  42. // serverdemo)
  43. procedure EventCallBack(usrPtr : pointer; PEvent : PSrvEvent ; Size : integer);
  44. {$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF}
  45. begin
  46. // print the event
  47. writeln(SrvEventText(PEvent^));
  48. end;
  49. Var
  50. Error : integer;
  51. begin
  52. Srv:=TS7Server.Create;
  53. // Share some resources with our virtual PLC
  54. Srv.RegisterArea(srvAreaDB, // We are registering a DB
  55. 1, // Its number is 1 (DB1)
  56. @DB1, // Our buffer for DB1
  57. sizeof(DB1)); // Its size
  58. // Do the same for DB2 and DB3
  59. Srv.RegisterArea(srvAreaDB, 2, @DB2, sizeof(DB2));
  60. Srv.RegisterArea(srvAreaDB, 3, @DB3, sizeof(DB3));
  61. // Set the event callback to show something : it's not strictly needed.
  62. // If you comment next line the server still works fine.
  63. Srv.SetEventsCallback(@EventCallBack, nil);
  64. // Start the server onto the default adapter.
  65. // To select an adapter we have to use Server->StartTo("192.168.x.y").
  66. // Start() is the same of StartTo("0.0.0.0");
  67. Error:=Srv.Start();
  68. if (Error=0) then
  69. begin
  70. // Now the server is running ... wait a key to terminate
  71. readln;
  72. end
  73. else
  74. writeln(SrvErrorText(Error));
  75. // If you got a start error:
  76. // Windows - most likely you ar running the server in a pc on wich is
  77. // installed step 7 : open a command prompt and type
  78. // "net stop s7oiehsx" (Win32) or
  79. // "net stop s7oiehsx64" (Win64)
  80. // And after this test :
  81. // "net start s7oiehsx" (Win32) or
  82. // "net start s7oiehsx64" (Win64)
  83. // Unix - you need root rights :-( because the isotcp port (102) is
  84. // low and so it's considered "privileged".
  85. Srv.Stop(); // <- not strictly needed, every server is stopped on deletion
  86. // and every client is gracefully disconnected.
  87. Srv.Free;
  88. // Finally, this is a very minimalist (but working) server :
  89. (*
  90. Var
  91. Srv : TS7Server;
  92. begin
  93. Srv:=TS7Server.Create;
  94. Srv.Start;
  95. Readln;
  96. Srv.Free;
  97. end.
  98. *)
  99. end.