コード例 #1
0
        public void AfterConnectingDeviceMasterShouldHaveItInList()
        {
            ConnectMasterAndSlave();

            Assert.True(_master.ConnectedSlaveDevices.First() == _client);
            AssertWait.Max(1000, () => _slave.IsConnected);
        }
コード例 #2
0
        public void DisconnectClientShouldBeRemovedFromMaster()
        {
            ConnectMasterAndSlave();
            AssertWait.Max(1000, () => _slave.IsConnected);

            _client.Disconnect();

            AssertWait.Max(10000, () => _master.ConnectedSlaveDevices.Count == 0);
        }
コード例 #3
0
        private void ConnectMasterAndSlave()
        {
            _slave = new ModbusSlave("Test", _source, _port, 1);
            _slave.Start();

            _master = new ModbusMaster();
            _client = _master.ConnectDevice("localhost", _port, 1);

            AssertWait.Max(2000, () => _client.IsConnected);
        }
コード例 #4
0
        public void TerminatingSlaveShouldBeRemovedFromMaster()
        {
            ConnectMasterAndSlave();
            AssertWait.Max(1000, () => _slave.IsConnected);

            _slave.Terminate();

            _client.ReadInputRegisters(0, 1);
            Assert.True(_master.ConnectedSlaveDevices.Count == 0);
        }
コード例 #5
0
        public void PollServiceShouldReconnectIfDeviceReturns()
        {
            var poll = new ModbusDevicePollService(_client, ModbusDevicePollService.Register.Holding, 0, 50)
            {
                PollRetries = 0
            };

            poll.ConnectionChanged   += (_, _) => { _connectionChanges++; };
            poll.ProcessImageChanged += _ => { _processImageChanges++; };
            poll.InputChanged        += (_, _, _) => { _inputChanges++; };
            poll.PollFailed          += (_) => { _pollFailed++; };
            var started = poll.Start(TimeSpan.FromSeconds(0.5), true);

            Assert.True(started);
            WaitForStableConnection(poll);

            // wait for poll connected
            AssertWait.Max(1000, () => _connectionChanges > 0);
            _connectionChanges = 0;

            // after one second - terminate slave
            Task.Delay(TimeSpan.FromSeconds(1)).Wait();
            _slave.Terminate();
            AssertWait.Max(2000, () => _connectionChanges > 0);

            // after two more seconds - restart slave
            Task.Delay(TimeSpan.FromSeconds(2)).Wait();
            _slave.Start();

            // after two more seconds - the poll service should have reconnected
            Task.Delay(TimeSpan.FromSeconds(2)).Wait();
            AssertWait.Max(2000, () => poll.IsConnected);
            Assert.Equal(2, _connectionChanges);

            // after one more second - there should be no events
            _processImageChanges = 0;
            _inputChanges        = 0;
            _pollFailed          = 0;
            Task.Delay(TimeSpan.FromSeconds(1)).Wait();

            Assert.Equal(0, _processImageChanges);
            Assert.Equal(0, _inputChanges);
            Assert.Equal(0, _pollFailed);

            // reconnected - now change image
            _source.WriteRegisters(0, new ushort[] { 0x55AA });
            // after one more second - there should be an event
            AssertWait.Max(2000, () => _processImageChanges > 0);

            // there should be no more connection changes
            AssertWait.Max(1000, () => _connectionChanges > 1);
        }
コード例 #6
0
        public void PollServiceShouldSetDisconnectedIfDeviceVanishes()
        {
            var poll    = new ModbusDevicePollService(_client, ModbusDevicePollService.Register.Holding, 0, 50);
            var started = poll.Start(TimeSpan.FromSeconds(0.5), true);

            Assert.True(started);
            WaitForStableConnection(poll);

            // after one second - terminate slave
            Task.Delay(TimeSpan.FromSeconds(1)).Wait();
            _slave.Terminate();

            // after two more seconds - service should not more be connected
            Task.Delay(TimeSpan.FromSeconds(2)).Wait();
            AssertWait.Max(2000, () => !poll.IsConnected);
        }
コード例 #7
0
        public void PollServiceShouldSignalChangeDuringReconnect()
        {
            var poll = new ModbusDevicePollService(_client, ModbusDevicePollService.Register.Holding, 0, 50)
            {
                PollRetries = 0
            };

            poll.ConnectionChanged   += (_, _) => { _connectionChanges++; };
            poll.ProcessImageChanged += _ => { _processImageChanges++; };
            poll.InputChanged        += (_, _, _) => { _inputChanges++; };
            poll.PollFailed          += (_) => { _pollFailed++; };
            var started = poll.Start(TimeSpan.FromSeconds(0.5), true);

            Assert.True(started);
            WaitForStableConnection(poll);

            // wait for poll connected
            Task.Delay(TimeSpan.FromSeconds(2)).Wait();
            _connectionChanges   = 0;
            _processImageChanges = 0;
            _inputChanges        = 0;
            _pollFailed          = 0;

            // after two seconds - terminate slave
            Task.Delay(TimeSpan.FromSeconds(2)).Wait();
            _slave.Terminate();

            // and change data
            _source.WriteRegisters(0, new ushort[] { 0x55AA });

            // after four more seconds - expect connection change
            Task.Delay(TimeSpan.FromSeconds(4)).Wait();
            AssertWait.Max(4000, () => _connectionChanges > 0);

            // restart slave
            _slave.Start();

            // after four more seconds - the poll service should have reconnected
            Task.Delay(TimeSpan.FromSeconds(2)).Wait();
            AssertWait.Max(2000, () => poll.IsConnected);
            Assert.Equal(2, _connectionChanges);      // disconnected, connected
            Assert.Equal(1, _processImageChanges);    // one image
            Assert.Equal(8, _inputChanges);           // count one bits of 0x55AA
            Assert.True(_pollFailed > 0);             // minimum one
        }
コード例 #8
0
        public void PollServiceShouldSendFirstEventWithinOneSecond()
        {
            var poll = new ModbusDevicePollService(_client, ModbusDevicePollService.Register.Holding, 0, 50);

            poll.ProcessImageChanged += (_) => { _processImageChanges++; };
            var started = poll.Start(TimeSpan.FromSeconds(1), true);

            Assert.True(started);
            WaitForStableConnection(poll);

            // after a second - change data
            Task.Delay(TimeSpan.FromSeconds(1)).Wait();
            _processImageChanges = 0;
            _source.WriteRegisters(0, new ushort[] { 0x55AA });

            // after one second - change should be reported
            AssertWait.Max(2000, () => _processImageChanges > 0);
        }
コード例 #9
0
        public void PollServiceShouldSendFailedEventIfDeviceVanishesAndNoOthers()
        {
            var poll = new ModbusDevicePollService(_client, ModbusDevicePollService.Register.Holding, 0, 50);

            poll.ProcessImageChanged += _ => { _processImageChanges++; };
            poll.InputChanged        += (_, _, _) => { _inputChanges++; };
            poll.PollFailed          += (_) => { _pollFailed++; };
            var started = poll.Start(TimeSpan.FromSeconds(0.5), true);

            Assert.True(started);
            WaitForStableConnection(poll);

            // after one second - terminate slave
            Task.Delay(TimeSpan.FromSeconds(1)).Wait();
            _processImageChanges = 0;
            _inputChanges        = 0;
            _pollFailed          = 0;
            _slave.Terminate();
            AssertWait.Max(3000, () => _pollFailed > 0);

            Assert.Equal(0, _processImageChanges);
            Assert.Equal(0, _inputChanges);
        }