S7TestServer.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using Snap7;
  3. namespace S7.Net.UnitTest.Helpers
  4. {
  5. class S7TestServer
  6. {
  7. static S7Server Server;
  8. static private byte[] DB1 = new byte[1024]; // Our DB1
  9. static private byte[] DB2 = new byte[64000]; // Our DB2
  10. static private byte[] DB3 = new byte[1024]; // Our DB3
  11. static private byte[] DB4 = new byte[6] { 3, 128, 1, 0, 197, 104 }; // Our DB4
  12. private static S7Server.TSrvCallback TheEventCallBack; // <== Static var containig the callback
  13. private static S7Server.TSrvCallback TheReadCallBack; // <== Static var containig the callback
  14. // Here we use the callback to show the log, this is not the best choice since
  15. // the callback is synchronous with the client access, i.e. the server cannot
  16. // handle futher request from that client until the callback is complete.
  17. // The right choice is to use the log queue via the method PickEvent.
  18. static void EventCallback(IntPtr usrPtr, ref S7Server.USrvEvent Event, int Size)
  19. {
  20. Console.WriteLine(Server.EventText(ref Event));
  21. }
  22. static void ReadEventCallback(IntPtr usrPtr, ref S7Server.USrvEvent Event, int Size)
  23. {
  24. Console.WriteLine(Server.EventText(ref Event));
  25. }
  26. public static void Start(short port)
  27. {
  28. Server = new S7Server();
  29. // Share some resources with our virtual PLC
  30. Server.RegisterArea(S7Server.srvAreaDB, // We are registering a DB
  31. 1, // Its number is 1 (DB1)
  32. DB1, // Our buffer for DB1
  33. DB1.Length); // Its size
  34. // Do the same for DB2, DB3, and DB4
  35. Server.RegisterArea(S7Server.srvAreaDB, 2, DB2, DB2.Length);
  36. Server.RegisterArea(S7Server.srvAreaDB, 3, DB3, DB3.Length);
  37. Server.RegisterArea(S7Server.srvAreaDB, 4, DB4, DB4.Length);
  38. // Exclude read event to avoid the double report
  39. // Set the callbacks (using the static var to avoid the garbage collect)
  40. TheEventCallBack = new S7Server.TSrvCallback(EventCallback);
  41. TheReadCallBack = new S7Server.TSrvCallback(ReadEventCallback);
  42. Server.EventMask = ~S7Server.evcDataRead;
  43. Server.SetEventsCallBack(TheEventCallBack, IntPtr.Zero);
  44. Server.SetReadEventsCallBack(TheReadCallBack, IntPtr.Zero);
  45. // Uncomment next line if you don't want to see wrapped messages
  46. // (Note : Doesn't work in Mono 2.10)
  47. // Console.SetBufferSize(100, Int16.MaxValue - 1);
  48. // Start the server onto the default adapter.
  49. // To select an adapter we have to use Server->StartTo("192.168.x.y").
  50. // Start() is the same of StartTo("0.0.0.0");
  51. Server.SetParam(S7Consts.p_u16_LocalPort, ref port);
  52. int Error = Server.Start();
  53. if (Error != 0)
  54. {
  55. throw new Exception($"Error starting Snap7 server: {Server.ErrorText(Error)}");
  56. }
  57. //if (Error == 0)
  58. //{
  59. // // Now the server is running ... wait a key to terminate
  60. // //Console.ReadKey();
  61. // Server.Stop();
  62. //}
  63. //else
  64. // Console.WriteLine(Server.ErrorText(Error));
  65. // If you got a start error:
  66. // Windows - most likely you ar running the server in a pc on wich is
  67. // installed step 7 : open a command prompt and type
  68. // "net stop s7oiehsx" (Win32) or
  69. // "net stop s7oiehsx64" (Win64)
  70. // And after this test :
  71. // "net start s7oiehsx" (Win32) or
  72. // "net start s7oiehsx64" (Win64)
  73. // Unix - you need root rights :-( because the isotcp port (102) is
  74. // low and so it's considered "privileged".
  75. }
  76. public static void Stop()
  77. {
  78. int Error = Server.Stop();
  79. }
  80. }
  81. }