Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions S7.Net.UnitTest/Helpers/TestClassUnevenSize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S7.Net.UnitTest.Helpers
{
public class TestClassUnevenSize
{
public bool Bool { get; set; }
public byte[] Bytes { get; set; }
public bool[] Bools { get; set; }

public TestClassUnevenSize(int byteCount, int bitCount)
{
Bytes = new byte[byteCount];
Bools = new bool[bitCount];
}
}
}
2 changes: 2 additions & 0 deletions S7.Net.UnitTest/S7.Net.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<ItemGroup>
<Compile Include="ConnectionRequestTest.cs" />
<Compile Include="ConvertersUnitTest.cs" />
<Compile Include="Helpers\TestClassUnevenSize.cs" />
<Compile Include="Helpers\TestClassWithNestedClass.cs" />
<Compile Include="ProtocolTests.cs" />
<Compile Include="Helpers\ConsoleManager.cs" />
Expand All @@ -79,6 +80,7 @@
<Compile Include="S7NetTestsSync.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Helpers\TestLongStruct.cs" />
<Compile Include="TypeTests\ClassTests.cs" />
<Compile Include="TypeTests\StringExTests.cs" />
<Compile Include="TypeTests\StringTests.cs" />
</ItemGroup>
Expand Down
26 changes: 26 additions & 0 deletions S7.Net.UnitTest/TypeTests/ClassTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using S7.Net.Types;
using S7.Net.UnitTest.Helpers;

namespace S7.Net.UnitTest.TypeTests
{
[TestClass]
public class ClassTests
{
[TestMethod]
public void GetClassSizeTest()
{
Assert.AreEqual(Class.GetClassSize(new TestClassUnevenSize(1, 1)), 6);
Assert.AreEqual(Class.GetClassSize(new TestClassUnevenSize(2, 15)), 6);
Assert.AreEqual(Class.GetClassSize(new TestClassUnevenSize(2, 16)), 6);
Assert.AreEqual(Class.GetClassSize(new TestClassUnevenSize(2, 17)), 8);
Assert.AreEqual(Class.GetClassSize(new TestClassUnevenSize(3, 15)), 8);
Assert.AreEqual(Class.GetClassSize(new TestClassUnevenSize(3, 17)), 10);
}
}
}
23 changes: 15 additions & 8 deletions S7.Net/Types/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public static double GetClassSize(object instance, double numBytes = 0.0, bool i
throw new Exception("Cannot determine size of class, because an array is defined which has no fixed size greater than zero.");
}

// Array starts at even byte adress
numBytes = IncreaseToEvenNumber(numBytes);
for (int i = 0; i < array.Length; i++)
{
numBytes = GetIncreasedNumberOfBytes(numBytes, elementType);
Expand All @@ -95,14 +97,9 @@ public static double GetClassSize(object instance, double numBytes = 0.0, bool i
numBytes = GetIncreasedNumberOfBytes(numBytes, property.PropertyType);
}
}
if (false == isInnerProperty)
{
// enlarge numBytes to next even number because S7-Structs in a DB always will be resized to an even byte count
numBytes = Math.Ceiling(numBytes);
if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0)
numBytes++;
}
return numBytes;
// enlarge numBytes to next even number because S7-Structs in a DB always will be resized to an even byte count
numBytes = IncreaseToEvenNumber(numBytes);
return (int)numBytes;
}

private static object GetPropertyValue(Type propertyType, byte[] bytes, ref double numBytes)
Expand Down Expand Up @@ -219,6 +216,7 @@ public static double FromBytes(object sourceClass, byte[] bytes, double numBytes
if (property.PropertyType.IsArray)
{
Array array = (Array)property.GetValue(sourceClass, null);
numBytes = IncreaseToEvenNumber(numBytes);
Type elementType = property.PropertyType.GetElementType();
for (int i = 0; i < array.Length && numBytes < bytes.Length; i++)
{
Expand Down Expand Up @@ -313,6 +311,7 @@ public static double ToBytes(object sourceClass, byte[] bytes, double numBytes =
{
if (property.PropertyType.IsArray)
{
numBytes = IncreaseToEvenNumber(numBytes);
Array array = (Array)property.GetValue(sourceClass, null);
Type elementType = property.PropertyType.GetElementType();
for (int i = 0; i < array.Length && numBytes < bytes.Length; i++)
Expand All @@ -327,5 +326,13 @@ public static double ToBytes(object sourceClass, byte[] bytes, double numBytes =
}
return numBytes;
}

private static double IncreaseToEvenNumber(double numBytes)
{
var numBytesInt = (int)Math.Ceiling(numBytes);
if (numBytesInt % 2 > 0)
numBytesInt++;
return numBytesInt;
}
}
}