From 49e4d3369acf010b5f0a4a90dcc5cd89fba22cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20P=C4=85gowski?= Date: Wed, 19 Jul 2023 21:12:54 +0200 Subject: [PATCH] Add TimeSpan serialization to Struct --- S7.Net/Types/Struct.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/S7.Net/Types/Struct.cs b/S7.Net/Types/Struct.cs index 1e95508..8fe67fa 100644 --- a/S7.Net/Types/Struct.cs +++ b/S7.Net/Types/Struct.cs @@ -45,6 +45,7 @@ namespace S7.Net.Types break; case "Int32": case "UInt32": + case "TimeSpan": numBytes = Math.Ceiling(numBytes); if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) numBytes++; @@ -215,6 +216,21 @@ namespace S7.Net.Types numBytes += sData.Length; break; + case "TimeSpan": + numBytes = Math.Ceiling(numBytes); + if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) + numBytes++; + + // get the value + info.SetValue(structValue, TimeSpan.FromByteArray(new[] + { + bytes[(int)numBytes + 0], + bytes[(int)numBytes + 1], + bytes[(int)numBytes + 2], + bytes[(int)numBytes + 3] + })); + numBytes += 4; + break; default: var buffer = new byte[GetStructSize(info.FieldType)]; if (buffer.Length == 0) @@ -303,6 +319,9 @@ namespace S7.Net.Types _ => throw new ArgumentException("Please use a valid string type for the S7StringAttribute") }; break; + case "TimeSpan": + bytes2 = TimeSpan.ToByteArray((System.TimeSpan)info.GetValue(structValue)); + break; } if (bytes2 != null) {