Problem reading bit

Dec 27, 2014 at 1:06 PM
Edited Dec 27, 2014 at 1:27 PM
Hello:
There are a problem reading a bit.
  string txt2 = txt.Substring(1);
                    if (txt2.IndexOf(".") == -1) throw new Exception();

                    mByte = int.Parse(txt2.Substring(0, txt2.IndexOf(".")));
                    mBit = int.Parse(txt2.Substring(txt2.IndexOf(".") + 1));
                    if (mBit > 7) throw new Exception();

                    /////////objBoolArray = (bool[])Read(mDataType, 0, mByte, VarType.Bit, 1);

                    object ByteObtenido_obj=Read(mDataType, 0, mByte, VarType.Bit, 1);
                    byte ByteBuscado = Convert.ToByte(ByteObtenido_obj);

                    bool BitBuscado = ByteExtensions.GetBit(ByteBuscado, mBit);
                    return BitBuscado;____
                    /////////objBoolArray = (bool[])Read(mDataType, 0, mByte, VarType.Bit, 1);
                    /////////return objBoolArray[mBit];

////////////////////////////////////////////////////////////////////////////////////////////////////////////



public static class ByteExtensions
{
    public static bool GetBit(this byte byt, int index)
    {
        System.Collections.BitArray ba = new BitArray(new byte[] { byt });
        return ba.Get(index);
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////


No exits "case VarType.Bit:"


////////////////////////////////////////////////////////////////////////////////////////////////////////////
 public object Read(DataType DataType, int DB, int StartByteAdr, VarType VarType, int VarCount)
    {
        byte[] bytes = null;
        int cntBytes = 0;
        bool[] Byte_EnBits;

        switch (VarType)
        {


            case VarType.Bit:
                cntBytes = VarCount;
                    if (cntBytes < 1) cntBytes = 1;
                    bytes = ReadBytes(DataType, DB, StartByteAdr, cntBytes);
                    if (bytes == null) return null;
                    if (VarCount == 1)
                        //Byte_EnBits=new BitArray(bytes);

                        return bytes[0];
                    else
                        return bytes;



            case VarType.Byte:
                cntBytes = VarCount;
                if (cntBytes < 1) cntBytes = 1;
                bytes = ReadBytes(DataType, DB, StartByteAdr, cntBytes);
                if (bytes == null) return null;

Dec 27, 2014 at 1:43 PM
Edited Dec 27, 2014 at 1:44 PM
Other error writing a bit. In "//////////////if ((int)value == 1)"
                    txt2 = txt.Substring(1);
                    if (txt2.IndexOf(".") == -1) throw new Exception("Unbekannte Variable");

                    mByte = int.Parse(txt2.Substring(0, txt2.IndexOf(".")));
                    mBit = int.Parse(txt2.Substring(txt2.IndexOf(".") + 1));
                    if (mBit > 7) throw new Exception("Unbekannte Variable");
                    _byte = (byte)Read(mDataType, 0, mByte, VarType.Byte, 1);


                    //////////////if ((int)value == 1)
                    if ((bool)value == true)



                        _byte = (byte)(_byte | (byte)Math.Pow(2, mBit));      // Bit setzen
                    else
                        _byte = (byte)(_byte & (_byte ^ (byte)Math.Pow(2, mBit))); // Bit rücksetzen

                    return Write(mDataType, 0, mByte, (byte)_byte);
            }
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
            lastErrorCode = ErrorCode.WrongVarFormat;
            lastErrorString = "Die Variable '" + variable + "' konnte nicht entschlüsselt werden!";
            return lastErrorCode;
Dec 27, 2014 at 2:35 PM
//Reading bit en DB
  UInt32 objU = (UInt32)Read(DataType.DataBlock, mDB, dbIndex, VarType.DWord, 1);
                            return objU;
                        case "DBX":
                            mByte = dbIndex;
                            mBit = int.Parse(strings[2]);
                            if (mBit > 7) throw new Exception();

                            object ByteObtenido_obj2 = Read(DataType.DataBlock, mDB, mByte, VarType.Bit, 1);
                            byte ByteBuscado2 = Convert.ToByte(ByteObtenido_obj2);
                            bool BitBuscado2 = ByteExtensions.GetBit(ByteBuscado2, mBit);
                            return BitBuscado2;
                            //objBoolArray = (bool[])Read(DataType.DataBlock, mDB, mByte, VarType.Bit, 1);
                            //return objBoolArray[mBit];
                        default:
                            throw new Exception();
                    }
                case "EB":
                    // Eingangsbyte
                    objByte = (byte)Read(DataType.Input, 0, int.Parse(txt.Substring(2)), VarType.Byte, 1);
                    return objByte;









//Writint bit en DB


case "DBX":
                            mByte = dbIndex;
                            mBit = int.Parse(strings[2]);
                            if (mBit > 7) throw new Exception();
                            byte b = (byte)Read(DataType.DataBlock, mDB, mByte, VarType.Byte, 1);
                            ////////////////if ((int)value == 1)
                            if ((bool)value == true)
                                b = (byte)(b | (byte)Math.Pow(2, mBit)); // Bit setzen
                            else
                                b = (byte)(b & (b ^ (byte)Math.Pow(2, mBit))); // Bit rücksetzen

                            return Write(DataType.DataBlock, mDB, mByte, (byte)b);
                        case "DBS":
                            // DB-String
                            return Write(DataType.DataBlock, mDB, dbIndex, (string)value);
                        default: