Skip to content

Commit

Permalink
Merge pull request #391 from mycroes/set-bit-clear-bit
Browse files Browse the repository at this point in the history
SetBit and ClearBit tests, fixes
  • Loading branch information
mycroes authored May 10, 2021
2 parents 44ee651 + aa50280 commit 632e1c1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
38 changes: 38 additions & 0 deletions S7.Net.UnitTest/TypeTests/BooleanTests.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
2 changes: 1 addition & 1 deletion S7.Net/PLC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions S7.Net/PLCHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ private static void BuildHeaderPackage(System.IO.MemoryStream stream, int amount
}

/// <summary>
/// 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.
/// </summary>
/// <param name="dataType">MemoryType (DB, Timer, Counter, etc.)</param>
/// <param name="db">Address of the memory to be read</param>
Expand Down
39 changes: 35 additions & 4 deletions S7.Net/Types/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,51 @@ public static bool GetValue(byte value, int bit)
}

/// <summary>
/// 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.
/// </summary>
/// <param name="value">The input value to modify.</param>
/// <param name="bit">The index (zero based) of the bit to set.</param>
/// <returns>The modified value with the bit at index set.</returns>
public static byte SetBit(byte value, int bit)
{
return (byte)((value | (1 << bit)) & 0xFF);
SetBit(ref value, bit);

return value;
}

/// <summary>
/// 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.
/// </summary>
/// <param name="value">The value to modify.</param>
/// <param name="bit">The index (zero based) of the bit to set.</param>
public static void SetBit(ref byte value, int bit)
{
value = (byte) ((value | (1 << bit)) & 0xFF);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="value">The input value to modify.</param>
/// <param name="bit">The index (zero based) of the bit to clear.</param>
/// <returns>The modified value with the bit at index cleared.</returns>
public static byte ClearBit(byte value, int bit)
{
return (byte)((value | (~(1 << bit))) & 0xFF);
ClearBit(ref value, bit);

return value;
}

/// <summary>
/// Resets the value of a bit to 0 (false), given the address of the bit
/// </summary>
/// <param name="value">The input value to modify.</param>
/// <param name="bit">The index (zero based) of the bit to clear.</param>
public static void ClearBit(ref byte value, int bit)
{
value = (byte) (value & ~(1 << bit) & 0xFF);
}
}
}

0 comments on commit 632e1c1

Please sign in to comment.