示例#1
0
        public async Task ClientReadDeviceInformationBasicTest()
        {
            var expectedRequest  = new byte[] { 0, 0, 0, 5, 13, 43, 14, 1, 0 };
            var expectedResponse = new Dictionary <DeviceIDObject, string>
            {
                { DeviceIDObject.VendorName, "AM.WD" },
                { DeviceIDObject.ProductCode, "Mini-Test" },
                { DeviceIDObject.MajorMinorRevision, "1.2.3.4" }
            };

            using (var server = new MiniTestServer())
            {
                server.RequestHandler = (request, clientIp) =>
                {
                    CollectionAssert.AreEqual(expectedRequest, request.Skip(2).ToArray(), "Request is incorrect");
                    Console.WriteLine("Server sending response");

                    var bytes = new List <byte>();
                    bytes.AddRange(request.Take(2));
                    bytes.AddRange(new byte[] { 0, 0, 0, 0, 13, 43, 14, 1, 1, 0, 0, (byte)expectedResponse.Count });
                    var len = 8;
                    foreach (var kvp in expectedResponse)
                    {
                        var b = Encoding.ASCII.GetBytes(kvp.Value);
                        bytes.Add((byte)kvp.Key);
                        len++;
                        bytes.Add((byte)b.Length);
                        len++;
                        bytes.AddRange(b);
                        len += b.Length;
                    }
                    bytes[5] = (byte)len;

                    return(bytes.ToArray());
                };
                server.Start();
                using (var client = new ModbusClient(IPAddress.Loopback, server.Port, new ConsoleLogger()))
                {
                    await client.Connect();

                    Assert.IsTrue(client.IsConnected);

                    var deviceInfo = await client.ReadDeviceInformation(13, DeviceIDCategory.Basic);

                    Assert.IsTrue(string.IsNullOrWhiteSpace(server.LastError), server.LastError);
                    CollectionAssert.AreEqual(expectedResponse, deviceInfo, "Response is incorrect");
                }
            }
        }
示例#2
0
        public async Task ClientReadDeviceInformationIndividualTest()
        {
            byte[] expectedRequest  = new byte[] { 0, 0, 0, 5, 13, 43, 14, 4, (byte)DeviceIDObject.ModelName };
            var    expectedResponse = new Dictionary <DeviceIDObject, string>
            {
                { DeviceIDObject.ModelName, "TestModel" }
            };

            using var server = new MiniTestServer
                  {
                      RequestHandler = (request, clientIp) =>
                      {
                          CollectionAssert.AreEqual(expectedRequest, request.Skip(2).ToArray(), "Request is incorrect");
                          Console.WriteLine("Server sending response");

                          var bytes = new List <byte>();
                          bytes.AddRange(request.Take(2));
                          bytes.AddRange(new byte[] { 0, 0, 0, 0, 13, 43, 14, 4, 2, 0, 0, (byte)expectedResponse.Count });
                          int len = 8;
                          foreach (var kvp in expectedResponse)
                          {
                              byte[] b = Encoding.ASCII.GetBytes(kvp.Value);
                              bytes.Add((byte)kvp.Key);
                              len++;
                              bytes.Add((byte)b.Length);
                              len++;
                              bytes.AddRange(b);
                              len += b.Length;
                          }
                          bytes[5] = (byte)len;

                          return(bytes.ToArray());
                      }
                  };
            server.Start();

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

            Assert.IsTrue(client.IsConnected);

            var deviceInfo = await client.ReadDeviceInformation(13, DeviceIDCategory.Individual, DeviceIDObject.ModelName);

            Assert.IsTrue(string.IsNullOrWhiteSpace(server.LastError), server.LastError);
            CollectionAssert.AreEqual(expectedResponse, deviceInfo, "Response is incorrect");
        }