Skip to content

Commit 6aa0133

Browse files
authored
Merge pull request #415 from scamille/fb-fixClassUint32
Fix ReadClass for UInt32
2 parents 3833d29 + 868e719 commit 6aa0133

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

S7.Net.UnitTest/TypeTests/ClassTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ public void GetClassSizeTest()
1717
Assert.AreEqual(Class.GetClassSize(new TestClassUnevenSize(3, 17)), 10);
1818
}
1919

20+
/// <summary>
21+
/// Ensure Uint32 is correctly parsed through ReadClass functions. Adresses issue https://github.com/S7NetPlus/s7netplus/issues/414
22+
/// </summary>
23+
[TestMethod]
24+
public void TestUint32Read()
25+
{
26+
var result = new TestUint32();
27+
var data = new byte[4] { 0, 0, 0, 5 };
28+
var bytesRead = Class.FromBytes(result, data);
29+
Assert.AreEqual(bytesRead, data.Length);
30+
Assert.AreEqual(5u, result.Value1);
31+
}
32+
2033
private class TestClassUnevenSize
2134
{
2235
public bool Bool { get; set; }
@@ -29,5 +42,10 @@ public TestClassUnevenSize(int byteCount, int bitCount)
2942
Bools = new bool[bitCount];
3043
}
3144
}
45+
46+
private class TestUint32
47+
{
48+
public uint Value1 { get; set; }
49+
}
3250
}
3351
}

S7.Net/Types/Class.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,17 @@ public static double GetClassSize(object instance, double numBytes = 0.0, bool i
148148
break;
149149
case "Int32":
150150
IncrementToEven(ref numBytes);
151-
// hier auswerten
152-
uint sourceUInt = DWord.FromBytes(bytes[(int)numBytes + 3],
153-
bytes[(int)numBytes + 2],
154-
bytes[(int)numBytes + 1],
155-
bytes[(int)numBytes + 0]);
151+
var wordBuffer = new byte[4];
152+
Array.Copy(bytes, (int)numBytes, wordBuffer, 0, wordBuffer.Length);
153+
uint sourceUInt = DWord.FromByteArray(wordBuffer);
156154
value = sourceUInt.ConvertToInt();
157155
numBytes += 4;
158156
break;
159157
case "UInt32":
160158
IncrementToEven(ref numBytes);
161-
// hier auswerten
162-
value = DWord.FromBytes(
163-
bytes[(int)numBytes],
164-
bytes[(int)numBytes + 1],
165-
bytes[(int)numBytes + 2],
166-
bytes[(int)numBytes + 3]);
159+
var wordBuffer2 = new byte[4];
160+
Array.Copy(bytes, (int)numBytes, wordBuffer2, 0, wordBuffer2.Length);
161+
value = DWord.FromByteArray(wordBuffer2);
167162
numBytes += 4;
168163
break;
169164
case "Single":

0 commit comments

Comments
 (0)