diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs index 6b5737a..c5b9676 100644 --- a/S7.Net/PLC.cs +++ b/S7.Net/PLC.cs @@ -1221,13 +1221,13 @@ namespace S7.Net else return Types.Counter.ToArray(bytes); case VarType.Bit: - if (varCount == 1 && bitAdr <= 7) - { - BitArray bitArr = new BitArray(new byte[] { bytes[0] }); - return bitArr[bitAdr]; - } + if (varCount == 1) + if (bitAdr > 7) + return null; + else + return Types.Bit.FromByte(bytes[0], bitAdr); else - return null; + return Types.Bit.ToBitArray(bytes); default: return null; } diff --git a/S7.Net/S7.Net.csproj b/S7.Net/S7.Net.csproj index 6ff90eb..5356672 100644 --- a/S7.Net/S7.Net.csproj +++ b/S7.Net/S7.Net.csproj @@ -80,6 +80,7 @@ + diff --git a/S7.Net/Types/Bit.cs b/S7.Net/Types/Bit.cs new file mode 100644 index 0000000..8523cb5 --- /dev/null +++ b/S7.Net/Types/Bit.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections; + +namespace S7.Net.Types +{ + /// + /// Contains the conversion methods to convert Bit from S7 plc to C#. + /// + public static class Bit + { + /// + /// Converts a Bit to bool + /// + public static bool FromByte(byte v, byte bitAdr) + { + BitArray bitArr = new BitArray(new byte[] { v }); + return bitArr[bitAdr]; + } + + /// + /// Converts an array of bytes to a BitArray + /// + public static BitArray ToBitArray(byte[] bytes) + { + BitArray bitArr = new BitArray(bytes); + return bitArr; + } + } +}