diff --git a/src/ThingsGateway.Gateway.Application/Plugin/Collect/CollectBase.cs b/src/ThingsGateway.Gateway.Application/Plugin/Collect/CollectBase.cs
index 218521737..f6edfc418 100644
--- a/src/ThingsGateway.Gateway.Application/Plugin/Collect/CollectBase.cs
+++ b/src/ThingsGateway.Gateway.Application/Plugin/Collect/CollectBase.cs
@@ -50,6 +50,8 @@ public abstract class CollectBase : DriverBase
private IStringLocalizer Localizer { get; set; }
+ public virtual bool IsSingleThread => true;
+
internal override void Init(DeviceRunTime device)
{
Localizer = App.CreateLocalizerByType(typeof(CollectBase))!;
@@ -89,8 +91,13 @@ public abstract class CollectBase : DriverBase
{
try
{
- //if (IsSingleThread)
- // await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+ if (IsSingleThread)
+ {
+ while (WriteLock.IsWaitting)
+ {
+ await Task.Delay(100).ConfigureAwait(false);
+ }
+ }
if (cancellationToken.IsCancellationRequested)
return new(new OperationCanceledException());
@@ -112,8 +119,6 @@ public abstract class CollectBase : DriverBase
}
finally
{
- //if (IsSingleThread)
- // WriteLock.Release();
}
}
@@ -126,8 +131,8 @@ public abstract class CollectBase : DriverBase
try
{
// 如果是单线程模式,则等待写入锁
- //if (IsSingleThread)
- // await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+ if (IsSingleThread)
+ await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
// 检查协议是否为空,如果为空则抛出异常
if (Protocol == null)
@@ -139,11 +144,18 @@ public abstract class CollectBase : DriverBase
// 使用并发方式遍历写入信息列表,并进行异步写入操作
await writeInfoLists.ParallelForEachAsync(async (writeInfo, cancellationToken) =>
{
- // 调用协议的写入方法,将写入信息中的数据写入到对应的寄存器地址,并获取操作结果
- var result = await Protocol.WriteAsync(writeInfo.Key.RegisterAddress, writeInfo.Value, writeInfo.Key.DataType, cancellationToken).ConfigureAwait(false);
+ try
+ {
+ // 调用协议的写入方法,将写入信息中的数据写入到对应的寄存器地址,并获取操作结果
+ var result = await Protocol.WriteAsync(writeInfo.Key.RegisterAddress, writeInfo.Value, writeInfo.Key.DataType, cancellationToken).ConfigureAwait(false);
- // 将操作结果添加到结果字典中,使用变量名称作为键
- operResults.TryAdd(writeInfo.Key.Name, result);
+ // 将操作结果添加到结果字典中,使用变量名称作为键
+ operResults.TryAdd(writeInfo.Key.Name, result);
+ }
+ catch (Exception ex)
+ {
+ operResults.TryAdd(writeInfo.Key.Name, new(ex));
+ }
}, CollectProperties.ConcurrentCount, cancellationToken).ConfigureAwait(false);
// 返回包含操作结果的字典
@@ -152,8 +164,8 @@ public abstract class CollectBase : DriverBase
finally
{
// 如果是单线程模式,则释放写入锁
- //if (IsSingleThread)
- // WriteLock.Release();
+ if (IsSingleThread)
+ WriteLock.Release();
}
}
@@ -535,14 +547,10 @@ public abstract class CollectBase : DriverBase
/// 指示是否为读取操作
/// 取消操作的通知
/// 操作结果,包含执行方法的结果
- internal async ValueTask> InvokeMethodAsync(VariableMethod variableMethod, string? value = null, bool isRead = true, CancellationToken cancellationToken = default)
+ protected virtual async ValueTask> InvokeMethodAsync(VariableMethod variableMethod, string? value = null, bool isRead = true, CancellationToken cancellationToken = default)
{
try
{
- // 如果配置为单线程模式,则获取写入锁定
- //if (IsSingleThread)
- // await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
-
// 初始化操作结果
OperResult