优化s7读写字符串,更改特殊方法为读写共用

This commit is contained in:
Kimdiego2098
2023-08-16 17:20:20 +08:00
parent 97ab04da91
commit e576f6aed3
4 changed files with 46 additions and 56 deletions

View File

@@ -330,11 +330,6 @@ public class CollectDeviceCore
return ThreadRunReturn.Continue;
}
if (DeviceVariableSourceReads.Count == 0 && Device.DeviceVariableRunTimes.Where(a => string.IsNullOrEmpty(a.OtherMethod)).Any())
{
//无采集变量
return ThreadRunReturn.Continue;
}
if (token.IsCancellationRequested)
return ThreadRunReturn.Break;
@@ -546,6 +541,8 @@ public class CollectDeviceCore
}
else
{
if (strs.Length <= index)
continue;
//得到对于的方法参数值
methodResult.MethodObj[i] = methodResult.Converter.ConvertFrom(strs[index], ps[i].ParameterType);
index++;
@@ -668,18 +665,25 @@ public class CollectDeviceCore
if (method.HasReturn && result != null && result.IsSuccess)
{
var content = deviceVariableMethodSource.Converter.ConvertTo(result.Content?.ToString()?.Replace($"\0", ""));
var operResult = deviceVariableMethodSource.DeviceVariable.SetValue(content);
if (!operResult.IsSuccess)
if (isRead)
{
_logger?.LogWarning(operResult.Message, ToString());
var operResult = deviceVariableMethodSource.DeviceVariable.SetValue(content);
if (!operResult.IsSuccess)
{
_logger?.LogWarning(operResult.Message, ToString());
}
}
}
else
{
var operResult = deviceVariableMethodSource.DeviceVariable.SetValue(null);
if (!operResult.IsSuccess)
if (isRead)
{
_logger?.LogWarning(operResult.Message, ToString());
var operResult = deviceVariableMethodSource.DeviceVariable.SetValue(null);
if (!operResult.IsSuccess)
{
_logger?.LogWarning(operResult.Message, ToString());
}
}
}
return result;

View File

@@ -53,18 +53,19 @@ namespace ThingsGateway.Siemens
}
else if (it.DataTypeEnum.GetSystemType() == typeof(string))
{
lastLen = it.ThingsGatewayBitConverter.StringLength;
if (siemensS7Net.CurrentPlc == SiemensEnum.S200Smart)
{
//字符串在S200Smart中第一个字节不属于实际内容
it.Index += 1;
it.ThingsGatewayBitConverter.StringLength -= 1;
//it.ThingsGatewayBitConverter.StringLength -= 1;
lastLen = it.ThingsGatewayBitConverter.StringLength + 1;
}
else
{
//字符串在S7中前两个字节不属于实际内容
it.Index += 2;
it.ThingsGatewayBitConverter.StringLength -= 2;
//it.ThingsGatewayBitConverter.StringLength -= 2;
lastLen = it.ThingsGatewayBitConverter.StringLength + 2;
}
}
else if (it.DataTypeEnum.GetSystemType() == typeof(object))

View File

@@ -19,6 +19,7 @@ using System.Threading.Tasks;
using ThingsGateway.Application;
using ThingsGateway.Foundation;
using ThingsGateway.Foundation.Extension;
namespace ThingsGateway.Siemens;
/// <summary>
@@ -79,53 +80,48 @@ public abstract class Siemens : CollectBase
/// <see cref="SiemensS7PLC.ReadDateAsync(string,CancellationToken)"/>
/// </summary>
/// <returns></returns>
[DeviceMethod("ReadDate", "")]
public Task<OperResult<System.DateTime>> ReadDateAsync(string address, CancellationToken token)
[DeviceMethod("ReadWriteDateAsync", "")]
public async Task<OperResult<System.DateTime>> ReadWriteDateAsync(string address, System.DateTime? value = null, CancellationToken token = default)
{
return _plc?.ReadDateAsync(address, token);
if (value == null)
return await _plc?.ReadDateAsync(address, token);
else
return (await _plc?.WriteDateAsync(address, value.Value, token)).Copy<System.DateTime>();
}
/// <summary>
/// <see cref="SiemensS7PLC.ReadDateTimeAsync(string,CancellationToken)"/>
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <param name="token"></param>
/// <returns></returns>
[DeviceMethod("ReadDateTime", "")]
public Task<OperResult<System.DateTime>> ReadDateTimeAsync(string address, CancellationToken token)
[DeviceMethod("ReadWriteDateTimeAsync", "")]
public async Task<OperResult<System.DateTime>> ReadWriteDateTimeAsync(string address, System.DateTime? value = null, CancellationToken token = default)
{
return _plc?.ReadDateTimeAsync(address, token);
if (value == null)
return await _plc?.ReadDateTimeAsync(address, token);
else
return (await _plc?.WriteDateTimeAsync(address, value.Value, token)).Copy<System.DateTime>();
}
/// <summary>
/// <see cref="SiemensS7PLC.ReadStringAsync(string,Encoding,CancellationToken)"/>
/// </summary>
/// <returns></returns>
[DeviceMethod("ReadString", "")]
public Task<OperResult<string>> ReadStringAsync(string address, Encoding encoding, CancellationToken token)
[DeviceMethod("ReadWriteStringAsync", "")]
public async Task<OperResult<string>> ReadWriteStringAsync(string address, Encoding encoding, string value = null, CancellationToken token = default)
{
return _plc?.ReadStringAsync(address, encoding, token);
if (value == null)
return await _plc?.ReadStringAsync(address, encoding, token);
else
return (await _plc?.WriteAsync(address, value, token)).Copy<string>();
}
/// <summary>
/// <see cref="SiemensS7PLC.WriteDateAsync(string,System.DateTime,CancellationToken)"/>
/// </summary>
/// <returns></returns>
[DeviceMethod("WriteDate", "")]
public Task<OperResult> WriteDateAsync(string address, System.DateTime dateTime, CancellationToken token)
{
return _plc?.WriteDateAsync(address, dateTime, token);
}
/// <summary>
/// <see cref="SiemensS7PLC.WriteDateTimeAsync(string,System.DateTime,CancellationToken)"/>
/// </summary>
/// <returns></returns>
[DeviceMethod("WriteDateTime", "")]
public Task<OperResult> WriteDateTimeAsync(string address, System.DateTime dateTime, CancellationToken token)
{
return _plc?.WriteDateTimeAsync(address, dateTime, token);
}
/// <inheritdoc/>
public override async Task<OperResult> WriteValueAsync(DeviceVariableRunTime deviceVariable, JToken value, CancellationToken token)

View File

@@ -117,38 +117,27 @@
<member name="M:ThingsGateway.Siemens.Siemens.LoadSourceRead(System.Collections.Generic.List{ThingsGateway.Application.DeviceVariableRunTime})">
<inheritdoc/>
</member>
<member name="M:ThingsGateway.Siemens.Siemens.ReadDateAsync(System.String,System.Threading.CancellationToken)">
<member name="M:ThingsGateway.Siemens.Siemens.ReadWriteDateAsync(System.String,System.Nullable{System.DateTime},System.Threading.CancellationToken)">
<summary>
<see cref="M:ThingsGateway.Foundation.Adapter.Siemens.SiemensS7PLC.ReadDateAsync(System.String,System.Threading.CancellationToken)"/>
</summary>
<returns></returns>
</member>
<member name="M:ThingsGateway.Siemens.Siemens.ReadDateTimeAsync(System.String,System.Threading.CancellationToken)">
<member name="M:ThingsGateway.Siemens.Siemens.ReadWriteDateTimeAsync(System.String,System.Nullable{System.DateTime},System.Threading.CancellationToken)">
<summary>
<see cref="M:ThingsGateway.Foundation.Adapter.Siemens.SiemensS7PLC.ReadDateTimeAsync(System.String,System.Threading.CancellationToken)"/>
</summary>
<param name="address"></param>
<param name="value"></param>
<param name="token"></param>
<returns></returns>
</member>
<member name="M:ThingsGateway.Siemens.Siemens.ReadStringAsync(System.String,System.Text.Encoding,System.Threading.CancellationToken)">
<member name="M:ThingsGateway.Siemens.Siemens.ReadWriteStringAsync(System.String,System.Text.Encoding,System.String,System.Threading.CancellationToken)">
<summary>
<see cref="M:ThingsGateway.Foundation.Adapter.Siemens.SiemensS7PLC.ReadStringAsync(System.String,System.Text.Encoding,System.Threading.CancellationToken)"/>
</summary>
<returns></returns>
</member>
<member name="M:ThingsGateway.Siemens.Siemens.WriteDateAsync(System.String,System.DateTime,System.Threading.CancellationToken)">
<summary>
<see cref="M:ThingsGateway.Foundation.Adapter.Siemens.SiemensS7PLC.WriteDateAsync(System.String,System.DateTime,System.Threading.CancellationToken)"/>
</summary>
<returns></returns>
</member>
<member name="M:ThingsGateway.Siemens.Siemens.WriteDateTimeAsync(System.String,System.DateTime,System.Threading.CancellationToken)">
<summary>
<see cref="M:ThingsGateway.Foundation.Adapter.Siemens.SiemensS7PLC.WriteDateTimeAsync(System.String,System.DateTime,System.Threading.CancellationToken)"/>
</summary>
<returns></returns>
</member>
<member name="M:ThingsGateway.Siemens.Siemens.WriteValueAsync(ThingsGateway.Application.DeviceVariableRunTime,Newtonsoft.Json.Linq.JToken,System.Threading.CancellationToken)">
<inheritdoc/>
</member>