From e66d21af05ae73a7a62fb970cec13e027df36ab6 Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Mon, 10 May 2021 20:49:15 +0200 Subject: [PATCH 1/4] Add tests for Boolean.SetBit and Boolean.ClearBit --- S7.Net.UnitTest/TypeTests/BooleanTests.cs | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 S7.Net.UnitTest/TypeTests/BooleanTests.cs diff --git a/S7.Net.UnitTest/TypeTests/BooleanTests.cs b/S7.Net.UnitTest/TypeTests/BooleanTests.cs new file mode 100644 index 00000000..3390b791 --- /dev/null +++ b/S7.Net.UnitTest/TypeTests/BooleanTests.cs @@ -0,0 +1,38 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Boolean = S7.Net.Types.Boolean; + +namespace S7.Net.UnitTest.TypeTests +{ + [TestClass] + public class BooleanTests + { + [DataTestMethod] + [DataRow(0)] + [DataRow(1)] + [DataRow(2)] + [DataRow(3)] + [DataRow(4)] + [DataRow(5)] + [DataRow(6)] + [DataRow(7)] + public void TestValidSetBitValues(int index) + { + Assert.AreEqual(Math.Pow(2, index), Boolean.SetBit(0, index)); + } + + [DataTestMethod] + [DataRow(0)] + [DataRow(1)] + [DataRow(2)] + [DataRow(3)] + [DataRow(4)] + [DataRow(5)] + [DataRow(6)] + [DataRow(7)] + public void TestValidClearBitValues(int index) + { + Assert.AreEqual((byte) ((uint) Math.Pow(2, index) ^ uint.MaxValue), Boolean.ClearBit(byte.MaxValue, index)); + } + } +} From 0b8bd66bf73f6236d18e1284a4a40b6bfadac613 Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Mon, 10 May 2021 20:50:03 +0200 Subject: [PATCH 2/4] Fix bitwise operator in Boolean.ClearBit Fixes #249 --- S7.Net/Types/Boolean.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/S7.Net/Types/Boolean.cs b/S7.Net/Types/Boolean.cs index b83369d4..cfb52f86 100644 --- a/S7.Net/Types/Boolean.cs +++ b/S7.Net/Types/Boolean.cs @@ -26,7 +26,7 @@ public static byte SetBit(byte value, int bit) /// public static byte ClearBit(byte value, int bit) { - return (byte)((value | (~(1 << bit))) & 0xFF); + return (byte)((value & (~(1 << bit))) & 0xFF); } } From 3a794e8a4664d7ecf117b4bc7177ee2db96e78f1 Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Mon, 10 May 2021 21:05:02 +0200 Subject: [PATCH 3/4] Cleanup trailing whitespace in PLC and PLCHelpers --- S7.Net/PLC.cs | 2 +- S7.Net/PLCHelpers.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs index 8882a138..3a73b4cc 100644 --- a/S7.Net/PLC.cs +++ b/S7.Net/PLC.cs @@ -14,7 +14,7 @@ namespace S7.Net public partial class Plc : IDisposable { private const int CONNECTION_TIMED_OUT_ERROR_CODE = 10060; - + //TCP connection to device private TcpClient? tcpClient; private NetworkStream? _stream; diff --git a/S7.Net/PLCHelpers.cs b/S7.Net/PLCHelpers.cs index ff56d270..5b2d77fa 100644 --- a/S7.Net/PLCHelpers.cs +++ b/S7.Net/PLCHelpers.cs @@ -30,8 +30,8 @@ private static void BuildHeaderPackage(System.IO.MemoryStream stream, int amount } /// - /// Create the bytes-package to request data from the PLC. You have to specify the memory type (dataType), - /// the address of the memory, the address of the byte and the bytes count. + /// Create the bytes-package to request data from the PLC. You have to specify the memory type (dataType), + /// the address of the memory, the address of the byte and the bytes count. /// /// MemoryType (DB, Timer, Counter, etc.) /// Address of the memory to be read From aa5028023371710152fd356bd2053d46747d7df9 Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Mon, 10 May 2021 21:14:43 +0200 Subject: [PATCH 4/4] Boolean: Add SetBit and ClearBit by reference --- S7.Net/Types/Boolean.cs | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/S7.Net/Types/Boolean.cs b/S7.Net/Types/Boolean.cs index cfb52f86..f7bc83ea 100644 --- a/S7.Net/Types/Boolean.cs +++ b/S7.Net/Types/Boolean.cs @@ -14,20 +14,51 @@ public static bool GetValue(byte value, int bit) } /// - /// Sets the value of a bit to 1 (true), given the address of the bit + /// Sets the value of a bit to 1 (true), given the address of the bit. Returns + /// a copy of the value with the bit set. /// + /// The input value to modify. + /// The index (zero based) of the bit to set. + /// The modified value with the bit at index set. public static byte SetBit(byte value, int bit) { - return (byte)((value | (1 << bit)) & 0xFF); + SetBit(ref value, bit); + + return value; } /// - /// Resets the value of a bit to 0 (false), given the address of the bit + /// Sets the value of a bit to 1 (true), given the address of the bit. + /// + /// The value to modify. + /// The index (zero based) of the bit to set. + public static void SetBit(ref byte value, int bit) + { + value = (byte) ((value | (1 << bit)) & 0xFF); + } + + /// + /// Resets the value of a bit to 0 (false), given the address of the bit. Returns + /// a copy of the value with the bit cleared. /// + /// The input value to modify. + /// The index (zero based) of the bit to clear. + /// The modified value with the bit at index cleared. public static byte ClearBit(byte value, int bit) { - return (byte)((value & (~(1 << bit))) & 0xFF); + ClearBit(ref value, bit); + + return value; } + /// + /// Resets the value of a bit to 0 (false), given the address of the bit + /// + /// The input value to modify. + /// The index (zero based) of the bit to clear. + public static void ClearBit(ref byte value, int bit) + { + value = (byte) (value & ~(1 << bit) & 0xFF); + } } }