From 3be1c9437aae972e1746696ece06c2774df90d5c Mon Sep 17 00:00:00 2001 From: timyhac Date: Sun, 23 Jun 2024 08:12:45 +1000 Subject: [PATCH 1/5] Allows AutoSyncReadInterval and AutoSyncWriteInterval to be set after Tag is initialized --- src/libplctag/NativeTagWrapper.cs | 64 ++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/src/libplctag/NativeTagWrapper.cs b/src/libplctag/NativeTagWrapper.cs index 5cb7427..85bdc02 100644 --- a/src/libplctag/NativeTagWrapper.cs +++ b/src/libplctag/NativeTagWrapper.cs @@ -157,15 +157,59 @@ public TimeSpan Timeout private TimeSpan? _autoSyncReadInterval; public TimeSpan? AutoSyncReadInterval { - get => GetField(ref _autoSyncReadInterval); - set => SetField(ref _autoSyncReadInterval, value); + get + { + ThrowIfAlreadyDisposed(); + + if(!_isInitialized) + return _autoSyncReadInterval; + + return TimeSpan.FromMilliseconds(GetIntAttribute("auto_sync_read_ms")); + } + set + { + ThrowIfAlreadyDisposed(); + + if (!_isInitialized) + { + _autoSyncReadInterval = value; + return; + } + + if (value is null) + SetIntAttribute("auto_sync_read_ms", 0); // 0 is a special value that turns off auto sync + else + SetIntAttribute("auto_sync_read_ms", (int)value.Value.TotalMilliseconds); + } } private TimeSpan? _autoSyncWriteInterval; public TimeSpan? AutoSyncWriteInterval { - get => GetField(ref _autoSyncWriteInterval); - set => SetField(ref _autoSyncWriteInterval, value); + get + { + ThrowIfAlreadyDisposed(); + + if (!_isInitialized) + return _autoSyncWriteInterval; + + return TimeSpan.FromMilliseconds(GetIntAttribute("auto_sync_write_ms")); + } + set + { + ThrowIfAlreadyDisposed(); + + if (!_isInitialized) + { + _autoSyncWriteInterval = value; + return; + } + + if (value is null) + SetIntAttribute("auto_sync_write_ms", 0); // 0 is a special value that turns off auto sync + else + SetIntAttribute("auto_sync_write_ms", (int)value.Value.TotalMilliseconds); + } } private DebugLevel _debugLevel = DebugLevel.None; @@ -700,6 +744,14 @@ string FormatPlcType(PlcType? type) return type?.ToString().ToLowerInvariant(); } + string FormatTimeSpan(TimeSpan? timespan) + { + if(timespan.HasValue) + return ((int)timespan.Value.TotalMilliseconds).ToString(); + else + return null; + } + var attributes = new Dictionary { { "protocol", Protocol?.ToString() }, @@ -712,8 +764,8 @@ string FormatPlcType(PlcType? type) { "read_cache_ms", ReadCacheMillisecondDuration?.ToString() }, { "use_connected_msg", FormatNullableBoolean(UseConnectedMessaging) }, { "allow_packing", FormatNullableBoolean(AllowPacking) }, - { "auto_sync_read_ms", AutoSyncReadInterval?.TotalMilliseconds.ToString() }, - { "auto_sync_write_ms", AutoSyncWriteInterval?.TotalMilliseconds.ToString() }, + { "auto_sync_read_ms", FormatTimeSpan(AutoSyncReadInterval) }, + { "auto_sync_write_ms", FormatTimeSpan(AutoSyncWriteInterval) }, { "debug", DebugLevel == DebugLevel.None ? null : ((int)DebugLevel).ToString() }, { "int16_byte_order", Int16ByteOrder }, { "int32_byte_order", Int32ByteOrder }, From 86d7a661137bdcc33f17392cb2e21c2f67086ada Mon Sep 17 00:00:00 2001 From: timyhac Date: Sun, 23 Jun 2024 08:52:11 +1000 Subject: [PATCH 2/5] Fix subtle bug that occurs if SetIntAttribute fails. --- src/libplctag/NativeTagWrapper.cs | 55 +++++++++++++++---------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/src/libplctag/NativeTagWrapper.cs b/src/libplctag/NativeTagWrapper.cs index 85bdc02..80c360c 100644 --- a/src/libplctag/NativeTagWrapper.cs +++ b/src/libplctag/NativeTagWrapper.cs @@ -118,22 +118,21 @@ public int? ReadCacheMillisecondDuration { ThrowIfAlreadyDisposed(); - if (!_isInitialized) + if (_isInitialized) + return GetIntAttribute("read_cache_ms"); + else return _readCacheMillisecondDuration; - return GetIntAttribute("read_cache_ms"); } set { ThrowIfAlreadyDisposed(); - if (!_isInitialized) - { + if (_isInitialized) + SetIntAttribute("read_cache_ms", value.Value); + else _readCacheMillisecondDuration = value; - return; - } - SetIntAttribute("read_cache_ms", value.Value); } } @@ -161,25 +160,24 @@ public TimeSpan? AutoSyncReadInterval { ThrowIfAlreadyDisposed(); - if(!_isInitialized) + if(_isInitialized) + return TimeSpan.FromMilliseconds(GetIntAttribute("auto_sync_read_ms")); + else return _autoSyncReadInterval; - - return TimeSpan.FromMilliseconds(GetIntAttribute("auto_sync_read_ms")); } set { ThrowIfAlreadyDisposed(); - if (!_isInitialized) + if (_isInitialized) { - _autoSyncReadInterval = value; - return; + if (value is null) + SetIntAttribute("auto_sync_read_ms", 0); // 0 is a special value that turns off auto sync + else + SetIntAttribute("auto_sync_read_ms", (int)value.Value.TotalMilliseconds); } - - if (value is null) - SetIntAttribute("auto_sync_read_ms", 0); // 0 is a special value that turns off auto sync - else - SetIntAttribute("auto_sync_read_ms", (int)value.Value.TotalMilliseconds); + + _autoSyncReadInterval = value; } } @@ -190,25 +188,24 @@ public TimeSpan? AutoSyncWriteInterval { ThrowIfAlreadyDisposed(); - if (!_isInitialized) + if (_isInitialized) + return TimeSpan.FromMilliseconds(GetIntAttribute("auto_sync_write_ms")); + else return _autoSyncWriteInterval; - - return TimeSpan.FromMilliseconds(GetIntAttribute("auto_sync_write_ms")); } set { ThrowIfAlreadyDisposed(); - if (!_isInitialized) + if (_isInitialized) { - _autoSyncWriteInterval = value; - return; + if (value is null) + SetIntAttribute("auto_sync_write_ms", 0); // 0 is a special value that turns off auto sync + else + SetIntAttribute("auto_sync_write_ms", (int)value.Value.TotalMilliseconds); } - - if (value is null) - SetIntAttribute("auto_sync_write_ms", 0); // 0 is a special value that turns off auto sync - else - SetIntAttribute("auto_sync_write_ms", (int)value.Value.TotalMilliseconds); + + _autoSyncWriteInterval = value; } } From d04d3e17be73e806a80baa40d1193672ad981899 Mon Sep 17 00:00:00 2001 From: timyhac Date: Sun, 23 Jun 2024 08:53:13 +1000 Subject: [PATCH 3/5] Added comments --- src/libplctag/NativeTagWrapper.cs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/libplctag/NativeTagWrapper.cs b/src/libplctag/NativeTagWrapper.cs index 80c360c..6de48f8 100644 --- a/src/libplctag/NativeTagWrapper.cs +++ b/src/libplctag/NativeTagWrapper.cs @@ -159,11 +159,7 @@ public TimeSpan? AutoSyncReadInterval get { ThrowIfAlreadyDisposed(); - - if(_isInitialized) - return TimeSpan.FromMilliseconds(GetIntAttribute("auto_sync_read_ms")); - else - return _autoSyncReadInterval; + return _autoSyncReadInterval; } set { @@ -177,6 +173,8 @@ public TimeSpan? AutoSyncReadInterval SetIntAttribute("auto_sync_read_ms", (int)value.Value.TotalMilliseconds); } + // Set after writing to underlying tag in case SetIntAttribute fails. + // Ensures the two have the same value. _autoSyncReadInterval = value; } } @@ -187,11 +185,7 @@ public TimeSpan? AutoSyncWriteInterval get { ThrowIfAlreadyDisposed(); - - if (_isInitialized) - return TimeSpan.FromMilliseconds(GetIntAttribute("auto_sync_write_ms")); - else - return _autoSyncWriteInterval; + return _autoSyncWriteInterval; } set { @@ -205,6 +199,8 @@ public TimeSpan? AutoSyncWriteInterval SetIntAttribute("auto_sync_write_ms", (int)value.Value.TotalMilliseconds); } + // Set after writing to underlying tag in case SetIntAttribute fails. + // Ensures the two have the same value. _autoSyncWriteInterval = value; } } From 1f4b3d5f7a6be66940507a4fbfa1d63c196a79af Mon Sep 17 00:00:00 2001 From: timyhac Date: Sun, 23 Jun 2024 08:54:57 +1000 Subject: [PATCH 4/5] And apply same treatment to ReadCacheDuration and DebugLevel --- src/libplctag/NativeTagWrapper.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/libplctag/NativeTagWrapper.cs b/src/libplctag/NativeTagWrapper.cs index 6de48f8..7fda00b 100644 --- a/src/libplctag/NativeTagWrapper.cs +++ b/src/libplctag/NativeTagWrapper.cs @@ -117,11 +117,7 @@ public int? ReadCacheMillisecondDuration get { ThrowIfAlreadyDisposed(); - - if (_isInitialized) - return GetIntAttribute("read_cache_ms"); - else - return _readCacheMillisecondDuration; + return _readCacheMillisecondDuration; } set @@ -130,8 +126,8 @@ public int? ReadCacheMillisecondDuration if (_isInitialized) SetIntAttribute("read_cache_ms", value.Value); - else - _readCacheMillisecondDuration = value; + + _readCacheMillisecondDuration = value; } } @@ -211,17 +207,16 @@ public DebugLevel DebugLevel get { ThrowIfAlreadyDisposed(); - return _debugLevel; } set { ThrowIfAlreadyDisposed(); - _debugLevel = value; - if (_isInitialized) SetDebugLevel(value); + + _debugLevel = value; } } From 2bb1e595c1bff046dd6ac7ceedb1f9e8a5ca221c Mon Sep 17 00:00:00 2001 From: timyhac Date: Sun, 23 Jun 2024 08:58:03 +1000 Subject: [PATCH 5/5] Added more comments --- src/libplctag/NativeTagWrapper.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libplctag/NativeTagWrapper.cs b/src/libplctag/NativeTagWrapper.cs index 7fda00b..27e628f 100644 --- a/src/libplctag/NativeTagWrapper.cs +++ b/src/libplctag/NativeTagWrapper.cs @@ -127,6 +127,8 @@ public int? ReadCacheMillisecondDuration if (_isInitialized) SetIntAttribute("read_cache_ms", value.Value); + // Set after writing to underlying tag in case SetIntAttribute fails. + // Ensures the two have the same value. _readCacheMillisecondDuration = value; } @@ -216,6 +218,8 @@ public DebugLevel DebugLevel if (_isInitialized) SetDebugLevel(value); + // Set after writing to underlying tag in case SetDebugLevel fails. + // Ensures the two have the same value. _debugLevel = value; } }