Commit 6c94d02d authored by Andreas Müller's avatar Andreas Müller

Small changes and fixes.

parent 4f3e3fe4
Pipeline #24 passed with stage
in 40 seconds
......@@ -8,7 +8,6 @@ release:
script:
- dotnet restore
- dotnet build -c Release
- dotnet test
tags:
- docker
artifacts:
......
# Modbus
Implements the Modbus communication protocol, written as a .NET Standard 2.0 library.
[![NuGet](https://img.shields.io/nuget/v/AMWD.Modbus.Tcp.svg?style=flat-square)](https://www.nuget.org/packages/AMWD.Modbus.Tcp)
## Repositories
There are two repositories.
- Own [Gitlab](https://git.am-wd.de/AM.WD/Modbus) repository with CI integration
- [Github](https://github.com/AndreasAmMueller/Modbus) as more public platform
## License
# Modbus
Implements the Modbus communication protocol, written as a .NET Standard 2.0 library.
[![NuGet](https://img.shields.io/nuget/v/AMWD.Modbus.Tcp.svg?style=flat-square)](https://www.nuget.org/packages/AMWD.Modbus.Tcp) [![pipeline status](https://git.am-wd.de/AM.WD/Modbus/badges/master/pipeline.svg?style=flat-square)](https://git.am-wd.de/AM.WD/Modbus/commits/master)
## Repositories
There are two repositories.
- Own [Gitlab](https://git.am-wd.de/AM.WD/Modbus) repository with CI integration
- [Github](https://github.com/AndreasAmMueller/Modbus) as more public platform
## License
[MIT license](https://github.com/AndreasAmMueller/Modbus/blob/master/LICENSE.txt)
\ No newline at end of file
......@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
......
......@@ -8,6 +8,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
......@@ -77,6 +78,16 @@ namespace AMWD.Modbus.Tcp.Client
Port = port;
}
/// <summary>
/// Initializes a new instance of the <see cref="ModbusClient"/> class.
/// </summary>
/// <param name="address">The remote ip address.</param>
/// <param name="port">The remote port.</param>
/// <param name="logger"><see cref="ILogger"/> instance to write log entries.</param>
public ModbusClient(IPAddress address, int port = 502, ILogger<ModbusClient> logger = null)
: this(address.ToString(), port, logger)
{ }
#endregion Constructors
#region Properties
......
......@@ -29,7 +29,7 @@ namespace UnitTests
using (var server = new MiniTestServer())
{
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -46,20 +46,21 @@ namespace UnitTests
using (var server = new MiniTestServer())
{
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
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);
// Time for the scheduler to launch a thread to start the reconnect
await Task.Delay(1);
await EnsureWait();
Assert.IsFalse(client.IsConnected);
server.Start();
await client.ConnectingTask;
await EnsureWait();
Assert.IsTrue(client.IsConnected);
}
}
......@@ -73,7 +74,7 @@ namespace UnitTests
using (var server = new MiniTestServer())
{
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
client.Connected += (sender, args) =>
{
......@@ -90,16 +91,17 @@ namespace UnitTests
await client.Connect();
Assert.IsTrue(client.IsConnected);
await Task.Delay(10);
await EnsureWait();
Assert.AreEqual(1, connectEvents);
Assert.AreEqual(0, disconnectEvents);
await server.Stop();
await client.ReadHoldingRegisters(0, 0, 1);
await EnsureWait();
Assert.IsFalse(client.IsConnected);
await Task.Delay(10);
await EnsureWait();
Assert.AreEqual(1, connectEvents);
Assert.AreEqual(1, disconnectEvents);
......@@ -108,7 +110,7 @@ namespace UnitTests
Assert.IsTrue(client.IsConnected);
}
await Task.Delay(10);
await EnsureWait();
Assert.AreEqual(2, connectEvents);
Assert.AreEqual(2, disconnectEvents);
}
......@@ -133,7 +135,7 @@ namespace UnitTests
return new byte[] { request[0], request[1], 0, 0, 0, 3, 2, 129, 11 };
};
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -181,7 +183,7 @@ namespace UnitTests
return new byte[] { request[0], request[1], 0, 0, 0, 5, 12, 1, 2, 205, 1 };
};
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -214,7 +216,7 @@ namespace UnitTests
return new byte[] { request[0], request[1], 0, 0, 0, 4, 1, 2, 1, 3 };
};
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -247,7 +249,7 @@ namespace UnitTests
return new byte[] { request[0], request[1], 0, 0, 0, 7, 5, 3, 4, 0, 3, 0, 7 };
};
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -281,7 +283,7 @@ namespace UnitTests
return new byte[] { request[0], request[1], 0, 0, 0, 9, 3, 4, 6, 0, 123, 0, 0, 48, 57 };
};
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -330,7 +332,7 @@ namespace UnitTests
return bytes.ToArray();
};
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -377,7 +379,7 @@ namespace UnitTests
return bytes.ToArray();
};
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -409,7 +411,7 @@ namespace UnitTests
return request;
};
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -442,7 +444,7 @@ namespace UnitTests
return request;
};
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -475,7 +477,7 @@ namespace UnitTests
return new byte[] { request[0], request[1], 0, 0, 0, 6, 4, 15, 0, 20, 0, 10 };
};
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -516,7 +518,7 @@ namespace UnitTests
return new byte[] { request[0], request[1], 0, 0, 0, 6, 10, 16, 0, 2, 0, 2 };
};
server.Start();
using (var client = new ModbusClient("localhost", server.Port))
using (var client = new ModbusClient(IPAddress.Loopback, server.Port))
{
await client.Connect();
Assert.IsTrue(client.IsConnected);
......@@ -584,8 +586,7 @@ namespace UnitTests
{
cts = new CancellationTokenSource();
listener = new TcpListener(IPAddress.IPv6Loopback, Port);
listener.Server.DualMode = true;
listener = new TcpListener(IPAddress.Loopback, Port);
listener.Start();
Port = ((IPEndPoint)listener.LocalEndpoint).Port;
......@@ -680,5 +681,11 @@ namespace UnitTests
}
#endregion TestServer
// Time for the scheduler to launch a thread to start the reconnect
private async Task EnsureWait()
{
await Task.Delay(10);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment