Skip to content

Commit ced10b4

Browse files
committed
Release S7NetPlus 0.11.0
Release highlights: - Fix for byte length calculation of bits - Fix for Boolean.ClearBit - Added Boolean.ClearBit and .SetBit by reference
2 parents d10c15b + 632e1c1 commit ced10b4

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Boolean = S7.Net.Types.Boolean;
4+
5+
namespace S7.Net.UnitTest.TypeTests
6+
{
7+
[TestClass]
8+
public class BooleanTests
9+
{
10+
[DataTestMethod]
11+
[DataRow(0)]
12+
[DataRow(1)]
13+
[DataRow(2)]
14+
[DataRow(3)]
15+
[DataRow(4)]
16+
[DataRow(5)]
17+
[DataRow(6)]
18+
[DataRow(7)]
19+
public void TestValidSetBitValues(int index)
20+
{
21+
Assert.AreEqual(Math.Pow(2, index), Boolean.SetBit(0, index));
22+
}
23+
24+
[DataTestMethod]
25+
[DataRow(0)]
26+
[DataRow(1)]
27+
[DataRow(2)]
28+
[DataRow(3)]
29+
[DataRow(4)]
30+
[DataRow(5)]
31+
[DataRow(6)]
32+
[DataRow(7)]
33+
public void TestValidClearBitValues(int index)
34+
{
35+
Assert.AreEqual((byte) ((uint) Math.Pow(2, index) ^ uint.MaxValue), Boolean.ClearBit(byte.MaxValue, index));
36+
}
37+
}
38+
}

S7.Net/PLC.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace S7.Net
1414
public partial class Plc : IDisposable
1515
{
1616
private const int CONNECTION_TIMED_OUT_ERROR_CODE = 10060;
17-
17+
1818
//TCP connection to device
1919
private TcpClient? tcpClient;
2020
private NetworkStream? _stream;

S7.Net/PLCHelpers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ private static void BuildHeaderPackage(System.IO.MemoryStream stream, int amount
3030
}
3131

3232
/// <summary>
33-
/// Create the bytes-package to request data from the PLC. You have to specify the memory type (dataType),
34-
/// the address of the memory, the address of the byte and the bytes count.
33+
/// Create the bytes-package to request data from the PLC. You have to specify the memory type (dataType),
34+
/// the address of the memory, the address of the byte and the bytes count.
3535
/// </summary>
3636
/// <param name="dataType">MemoryType (DB, Timer, Counter, etc.)</param>
3737
/// <param name="db">Address of the memory to be read</param>
@@ -184,7 +184,7 @@ internal static int VarTypeToByteLength(VarType varType, int varCount = 1)
184184
switch (varType)
185185
{
186186
case VarType.Bit:
187-
return varCount + 7 / 8;
187+
return (varCount + 7) / 8;
188188
case VarType.Byte:
189189
return (varCount < 1) ? 1 : varCount;
190190
case VarType.String:

S7.Net/Types/Boolean.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,51 @@ public static bool GetValue(byte value, int bit)
1414
}
1515

1616
/// <summary>
17-
/// Sets the value of a bit to 1 (true), given the address of the bit
17+
/// Sets the value of a bit to 1 (true), given the address of the bit. Returns
18+
/// a copy of the value with the bit set.
1819
/// </summary>
20+
/// <param name="value">The input value to modify.</param>
21+
/// <param name="bit">The index (zero based) of the bit to set.</param>
22+
/// <returns>The modified value with the bit at index set.</returns>
1923
public static byte SetBit(byte value, int bit)
2024
{
21-
return (byte)((value | (1 << bit)) & 0xFF);
25+
SetBit(ref value, bit);
26+
27+
return value;
2228
}
2329

2430
/// <summary>
25-
/// Resets the value of a bit to 0 (false), given the address of the bit
31+
/// Sets the value of a bit to 1 (true), given the address of the bit.
32+
/// </summary>
33+
/// <param name="value">The value to modify.</param>
34+
/// <param name="bit">The index (zero based) of the bit to set.</param>
35+
public static void SetBit(ref byte value, int bit)
36+
{
37+
value = (byte) ((value | (1 << bit)) & 0xFF);
38+
}
39+
40+
/// <summary>
41+
/// Resets the value of a bit to 0 (false), given the address of the bit. Returns
42+
/// a copy of the value with the bit cleared.
2643
/// </summary>
44+
/// <param name="value">The input value to modify.</param>
45+
/// <param name="bit">The index (zero based) of the bit to clear.</param>
46+
/// <returns>The modified value with the bit at index cleared.</returns>
2747
public static byte ClearBit(byte value, int bit)
2848
{
29-
return (byte)((value | (~(1 << bit))) & 0xFF);
49+
ClearBit(ref value, bit);
50+
51+
return value;
3052
}
3153

54+
/// <summary>
55+
/// Resets the value of a bit to 0 (false), given the address of the bit
56+
/// </summary>
57+
/// <param name="value">The input value to modify.</param>
58+
/// <param name="bit">The index (zero based) of the bit to clear.</param>
59+
public static void ClearBit(ref byte value, int bit)
60+
{
61+
value = (byte) (value & ~(1 << bit) & 0xFF);
62+
}
3263
}
3364
}

0 commit comments

Comments
 (0)