Merge pull request #97 from GS770/master

Added Bit to Types
This commit is contained in:
Michele Cattafesta
2017-12-14 23:00:19 +00:00
committed by GitHub
3 changed files with 36 additions and 6 deletions

View File

@@ -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;
}

View File

@@ -80,6 +80,7 @@
<Compile Include="Enums.cs" />
<Compile Include="PLC.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Types\Bit.cs" />
<Compile Include="Types\Boolean.cs" />
<Compile Include="Types\Byte.cs" />
<Compile Include="Types\ByteArray.cs" />

29
S7.Net/Types/Bit.cs Normal file
View File

@@ -0,0 +1,29 @@
using System;
using System.Collections;
namespace S7.Net.Types
{
/// <summary>
/// Contains the conversion methods to convert Bit from S7 plc to C#.
/// </summary>
public static class Bit
{
/// <summary>
/// Converts a Bit to bool
/// </summary>
public static bool FromByte(byte v, byte bitAdr)
{
BitArray bitArr = new BitArray(new byte[] { v });
return bitArr[bitAdr];
}
/// <summary>
/// Converts an array of bytes to a BitArray
/// </summary>
public static BitArray ToBitArray(byte[] bytes)
{
BitArray bitArr = new BitArray(bytes);
return bitArr;
}
}
}