mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 14:28:25 +08:00
Merge pull request #415 from scamille/fb-fixClassUint32
Fix ReadClass for UInt32
This commit is contained in:
@@ -17,6 +17,19 @@ namespace S7.Net.UnitTest.TypeTests
|
||||
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
|
||||
{
|
||||
public bool Bool { get; set; }
|
||||
@@ -29,5 +42,10 @@ namespace S7.Net.UnitTest.TypeTests
|
||||
Bools = new bool[bitCount];
|
||||
}
|
||||
}
|
||||
|
||||
private class TestUint32
|
||||
{
|
||||
public uint Value1 { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,22 +148,17 @@ namespace S7.Net.Types
|
||||
break;
|
||||
case "Int32":
|
||||
IncrementToEven(ref numBytes);
|
||||
// hier auswerten
|
||||
uint sourceUInt = DWord.FromBytes(bytes[(int)numBytes + 3],
|
||||
bytes[(int)numBytes + 2],
|
||||
bytes[(int)numBytes + 1],
|
||||
bytes[(int)numBytes + 0]);
|
||||
var wordBuffer = new byte[4];
|
||||
Array.Copy(bytes, (int)numBytes, wordBuffer, 0, wordBuffer.Length);
|
||||
uint sourceUInt = DWord.FromByteArray(wordBuffer);
|
||||
value = sourceUInt.ConvertToInt();
|
||||
numBytes += 4;
|
||||
break;
|
||||
case "UInt32":
|
||||
IncrementToEven(ref numBytes);
|
||||
// hier auswerten
|
||||
value = DWord.FromBytes(
|
||||
bytes[(int)numBytes],
|
||||
bytes[(int)numBytes + 1],
|
||||
bytes[(int)numBytes + 2],
|
||||
bytes[(int)numBytes + 3]);
|
||||
var wordBuffer2 = new byte[4];
|
||||
Array.Copy(bytes, (int)numBytes, wordBuffer2, 0, wordBuffer2.Length);
|
||||
value = DWord.FromByteArray(wordBuffer2);
|
||||
numBytes += 4;
|
||||
break;
|
||||
case "Single":
|
||||
|
||||
Reference in New Issue
Block a user