Esempio n. 1
0
        public async Task ClientReconnectTest()
        {
            using (var server = new MiniTestServer())
            {
                server.Start();
                using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
                {
                    await client.Connect();
                    await EnsureWait();

                    Assert.IsTrue(client.IsConnected);

                    await server.Stop();

                    await client.ReadHoldingRegisters(0, 0, 1);
                    await EnsureWait();

                    Assert.IsFalse(client.IsConnected);

                    server.Start();
                    await client.ConnectingTask;
                    await EnsureWait();

                    Assert.IsTrue(client.IsConnected);
                }
            }
        }
Esempio n. 2
0
        public async Task ClientEventsTest()
        {
            int connectEvents    = 0;
            int disconnectEvents = 0;

            using var server = new MiniTestServer();
            server.Start();

            using (var client = new ModbusClient(IPAddress.Loopback, server.Port, new ConsoleLogger()))
            {
                client.Connected += (sender, args) =>
                {
                    connectEvents++;
                };
                client.Disconnected += (sender, args) =>
                {
                    disconnectEvents++;
                };

                Assert.AreEqual(0, connectEvents, "No connet events");
                Assert.AreEqual(0, disconnectEvents, "No disconnect events");

                await client.Connect();

                Assert.IsTrue(client.IsConnected, "Client should be connected");

                await EnsureWait();                 // get events raised

                Assert.AreEqual(1, connectEvents, "One connect event");
                Assert.AreEqual(0, disconnectEvents, "No disconnect events");

                await server.Stop();

                await EnsureWait();                  // time to set all information

                Assert.IsFalse(client.IsConnected, "Client should not be connected");

                await EnsureWait();                 // get events raised

                Assert.AreEqual(1, connectEvents, "One connect event");
                Assert.AreEqual(1, disconnectEvents, "One disconnect event");

                server.Start();
                await client.ConnectingTask;
                Assert.IsTrue(client.IsConnected, "Client should be connected");

                await EnsureWait();                 // get events raised

                Assert.AreEqual(2, connectEvents, "Two connect events");
            }

            await EnsureWait();             // get events raised

            Assert.AreEqual(2, disconnectEvents, "Two disconnect events");
        }
Esempio n. 3
0
        public async Task ClientEventsTest()
        {
            int connectEvents    = 0;
            int disconnectEvents = 0;

            using (var server = new MiniTestServer())
            {
                server.Start();
                using (var client = new ModbusClient(IPAddress.Loopback, server.Port, new ConsoleLogger()))
                {
                    client.Connected += (sender, args) =>
                    {
                        connectEvents++;
                    };
                    client.Disconnected += (sender, args) =>
                    {
                        disconnectEvents++;
                    };

                    Assert.AreEqual(0, connectEvents);
                    Assert.AreEqual(0, disconnectEvents);

                    await client.Connect();

                    Assert.IsTrue(client.IsConnected);

                    await EnsureWait();                     // get events raised

                    Assert.AreEqual(1, connectEvents);
                    Assert.AreEqual(0, disconnectEvents);

                    await server.Stop();

                    await client.ReadHoldingRegisters(0, 0, 1);
                    await EnsureWait();                      // time to set all information

                    Assert.IsFalse(client.IsConnected);

                    await EnsureWait();                     // get events raised

                    Assert.AreEqual(1, connectEvents);
                    Assert.AreEqual(1, disconnectEvents);

                    server.Start();
                    await client.ConnectingTask;
                    Assert.IsTrue(client.IsConnected);
                }

                await EnsureWait();                 // get events raised

                Assert.AreEqual(2, connectEvents);
                Assert.AreEqual(2, disconnectEvents);
            }
        }
Esempio n. 4
0
        public async Task ClientReconnectTest()
        {
            using var server = new MiniTestServer();
            server.Start();

            using var client = new ModbusClient(IPAddress.Loopback, server.Port, new ConsoleLogger());
            await client.Connect();

            Assert.IsTrue(client.IsConnected, "Client should be connected");

            await server.Stop();

            await EnsureWait();              // time to start reconnect

            Assert.IsFalse(client.IsConnected, "Client should not be connected");

            server.Start();
            await client.ConnectingTask;

            Assert.IsTrue(client.IsConnected, "Client should be connected again");
        }