diff --git a/README.md b/README.md index a6e990a..81ce33b 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,5 @@ PM> Install-Package S7netplus ## Running the tests -Unit tests use Snap7 server, so port 102 must be not in use. -If you have Siemens Step7 installed, the service s7oiehsx64 is stopped when running unit tests. -You have to restart the service manually if you need it. +Unit tests use Snap7 server. +On Windows, the DLL is included with the test project. On other platforms, Snap7 must be installed manually before running tests. diff --git a/S7.Net.UnitTest/Helpers/S7TestServer.cs b/S7.Net.UnitTest/Helpers/S7TestServer.cs index 8db9328..401587f 100644 --- a/S7.Net.UnitTest/Helpers/S7TestServer.cs +++ b/S7.Net.UnitTest/Helpers/S7TestServer.cs @@ -29,7 +29,7 @@ namespace S7.Net.UnitTest.Helpers Console.WriteLine(Server.EventText(ref Event)); } - public static void Start() + public static void Start(short port) { Server = new S7Server(); // Share some resources with our virtual PLC @@ -59,7 +59,14 @@ namespace S7.Net.UnitTest.Helpers // Start the server onto the default adapter. // To select an adapter we have to use Server->StartTo("192.168.x.y"). // Start() is the same of StartTo("0.0.0.0"); + + Server.SetParam(S7Consts.p_u16_LocalPort, ref port); + int Error = Server.Start(); + if (Error != 0) + { + throw new Exception($"Error starting Snap7 server: {Server.ErrorText(Error)}"); + } //if (Error == 0) //{ // // Now the server is running ... wait a key to terminate diff --git a/S7.Net.UnitTest/ProtocolTests.cs b/S7.Net.UnitTest/ProtocolTests.cs index 4d53f17..b18ebe8 100644 --- a/S7.Net.UnitTest/ProtocolTests.cs +++ b/S7.Net.UnitTest/ProtocolTests.cs @@ -13,7 +13,7 @@ namespace S7.Net.UnitTest [TestClass] public class ProtocolUnitTest { - private TestContext TestContext { get; set; } + public TestContext TestContext { get; set; } [TestMethod] public async Task TPKT_Read() diff --git a/S7.Net.UnitTest/S7.Net.UnitTest.csproj b/S7.Net.UnitTest/S7.Net.UnitTest.csproj index 70f0e07..e836b62 100644 --- a/S7.Net.UnitTest/S7.Net.UnitTest.csproj +++ b/S7.Net.UnitTest/S7.Net.UnitTest.csproj @@ -1,15 +1,17 @@  - net452 + net452;netcoreapp3.1 true Properties\S7.Net.snk false Copyright © 2014 + x64 - + + @@ -24,13 +26,8 @@ - - - - - + PreserveNewest - diff --git a/S7.Net.UnitTest/S7NetTestsAsync.cs b/S7.Net.UnitTest/S7NetTestsAsync.cs index 604e2bb..f90f250 100644 --- a/S7.Net.UnitTest/S7NetTestsAsync.cs +++ b/S7.Net.UnitTest/S7NetTestsAsync.cs @@ -1,12 +1,8 @@ #region Using using System; using System.Collections.Generic; -using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; -using S7.Net; using S7.Net.UnitTest.Helpers; -using S7.Net.UnitTest; -using System.ServiceProcess; using S7.Net.Types; using S7.UnitTest.Helpers; using System.Threading.Tasks; diff --git a/S7.Net.UnitTest/S7NetTestsSync.cs b/S7.Net.UnitTest/S7NetTestsSync.cs index fd63448..162c5a2 100644 --- a/S7.Net.UnitTest/S7NetTestsSync.cs +++ b/S7.Net.UnitTest/S7NetTestsSync.cs @@ -1,12 +1,8 @@ #region Using using System; using System.Collections.Generic; -using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; -using S7.Net; using S7.Net.UnitTest.Helpers; -using S7.Net.UnitTest; -using System.ServiceProcess; using S7.Net.Types; using S7.UnitTest.Helpers; @@ -41,6 +37,8 @@ namespace S7.Net.UnitTest #region Constants const int DB2 = 2; const int DB4 = 4; + const short TestServerPort = 31122; + const string TestServerIp = "127.0.0.1"; #endregion #region Private fields @@ -53,16 +51,19 @@ namespace S7.Net.UnitTest /// public S7NetTests() { - plc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2); - //ConsoleManager.Show(); - ShutDownServiceS7oiehsx64(); + plc = CreatePlc(); } + private static Plc CreatePlc() + { + return new Plc(CpuType.S7300, TestServerIp, TestServerPort, 0, 2); + } + [TestInitialize] public void Setup() { - S7TestServer.Start(); + S7TestServer.Start(TestServerPort); plc.Open(); } @@ -931,9 +932,9 @@ namespace S7.Net.UnitTest { plc.Close(); S7TestServer.Stop(); - S7TestServer.Start(); + S7TestServer.Start(TestServerPort); - var reachablePlc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2); + var reachablePlc = CreatePlc(); Assert.IsTrue(reachablePlc.IsAvailable); } @@ -1026,18 +1027,6 @@ namespace S7.Net.UnitTest #endregion #region Private methods - private static void ShutDownServiceS7oiehsx64() - { - ServiceController[] services = ServiceController.GetServices(); - var service = services.FirstOrDefault(s => s.ServiceName == "s7oiehsx64"); - if (service != null) - { - if (service.Status == ServiceControllerStatus.Running) - { - service.Stop(); - } - } - } #region IDisposable Support private bool disposedValue = false; // To detect redundant calls diff --git a/S7.Net.UnitTest/Snap7/snap7.net.cs b/S7.Net.UnitTest/Snap7/snap7.net.cs index 98efa75..b6ae831 100644 --- a/S7.Net.UnitTest/Snap7/snap7.net.cs +++ b/S7.Net.UnitTest/Snap7/snap7.net.cs @@ -36,11 +36,8 @@ namespace Snap7 public class S7Consts { - #if __MonoCS__ // Assuming that we are using Unix release of Mono (otherwise modify it) - public const string Snap7LibName = "libsnap7.so"; - #else - public const string Snap7LibName = "snap7.dll"; - #endif + public const string Snap7LibName = "snap7"; + //------------------------------------------------------------------------------ // PARAMS LIST //------------------------------------------------------------------------------ diff --git a/S7.Net.UnitTest/StreamTests.cs b/S7.Net.UnitTest/StreamTests.cs index 375c02c..9cb0969 100644 --- a/S7.Net.UnitTest/StreamTests.cs +++ b/S7.Net.UnitTest/StreamTests.cs @@ -65,7 +65,7 @@ namespace S7.Net.UnitTest [TestClass] public class StreamTests { - private TestContext TestContext { get; set; } + public TestContext TestContext { get; set; } [TestMethod] public async Task TPKT_ReadRestrictedStreamAsync() diff --git a/S7.Net.UnitTest/runtimes/win-x64/native/snap7.dll b/S7.Net.UnitTest/runtimes/win-x64/native/snap7.dll new file mode 100644 index 0000000..e1baafc Binary files /dev/null and b/S7.Net.UnitTest/runtimes/win-x64/native/snap7.dll differ diff --git a/S7.Net.UnitTest/snap7.dll b/S7.Net.UnitTest/snap7.dll deleted file mode 100644 index d52c361..0000000 Binary files a/S7.Net.UnitTest/snap7.dll and /dev/null differ diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs index e4a7f7f..96f2474 100644 --- a/S7.Net/PLC.cs +++ b/S7.Net/PLC.cs @@ -18,8 +18,8 @@ namespace S7.Net private TcpClient? tcpClient; private NetworkStream? _stream; - private int readTimeout = System.Threading.Timeout.Infinite; - private int writeTimeout = System.Threading.Timeout.Infinite; + private int readTimeout = 0; // default no timeout + private int writeTimeout = 0; // default no timeout /// /// IP address of the PLC