From e69ba85bcb3a6877c52a03157aa3636b69c2ed10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E8=B6=85?= Date: Wed, 17 Jul 2024 12:14:56 +0800 Subject: [PATCH 1/3] Code error for calculating structure length old code: return (int)numBytes; new code: return (int)Math.Ceiling(numBytes); //For example: 9 bool data, length = 1.125, but actually occupies 2 bytes --- S7.Net/Types/Struct.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/S7.Net/Types/Struct.cs b/S7.Net/Types/Struct.cs index 136638ac..a2381d14 100644 --- a/S7.Net/Types/Struct.cs +++ b/S7.Net/Types/Struct.cs @@ -78,8 +78,8 @@ public static int GetStructSize(Type structType) break; } } - return (int)numBytes; - } + return (int)Math.Ceiling(numBytes); //For example: 9 bool data, length = 1.125, but actually occupies 2 bytes + } /// /// Creates a struct of a specified type by an array of bytes. From 4b6ec7a3d48694cb7d7b1434302dd78e41168589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E8=B6=85?= Date: Wed, 17 Jul 2024 13:45:03 +0800 Subject: [PATCH 2/3] Two byte alignment issue in the structure --- S7.Net/Types/Struct.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/S7.Net/Types/Struct.cs b/S7.Net/Types/Struct.cs index a2381d14..601b0d35 100644 --- a/S7.Net/Types/Struct.cs +++ b/S7.Net/Types/Struct.cs @@ -24,10 +24,11 @@ public static int GetStructSize(Type structType) #else .GetFields(); #endif - - foreach (var info in infos) + int count = 0; + foreach (var info in infos) { - switch (info.FieldType.Name) + count++; + switch (info.FieldType.Name) { case "Boolean": numBytes += 0.125; @@ -75,6 +76,10 @@ public static int GetStructSize(Type structType) break; default: numBytes += GetStructSize(info.FieldType); + if (count != infos.Count()) { + if(numBytes % 2 != 0) + numBytes++;//word align + } break; } } @@ -238,6 +243,8 @@ public static int GetStructSize(Type structType) Buffer.BlockCopy(bytes, (int)Math.Ceiling(numBytes), buffer, 0, buffer.Length); info.SetValue(structValue, FromBytes(info.FieldType, buffer)); numBytes += buffer.Length; + if(numBytes %2 != 0) //word align + numBytes++; break; } } From 8d95d4538bfa66a8b214604c53ed6849efed3e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E8=B6=85?= Date: Thu, 18 Jul 2024 15:02:31 +0800 Subject: [PATCH 3/3] NetStandard1.3 is no longer supported. Support for enumeration types in structures --- S7.Net/S7.Net.csproj | 2 +- S7.Net/Types/Struct.cs | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/S7.Net/S7.Net.csproj b/S7.Net/S7.Net.csproj index a5643f5d..228d5677 100644 --- a/S7.Net/S7.Net.csproj +++ b/S7.Net/S7.Net.csproj @@ -1,7 +1,7 @@  - net452;net462;netstandard2.0;netstandard1.3;net5.0;net6.0;net7.0 + net452;net462;netstandard2.0;net5.0;net6.0;net7.0 true Properties\S7.Net.snk S7.Net.UnitTest diff --git a/S7.Net/Types/Struct.cs b/S7.Net/Types/Struct.cs index 601b0d35..c2f08193 100644 --- a/S7.Net/Types/Struct.cs +++ b/S7.Net/Types/Struct.cs @@ -9,12 +9,12 @@ namespace S7.Net.Types /// public static class Struct { - /// - /// Gets the size of the struct in bytes. - /// - /// the type of the struct - /// the number of bytes - public static int GetStructSize(Type structType) + /// + /// Gets the size of the struct in bytes. + /// + /// the type of the struct + /// the number of bytes + public static int GetStructSize(Type structType) { double numBytes = 0.0; @@ -28,7 +28,11 @@ public static int GetStructSize(Type structType) foreach (var info in infos) { count++; - switch (info.FieldType.Name) + var type = info.FieldType; + string name = info.FieldType.Name; + if (type.BaseType == typeof(System.Enum)) + name = Enum.GetUnderlyingType(type).Name; + switch (name) { case "Boolean": numBytes += 0.125; @@ -116,7 +120,11 @@ public static int GetStructSize(Type structType) foreach (var info in infos) { - switch (info.FieldType.Name) + var type = info.FieldType; + string name = info.FieldType.Name; + if (type.BaseType == typeof(System.Enum)) + name = Enum.GetUnderlyingType(type).Name; + switch (name) { case "Boolean": // get the value