예제 #1
0
            public Item(BinaryReader source)
            {
                int dataLength;
                {
                    // Parse DIF
                    DIF dif;
                    do
                    {
                        dif = new DIF(source.ReadByte());
                    } while ((dif.DataType == DataTypes._Special_Functions) && (dif.Function == Functions.Minimum) && (!dif.StorageLSB) && (!dif.Extension));
                    DataType      = dif.DataType;
                    Function      = dif.Function;
                    StorageNumber = dif.StorageLSB ? 1UL : 0UL;
                    dataLength    = dif.Length;
                    Tariff        = 0;
                    SubUnit       = 0;
                    int  n         = 0;
                    bool Extension = dif.Extension;
                    while (Extension)
                    {
                        // Parse DIFE
                        if (n > 10)
                        {
                            throw new InvalidDataException();
                        }
                        DIFE   dife   = new DIFE(source.ReadByte());
                        UInt64 snPart = (UInt64)dife.StorageNumber;
                        snPart       <<= (n * 4 + 1);
                        StorageNumber |= snPart;
                        UInt32 tPart = (UInt32)dife.Tariff;
                        tPart <<= (n * 2);
                        Tariff |= tPart;
                        UInt16 suPart = (UInt16)dife.Device;
                        suPart  <<= n;
                        SubUnit  |= suPart;
                        Extension = dife.Extension;
                        n++;
                    }
                }
                {
                    // Parse VIF
                    VIF             vif       = new VIF(source);
                    List <UnitData> unitsList = new List <UnitData>();
                    if ((vif.VIFType != VIF.VIFTypes.LinearVIFExtensionFD)
                        &&
                        (vif.VIFType != VIF.VIFTypes.LinearVIFExtensionFB))
                    {
                        unitsList.Add(new UnitData(vif.Units, vif.Magnitude, vif.VIF_string));
                    }
                    bool Extension = vif.Extension;
                    int  n         = 0;
                    while (Extension)
                    {
                        // Parse VIFE
                        if (n > 10)
                        {
                            throw new InvalidDataException();
                        }
                        switch (vif.VIFType)
                        {
                        case VIF.VIFTypes.LinearVIFExtensionFB:
                        {
                            VIFE_FB vife = new VIFE_FB(source.ReadByte());
                            Extension = vife.Extension;
                            unitsList.Add(new UnitData(vife.Units, vife.Magnitude));
                        }
                        break;

                        case VIF.VIFTypes.LinearVIFExtensionFD:
                        {
                            VIFE_FD vife = new VIFE_FD(source.ReadByte());
                            Extension = vife.Extension;
                            unitsList.Add(new UnitData(vife.Units, vife.Magnitude));
                        }
                        break;

                        default:
                        {
                            VIFE vife = new VIFE(source.ReadByte());
                            Extension = vife.Extension;
                            unitsList.Add(new UnitData(vife.Units, vife.Magnitude));
                        }
                        break;
                        }
                        n++;
                    }
                    Units = unitsList.ToArray();
                }
                {
                    // Parse Data
                    byte[] buf = (dataLength > 0) ? source.ReadBytes(dataLength) : null;
                    switch (DataType)
                    {
                    case DataTypes._No_data:
                        Value = null;
                        break;

                    case DataTypes._8_Bit_Integer:
                        System.Diagnostics.Debug.Assert(buf.Length == 1);
                        Value = buf.Single();
                        break;

                    case DataTypes._16_Bit_Integer:
                        System.Diagnostics.Debug.Assert(buf.Length == 2);
                        Value = BitConverter.ToInt16(buf, 0);
                        break;

                    case DataTypes._24_Bit_Integer:
                        System.Diagnostics.Debug.Assert(buf.Length == 3);
                        Value = BitConverter.ToInt32(new byte[] { 0 }.Concat(buf).ToArray(), 0);
                        break;

                    case DataTypes._32_Bit_Integer:
                        System.Diagnostics.Debug.Assert(buf.Length == 4);
                        Value = BitConverter.ToInt32(buf, 0);
                        break;

                    case DataTypes._32_Bit_Real:
                        System.Diagnostics.Debug.Assert(buf.Length == 4);
                        Value = BitConverter.ToSingle(buf, 0);
                        break;

                    case DataTypes._48_Bit_Integer:
                        System.Diagnostics.Debug.Assert(buf.Length == 6);
                        Value = BitConverter.ToInt64(new byte[] { 0, 0 }.Concat(buf).ToArray(), 0);
                        break;

                    case DataTypes._64_Bit_Integer:
                        System.Diagnostics.Debug.Assert(buf.Length == 8);
                        Value = BitConverter.ToInt64(buf, 0);
                        break;

                    case DataTypes._Selection_for_Readout:
                        throw new NotImplementedException();     /// !!!

                    case DataTypes._2_digit_BCD:
                        System.Diagnostics.Debug.Assert(buf.Length == 1);
                        Value = sbyte.Parse(buf.BCDToString());
                        break;

                    case DataTypes._4_digit_BCD:
                        System.Diagnostics.Debug.Assert(buf.Length == 2);
                        Value = Int16.Parse(buf.BCDToString());
                        break;

                    case DataTypes._6_digit_BCD:
                        System.Diagnostics.Debug.Assert(buf.Length == 3);
                        Value = Int32.Parse(buf.BCDToString());
                        break;

                    case DataTypes._8_digit_BCD:
                        System.Diagnostics.Debug.Assert(buf.Length == 4);
                        Value = Int32.Parse(buf.BCDToString());
                        break;

                    case DataTypes._variable_length:
                        System.Diagnostics.Debug.Assert(buf == null);
                        Value = VariableLengthData.ReadValue(source);
                        break;

                    case DataTypes._12_digit_BCD:
                        System.Diagnostics.Debug.Assert(buf.Length == 6);
                        Value = Int64.Parse(buf.BCDToString());
                        break;

                    case DataTypes._Special_Functions:
                        throw new NotImplementedException();     /// !!!
                        break;
                    }
                }
            }
예제 #2
0
        public VIF(BinaryReader source)
        {
            byte b = source.ReadByte();

            if (((b >= 0) && (b <= 0x7b))
                ||
                ((b >= 0x80) && (b <= 0xfb)))
            {
                VIFType = VIFTypes.PrimaryVIF;
            }
            else if ((b & 0x7f) == 0x7c)
            {
                VIFType = VIFTypes.PlainTextVIF;
            }
            else if ((b & 0x7f) == 0x7e)
            {
                VIFType = VIFTypes.AnyVIF;
            }
            else if ((b & 0x7f) == 0x7f)
            {
                VIFType = VIFTypes.ManufacturerSpecific;
            }
            else if (b == 0xfd)
            {
                VIFType = VIFTypes.LinearVIFExtensionFD;
            }
            else if (b == 0xfb)
            {
                VIFType = VIFTypes.LinearVIFExtensionFB;
            }
            else
            {
                throw new InvalidDataException();
            }
            Extension = (b & 0x80) != 0;
            b        &= 0x7f; // clear Extension bit
            if ((b >= 0) && (b <= 0x07))
            {
                Units     = UnitsVariableData.EnergyWh;
                Magnitude = (b & 0x07) - 3;
            }
            else if ((b >= 0x08) && (b <= 0x0f))
            {
                Units     = UnitsVariableData.EnergyJ;
                Magnitude = b & 0x07;
            }
            else if ((b >= 0x10) && (b <= 0x17))
            {
                Units     = UnitsVariableData.Volume_m3;
                Magnitude = (b & 0x07) - 6;
            }
            else if ((b >= 0x18) && (b <= 0x1f))
            {
                Units     = UnitsVariableData.Mass_kg;
                Magnitude = (b & 0x07) - 3;
            }
            else if ((b >= 0x20) && (b <= 0x23))
            {
                Units     = UnitsVariableData.OnTime;
                Magnitude = b & 0x03;
            }
            else if ((b >= 0x24) && (b <= 0x27))
            {
                Units     = UnitsVariableData.OperatingTime;
                Magnitude = b & 0x03;
            }
            else if ((b >= 0x28) && (b <= 0x2f))
            {
                Units     = UnitsVariableData.PowerW;
                Magnitude = (b & 0x07) - 3;
            }
            else if ((b >= 0x30) && (b <= 0x37))
            {
                Units     = UnitsVariableData.PowerJ_per_h;
                Magnitude = b & 0x07;
            }
            else if ((b >= 0x38) && (b <= 0x3f))
            {
                Units     = UnitsVariableData.VolumeFlowM3_per_h;
                Magnitude = (b & 0x07) - 6;
            }
            else if ((b >= 0x40) && (b <= 0x47))
            {
                Units     = UnitsVariableData.VolumeFlowExtM3_per_min;
                Magnitude = (b & 0x07) - 7;
            }
            else if ((b >= 0x48) && (b <= 0x4f))
            {
                Units     = UnitsVariableData.VolumeFlowExtM3_per_s;
                Magnitude = (b & 0x07) - 9;
            }
            else if ((b >= 0x50) && (b <= 0x57))
            {
                Units     = UnitsVariableData.MassFlowKg_per_h;
                Magnitude = (b & 0x07) - 3;
            }
            else if ((b >= 0x58) && (b <= 0x5b))
            {
                Units     = UnitsVariableData.FlowTemperatureC;
                Magnitude = (b & 0x03) - 3;
            }
            else if ((b >= 0x5c) && (b <= 0x5f))
            {
                Units     = UnitsVariableData.ReturnTemperatureC;
                Magnitude = (b & 0x03) - 3;
            }
            else if ((b >= 0x60) && (b <= 0x63))
            {
                Units     = UnitsVariableData.TemperatureDifferenceK;
                Magnitude = (b & 0x03) - 3;
            }
            else if ((b >= 0x64) && (b <= 0x67))
            {
                Units     = UnitsVariableData.ExternalTemperatureC;
                Magnitude = (b & 0x03) - 3;
            }
            else if ((b >= 0x68) && (b <= 0x6b))
            {
                Units     = UnitsVariableData.PressureBar;
                Magnitude = (b & 0x03) - 3;
            }
            else if ((b >= 0x6c) && (b <= 0x6d))
            {
                Units     = UnitsVariableData.TimePoint;
                Magnitude = b & 0x01;
            }
            else if (b == 0x6e)
            {
                Units     = UnitsVariableData.UnitsForHCA;
                Magnitude = 0;
            }
            else if (b == 0x6f)
            {
                Units     = UnitsVariableData.Reserved;
                Magnitude = 0;
            }
            else if ((b >= 0x70) && (b <= 0x73))
            {
                Units     = UnitsVariableData.AveragingDuration;
                Magnitude = b & 0x03;
            }
            else if ((b >= 0x74) && (b <= 0x77))
            {
                Units     = UnitsVariableData.ActualityDuration;
                Magnitude = b & 0x03;
            }
            else if (b == 0x78)
            {
                Units     = UnitsVariableData.FabricationNo;
                Magnitude = 0;
            }
            else if (b == 0x79)
            {
                Units     = UnitsVariableData.EnhancedIdentification;
                Magnitude = 0;
            }
            else if (b == 0x7A)
            {
                Units     = UnitsVariableData.BusAddress;
                Magnitude = 0;
            }
            else if (b == 0x7B)
            {
                Units     = UnitsVariableData.Extension_7B;
                Magnitude = 0;
            }
            else if (b == 0x7C)
            {
                Units      = UnitsVariableData.VIF_string;
                Magnitude  = 0;
                VIF_string = VariableLengthData.ReadString(source);
            }
            else if (b == 0x7D)
            {
                Units     = UnitsVariableData.Extension_7D;
                Magnitude = 0;
            }
            else if (b == 0x7E)
            {
                Units     = UnitsVariableData.AnyVIF;
                Magnitude = 0;
            }
            else if (b == 0x7F)
            {
                Units     = UnitsVariableData.ManufacturerSpecific;
                Magnitude = 0;
            }
            else
            {
                throw new InvalidDataException();
            }
        }