ToDecimal方法添加异步捕获,防止double类型值超限

This commit is contained in:
Diego
2025-07-08 11:21:06 +08:00
parent 4c62bb0b21
commit 34120da008
2 changed files with 16 additions and 6 deletions

View File

@@ -466,7 +466,14 @@ public class DefaultConvert
if (value is Double d)
{
return Double.IsNaN(d) ? defaultValue : (Decimal)d;
try
{
return Double.IsNaN(d) ? defaultValue : (Decimal)d;
}
catch
{
return defaultValue;
}
}
try

View File

@@ -30,7 +30,7 @@ public class ModbusSlave : BusinessBase
{
private readonly ModbusSlaveProperty _driverPropertys = new();
private readonly ConcurrentDictionary<string, VariableRuntime> ModbusVariableQueue = new();
private readonly ConcurrentDictionary<string, long> ModbusVariableQueue = new();
private readonly ModbusSlaveVariableProperty _variablePropertys = new();
@@ -177,14 +177,17 @@ public class ModbusSlave : BusinessBase
{
if (cancellationToken.IsCancellationRequested)
break;
var type = item.Value.GetPropertyValue(CurrentDevice.Id, nameof(ModbusSlaveVariableProperty.DataType));
if (!IdVariableRuntimes.TryGetValue(item.Value, out var variableRuntime))
break;
var type = variableRuntime.GetPropertyValue(CurrentDevice.Id, nameof(ModbusSlaveVariableProperty.DataType));
if (Enum.TryParse(type, out DataTypeEnum result))
{
await _plc.WriteAsync(item.Key, JToken.FromObject(item.Value.Value), result, cancellationToken).ConfigureAwait(false);
await _plc.WriteAsync(item.Key, JToken.FromObject(variableRuntime.Value), result, cancellationToken).ConfigureAwait(false);
}
else
{
await _plc.WriteAsync(item.Key, JToken.FromObject(item.Value.Value), item.Value.DataType, cancellationToken).ConfigureAwait(false);
await _plc.WriteAsync(item.Key, JToken.FromObject(variableRuntime.Value), variableRuntime.DataType, cancellationToken).ConfigureAwait(false);
}
}
@@ -252,7 +255,7 @@ public class ModbusSlave : BusinessBase
var address = variableRuntime.GetPropertyValue(DeviceId, nameof(_variablePropertys.ServiceAddress));
if (address != null && variableRuntime.Value != null)
{
ModbusVariableQueue?.AddOrUpdate(address, address => variableRuntime, (address, addvalue) => variableRuntime);
ModbusVariableQueue?.AddOrUpdate(address, address => variableRuntime.Id, (address, addvalue) => variableRuntime.Id);
}
}