Esempio n. 1
0
        static void DoServerWork(ModbusTcpServer server)
        {
            var random = new Random();

            // Option A: normal performance version, more flexibility

            /* get buffer in standard form (Span<short>) */
            var registers = server.GetHoldingRegisters();

            registers.SetLittleEndian <int>(address: 5, random.Next());

            // Option B: high performance version, less flexibility

            /* interpret buffer as array of bytes (8 bit) */
            var byte_buffer = server.GetHoldingRegisterBuffer <byte>();

            byte_buffer[20] = (byte)(random.Next() >> 24);

            /* interpret buffer as array of shorts (16 bit) */
            var short_buffer = server.GetHoldingRegisterBuffer <short>();

            short_buffer[30] = (short)(random.Next(0, 100) >> 16);

            /* interpret buffer as array of ints (32 bit) */
            var int_buffer = server.GetHoldingRegisterBuffer <int>();

            int_buffer[40] = random.Next(0, 100);
        }
Esempio n. 2
0
        public void FC03Test()
        {
            // Arrange
            _server.Start(_endpoint);

            lock (_server.Lock)
            {
                var buffer = _server.GetHoldingRegisterBuffer <float>();

                buffer[6] = 65.455F;
                buffer[7] = 24;
                buffer[8] = 25;
            }

            var client = new ModbusTcpClient();

            client.Connect(_endpoint);

            // Act
            var actual = client.ReadHoldingRegisters <float>(0, 2, 10);

            // Assert
            var expected = _array;

            Assert.True(expected.SequenceEqual(actual.ToArray()));
        }
Esempio n. 3
0
        public void FC023Test()
        {
            // Arrange
            var endpoint = EndpointSource.GetNext();

            using var server = new ModbusTcpServer();
            server.Start(endpoint);

            lock (server.Lock)
            {
                var buffer = server.GetHoldingRegisterBuffer <float>();

                buffer[6] = 65.455F;
                buffer[7] = 24;
                buffer[8] = 25;
            }

            var client = new ModbusTcpClient();

            client.Connect(endpoint);

            // Act
            var actual1 = client.ReadWriteMultipleRegisters <float, float>(0, 2, 10, 12, new float[] { 1.211F });

            // Assert
            var expected = new float[] { 0, 0, 0, 0, 0, 1.211F, 24, 25, 0, 0 };

            Assert.True(expected.SequenceEqual(actual1.ToArray()));

            lock (server.Lock)
            {
                var actual2 = server.GetHoldingRegisterBuffer <float>().Slice(1, 10);
                Assert.True(expected.SequenceEqual(actual2.ToArray()));
            }
        }
Esempio n. 4
0
        public void FC16Test()
        {
            // Arrange
            var endpoint = EndpointSource.GetNext();

            var server = new ModbusTcpServer();

            server.Start(endpoint);

            var client = new ModbusTcpClient();

            client.Connect(endpoint);

            // Act
            client.WriteMultipleRegisters(0, 2, _array);

            // Assert
            var expected = _array;

            lock (server.Lock)
            {
                var actual = server.GetHoldingRegisterBuffer <float>().Slice(1, 10);
                Assert.True(expected.SequenceEqual(actual.ToArray()));
            }
        }
Esempio n. 5
0
        public void FC06Test()
        {
            // Arrange
            var endpoint = EndpointSource.GetNext();

            var server = new ModbusTcpServer();

            server.Start(endpoint);

            var client = new ModbusTcpClient();

            client.Connect(endpoint);

            // Act
            client.WriteSingleRegister(0, 02, 259);
            client.WriteSingleRegister(0, 10, 125);
            client.WriteSingleRegister(0, 11, 16544);
            client.WriteSingleRegister(0, 12, 4848);

            // Assert
            var expected = new short[] { 0, 0, 259, 0, 0, 0, 0, 0, 0, 0, 125, 16544, 4848 };

            lock (server.Lock)
            {
                var actual = server.GetHoldingRegisterBuffer <short>().Slice(0, 13);
                Assert.True(expected.SequenceEqual(actual.ToArray()));
            }
        }
Esempio n. 6
0
        public void HandlesBigEndian()
        {
            // Arrange
            var endpoint = EndpointSource.GetNext();

            var server = new ModbusTcpServer();

            server.Start(endpoint);

            var client = new ModbusTcpClient();

            client.Connect(endpoint, ModbusEndianness.BigEndian);

            // Act
            client.WriteMultipleRegisters(0, 0, new int[] { 0x20302020, 0x40101010, 0x11220000 });

            // Assert
            var expected = new int[] { 0x20203020, 0x10101040, 0x00002211 };

            lock (server.Lock)
            {
                var actual = server.GetHoldingRegisterBuffer <int>().Slice(0, 3);
                Assert.True(expected.SequenceEqual(actual.ToArray()));
            }
        }
Esempio n. 7
0
        public async Task FC03Test()
        {
            // Arrange
            var endpoint = EndpointSource.GetNext();

            using var server = new ModbusTcpServer();
            server.Start(endpoint);

            void AsyncWorkaround()
            {
                var buffer = server.GetHoldingRegisterBuffer <float>();

                buffer[6] = 65.455F;
                buffer[7] = 24;
                buffer[8] = 25;
            }

            lock (server.Lock)
            {
                AsyncWorkaround();
            }

            var client = new ModbusTcpClient();

            client.Connect(endpoint);

            // Act
            var actual = await client.ReadHoldingRegistersAsync <float>(0, 2, 10);

            // Assert
            var expected = _array;

            Assert.True(expected.SequenceEqual(actual.ToArray()));
        }
Esempio n. 8
0
        static void DoServerWork(ModbusTcpServer server)
        {
            var random = new Random();

            // interpret buffer as array of bytes (8 bit)
            var byte_buffer = server.GetHoldingRegisterBuffer <byte>();

            byte_buffer[20] = (byte)(random.Next() >> 24);

            // interpret buffer as array of shorts (16 bit)
            var short_buffer = server.GetHoldingRegisterBuffer <short>();

            short_buffer[30] = (short)(random.Next(0, 100) >> 16);

            // interpret buffer as array of ints (32 bit)
            var int_buffer = server.GetHoldingRegisterBuffer <int>();

            int_buffer[40] = random.Next(0, 100);
        }