Commit 249db101 authored by Andreas Müller's avatar Andreas Müller

Restructured ModbusTCP Client to be more flexible using TaskCompletionSources.

parent 19c836d8
Pipeline #30 passed with stage
......@@ -3,8 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Modbus.Tcp", "src\Modbus.Tcp\Modbus.Tcp.csproj", "{BF60D1D4-3767-4EC4-AC53-C1772958BE14}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0A7CE6D2-374F-4EBC-BFD3-DE2587A6701C}"
ProjectSection(SolutionItems) = preProject
.gitattributes = .gitattributes
......@@ -24,6 +22,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Modbus.Common", "src\Modbus
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Modbus.Serial", "src\Modbus.Serial\Modbus.Serial.csproj", "{30629B0B-B815-4951-AE56-9A0038210ECB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Modbus.Tcp", "src\Modbus.Tcp\Modbus.Tcp.csproj", "{BF60D1D4-3767-4EC4-AC53-C1772958BE14}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "test\UnitTests\UnitTests.csproj", "{33EB4212-B2B4-4CFA-A374-1DD9D2675822}"
EndProject
Global
......
......@@ -105,7 +105,7 @@ namespace ConsoleDemo
Console.Write("Device ID: ");
var id = Convert.ToByte(Console.ReadLine().Trim());
Console.Write("Function [1] Read Register, [2] Device Info: ");
Console.Write("Function [1] Read Register, [2] Device Info, [9] Write Register : ");
var fn = Convert.ToInt32(Console.ReadLine().Trim());
try
......@@ -199,6 +199,40 @@ namespace ConsoleDemo
}
}
break;
case 9:
{
Console.Write("Address: ");
var address = Convert.ToUInt16(Console.ReadLine().Trim());
Console.Write("Bytes (HEX): ");
var byteStr = Console.ReadLine().Trim();
byteStr = byteStr.Replace(" ", "").ToLower();
var bytes = Enumerable.Range(0, byteStr.Length)
.Where(i => i % 2 == 0)
.Select(i => Convert.ToByte(byteStr.Substring(i, 2), 16))
.ToArray();
var registers = Enumerable.Range(0, bytes.Length)
.Where(i => i % 2 == 0)
.Select(i =>
{
return new Register
{
Address = address++,
HiByte = bytes[i],
LoByte = bytes[i + 1]
};
})
.ToList();
if (!await client.WriteRegisters(id, registers))
{
throw new Exception($"Writing '{byteStr}' to address {address} failed");
}
}
break;
}
}
......
This diff is collapsed.
......@@ -11,19 +11,6 @@ namespace AMWD.Modbus.Tcp.Protocol
/// </summary>
internal class Request
{
#region Fields
private static int transactionNumber = 0;
private static ushort NextTransactionId
{
get
{
return (ushort)Interlocked.Increment(ref transactionNumber);
}
}
#endregion Fields
#region Constructors
/// <summary>
......@@ -33,9 +20,7 @@ namespace AMWD.Modbus.Tcp.Protocol
/// The transaction id is automatically set to a unique number.
/// </remarks>
internal Request()
{
TransactionId = NextTransactionId;
}
{ }
/// <summary>
/// Initializes a new instance of the <see cref="Request"/> class.
......@@ -53,7 +38,7 @@ namespace AMWD.Modbus.Tcp.Protocol
/// <summary>
/// Gets the unique transaction id of the request.
/// </summary>
public ushort TransactionId { get; private set; }
public ushort TransactionId { get; set; }
/// <summary>
/// Gets the id to identify the device.
......
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