Merge pull request #415 from scamille/fb-fixClassUint32

Fix ReadClass for UInt32
This commit is contained in:
Michael Croes
2022-12-10 21:12:28 +01:00
committed by GitHub
2 changed files with 24 additions and 11 deletions

View File

@@ -17,6 +17,19 @@ namespace S7.Net.UnitTest.TypeTests
Assert.AreEqual(Class.GetClassSize(new TestClassUnevenSize(3, 17)), 10); Assert.AreEqual(Class.GetClassSize(new TestClassUnevenSize(3, 17)), 10);
} }
/// <summary>
/// Ensure Uint32 is correctly parsed through ReadClass functions. Adresses issue https://github.com/S7NetPlus/s7netplus/issues/414
/// </summary>
[TestMethod]
public void TestUint32Read()
{
var result = new TestUint32();
var data = new byte[4] { 0, 0, 0, 5 };
var bytesRead = Class.FromBytes(result, data);
Assert.AreEqual(bytesRead, data.Length);
Assert.AreEqual(5u, result.Value1);
}
private class TestClassUnevenSize private class TestClassUnevenSize
{ {
public bool Bool { get; set; } public bool Bool { get; set; }
@@ -29,5 +42,10 @@ namespace S7.Net.UnitTest.TypeTests
Bools = new bool[bitCount]; Bools = new bool[bitCount];
} }
} }
private class TestUint32
{
public uint Value1 { get; set; }
}
} }
} }

View File

@@ -148,22 +148,17 @@ namespace S7.Net.Types
break; break;
case "Int32": case "Int32":
IncrementToEven(ref numBytes); IncrementToEven(ref numBytes);
// hier auswerten var wordBuffer = new byte[4];
uint sourceUInt = DWord.FromBytes(bytes[(int)numBytes + 3], Array.Copy(bytes, (int)numBytes, wordBuffer, 0, wordBuffer.Length);
bytes[(int)numBytes + 2], uint sourceUInt = DWord.FromByteArray(wordBuffer);
bytes[(int)numBytes + 1],
bytes[(int)numBytes + 0]);
value = sourceUInt.ConvertToInt(); value = sourceUInt.ConvertToInt();
numBytes += 4; numBytes += 4;
break; break;
case "UInt32": case "UInt32":
IncrementToEven(ref numBytes); IncrementToEven(ref numBytes);
// hier auswerten var wordBuffer2 = new byte[4];
value = DWord.FromBytes( Array.Copy(bytes, (int)numBytes, wordBuffer2, 0, wordBuffer2.Length);
bytes[(int)numBytes], value = DWord.FromByteArray(wordBuffer2);
bytes[(int)numBytes + 1],
bytes[(int)numBytes + 2],
bytes[(int)numBytes + 3]);
numBytes += 4; numBytes += 4;
break; break;
case "Single": case "Single":