This commit is contained in:
2248356998 qq.com
2025-09-17 11:07:45 +08:00
parent 764957c014
commit 901aa2d59f
9 changed files with 42 additions and 40 deletions

View File

@@ -15,23 +15,19 @@ public class ExpiringDictionary<TKey, TValue> : IDisposable
public void TryAdd(TKey key, TValue value)
{
if (_cleanupTimer.Disposed) throw new ObjectDisposedException(nameof(ExpiringDictionary<TKey, TValue>));
_dict.TryAdd(key, value);
}
public bool TryGetValue(TKey key, out TValue value)
{
if (_cleanupTimer.Disposed) throw new ObjectDisposedException(nameof(ExpiringDictionary<TKey, TValue>));
return _dict.TryGetValue(key, out value);
}
public TValue GetOrAdd(TKey key, Func<TKey, TValue> func)
{
if (_cleanupTimer.Disposed) throw new ObjectDisposedException(nameof(ExpiringDictionary<TKey, TValue>));
return _dict.GetOrAdd(key, func);
}
public TValue GetOrAdd(TKey key, TValue value)
{
if (_cleanupTimer.Disposed) throw new ObjectDisposedException(nameof(ExpiringDictionary<TKey, TValue>));
return _dict.GetOrAdd(key, value);
}

View File

@@ -1,9 +1,9 @@
<Project>
<PropertyGroup>
<PluginVersion>10.11.51</PluginVersion>
<ProPluginVersion>10.11.51</ProPluginVersion>
<DefaultVersion>10.11.51</DefaultVersion>
<PluginVersion>10.11.52</PluginVersion>
<ProPluginVersion>10.11.52</ProPluginVersion>
<DefaultVersion>10.11.52</DefaultVersion>
<AuthenticationVersion>10.11.5</AuthenticationVersion>
<SourceGeneratorVersion>10.11.4</SourceGeneratorVersion>
<NET8Version>8.0.20</NET8Version>

View File

@@ -68,6 +68,7 @@ public class ModbusMasterDemo
bytes = await device.ModbusReadAsync(new ModbusAddress()
{
Station = 1,
StartAddress = 0,
FunctionCode = 3,
Length = 10,
@@ -92,6 +93,7 @@ public class ModbusMasterDemo
write = await device.ModbusRequestAsync(new ModbusAddress()
{
Station = 1,
StartAddress = 0,
FunctionCode = 3,
MasterWriteDatas = device.ThingsGatewayBitConverter.GetBytes(new double[] { 123.456, 123.456 })

View File

@@ -110,26 +110,7 @@ public class ModbusBenchmark : IDisposable
}
}
}
[Benchmark]
public async Task LongbowModbus()
{
List<Task> tasks = new List<Task>();
foreach (var _lgbModbusClient in _lgbModbusClients)
{
for (int i = 0; i < Program.TaskNumberOfItems; i++)
{
tasks.Add(Task.Run(async () =>
{
for (int i = 0; i < Program.NumberOfItems; i++)
{
var task = await _lgbModbusClient.ReadHoldingRegistersAsync(1, 0, 100);
}
}));
}
}
await Task.WhenAll(tasks);
}
[Benchmark]
public async Task ThingsGateway()
{
@@ -144,7 +125,7 @@ public class ModbusBenchmark : IDisposable
{
for (int i = 0; i < Program.NumberOfItems; i++)
{
var result = await thingsgatewaymodbus.ModbusReadAsync(new ModbusAddress() { FunctionCode = 3, StartAddress = 0, Length = 100 });
var result = await thingsgatewaymodbus.ModbusReadAsync(new ModbusAddress() { Station = 1, FunctionCode = 3, StartAddress = 0, Length = 100 }).ConfigureAwait(false);
if (!result.IsSuccess)
{
throw new Exception(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + result.ToString());
@@ -157,7 +138,27 @@ public class ModbusBenchmark : IDisposable
await Task.WhenAll(tasks);
}
[Benchmark]
public async Task LongbowModbus()
{
List<Task> tasks = new List<Task>();
foreach (var _lgbModbusClient in _lgbModbusClients)
{
for (int i = 0; i < Program.TaskNumberOfItems; i++)
{
tasks.Add(Task.Run(async () =>
{
for (int i = 0; i < Program.NumberOfItems; i++)
{
using var cts = new CancellationTokenSource(3000);
var task = await _lgbModbusClient.ReadHoldingRegistersAsync(1, 0, 100, cts.Token).ConfigureAwait(false);
}
}));
}
}
await Task.WhenAll(tasks);
}
[Benchmark]
public async Task TouchSocket()
@@ -171,7 +172,7 @@ public class ModbusBenchmark : IDisposable
{
for (int i = 0; i < Program.NumberOfItems; i++)
{
var result = await modbusTcpMaster.ReadHoldingRegistersAsync(0, 100);
var result = await modbusTcpMaster.ReadHoldingRegistersAsync(0, 100).ConfigureAwait(false);
var data = TouchSocketBitConverter.ConvertValues<byte, ushort>(result.Data.Span, EndianType.Little);
if (!result.IsSuccess)
{
@@ -197,7 +198,7 @@ public class ModbusBenchmark : IDisposable
{
for (int i = 0; i < Program.NumberOfItems; i++)
{
var result = await nmodbus.ReadHoldingRegistersAsync(1, 0, 100);
var result = await nmodbus.ReadHoldingRegistersAsync(1, 0, 100).ConfigureAwait(false);
}
}));
}

View File

@@ -20,9 +20,9 @@ namespace BenchmarkConsoleApp
{
internal class Program
{
public static int ClientCount = 30;
public static int TaskNumberOfItems = 10;
public static int NumberOfItems = 30;
public static int ClientCount = 50;
public static int TaskNumberOfItems = 1;
public static int NumberOfItems = 50;
private static async Task Main(string[] args)
{

View File

@@ -43,17 +43,17 @@
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
<PackageReference Include="HslCommunication" Version="12.5.0" />
<PackageReference Include="Longbow.Modbus" Version="9.0.5" />
<PackageReference Include="Longbow.Modbus" Version="9.0.6" />
<PackageReference Include="NModbus" Version="3.0.81" />
<PackageReference Include="NModbus.Serial" Version="3.0.81" />
<PackageReference Include="S7netplus" Version="0.20.0" />
<PackageReference Include="ThingsGateway.Foundation.Modbus" Version="$(DefaultVersion)" />
<PackageReference Include="ThingsGateway.Foundation.SiemensS7" Version="$(DefaultVersion)" />
<!--<PackageReference Include="ThingsGateway.Foundation.Modbus" Version="$(DefaultVersion)" />
<PackageReference Include="ThingsGateway.Foundation.SiemensS7" Version="$(DefaultVersion)" />-->
<PackageReference Include="TouchSocket.Modbus" Version="4.0.0-beta.27" />
</ItemGroup>
<ItemGroup>
<!--<ProjectReference Include="..\..\Plugin\ThingsGateway.Foundation.Modbus\ThingsGateway.Foundation.Modbus.csproj" />
<ProjectReference Include="..\..\Plugin\ThingsGateway.Foundation.SiemensS7\ThingsGateway.Foundation.SiemensS7.csproj" />-->
<ProjectReference Include="..\..\Plugin\ThingsGateway.Foundation.Modbus\ThingsGateway.Foundation.Modbus.csproj" />
<ProjectReference Include="..\..\Plugin\ThingsGateway.Foundation.SiemensS7\ThingsGateway.Foundation.SiemensS7.csproj" />
</ItemGroup>
</Project>

View File

@@ -52,7 +52,7 @@ public class ModbusRequest
/// <summary>
/// 站号
/// </summary>
public byte Station { get; set; }
public byte Station { get; set; } = 1;
#endregion Request
}

View File

@@ -97,7 +97,9 @@ public class ModbusSlave : DeviceBase, IModbusAddress
{
return $"{base.GetAddressDescription()}{Environment.NewLine}{ModbusHelper.GetAddressDescription()}";
}
protected override void SetChannel()
{
}
/// <inheritdoc/>
public override DataHandlingAdapter GetDataAdapter()
{

View File

@@ -114,7 +114,8 @@ public class ModbusSlave : BusinessBase
try
{
await _plc.ConnectAsync(cancellationToken).ConfigureAwait(false);
if (channel.ChannelType == ChannelTypeEnum.TcpService)
await _plc.ConnectAsync(cancellationToken).ConfigureAwait(false);
}
catch
{