Exemple #1
0
        public async Task ClientWriteRegistersTest()
        {
            // Function Code 0x10

            var expectedRequest = new byte[] { 0, 0, 0, 11, 10, 16, 0, 2, 0, 2, 4, 0, 10, 1, 2 };

            using (var server = new MiniTestServer())
            {
                server.RequestHandler = (request, clientIp) =>
                {
                    CollectionAssert.AreEqual(expectedRequest, request.Skip(2).ToArray(), "Request is incorrect");
                    Console.WriteLine("Server sending response");
                    return(new byte[] { request[0], request[1], 0, 0, 0, 6, 10, 16, 0, 2, 0, 2 });
                };
                server.Start();
                using (var client = new ModbusClient(IPAddress.Loopback, server.Port, new ConsoleLogger()))
                {
                    await client.Connect();

                    Assert.IsTrue(client.IsConnected);

                    var registers = new List <Register>
                    {
                        new Register {
                            Address = 2, Value = 10
                        },
                        new Register {
                            Address = 3, Value = 258
                        }
                    };
                    var success = await client.WriteRegisters(10, registers);

                    Assert.IsTrue(string.IsNullOrWhiteSpace(server.LastError), server.LastError);
                    Assert.IsTrue(success);
                }
            }
        }