public IAinSettings GetResult(byte[] reply)
        {
            if (reply[0] != OneBasedAinNumber)
            {
                throw new Exception("неверный номер АИН в ответе, ожидался " + OneBasedAinNumber);
            }

            // TODO: check if reply[0] is equal oneBasedAinNumber
            var replyWithoutAinNumber = reply.Skip(1).ToList();


            var bp52 = new BytesPair(replyWithoutAinNumber[104], replyWithoutAinNumber[105]);
            //Console.WriteLine("<<READ>> NPRM = " + bp52.First.ToString("X2") + bp52.Second.ToString("X2"));
            var np            = bp52.First & 0x1F;
            var nimpFloorCode = (bp52.First & 0xE0) >> 5;
            var fanMode       = AinTelemetryFanWorkmodeExtensions.FromIoBits(bp52.Second & 0x03);
            var directCurrentMagnetization = bp52.Second.GetBit(3);

            return(new AinSettingsSimple(
                       new BytesPair(replyWithoutAinNumber[0], replyWithoutAinNumber[1]),
                       BytesPairToDecimalQ8Converter.ConvertBytesPairToDecimalQ8(new BytesPair(replyWithoutAinNumber[2],
                                                                                               replyWithoutAinNumber[3])),
                       // kiW
                       (replyWithoutAinNumber[4] + (replyWithoutAinNumber[5] << 8) + (replyWithoutAinNumber[6] << 16) +
                        (replyWithoutAinNumber[7] << 24)) / 16777216.0m,
                       new BytesPair(replyWithoutAinNumber[8], replyWithoutAinNumber[9]).LowFirstSignedValue / 1000.0m,
                       new BytesPair(replyWithoutAinNumber[10], replyWithoutAinNumber[11]).LowFirstSignedValue,
                       new BytesPair(replyWithoutAinNumber[12], replyWithoutAinNumber[13]).LowFirstSignedValue,
                       new BytesPair(replyWithoutAinNumber[14], replyWithoutAinNumber[15]).LowFirstSignedValue,

                       // Fnom:
                       new BytesPair(replyWithoutAinNumber[16], replyWithoutAinNumber[17]).LowFirstSignedValue / 10.0m,
                       // Fmax:
                       new BytesPair(replyWithoutAinNumber[18], replyWithoutAinNumber[19]).LowFirstSignedValue / 10.0m,
                       // DflLim:
                       new BytesPair(replyWithoutAinNumber[20], replyWithoutAinNumber[21]).LowFirstSignedValue / 1000.0m,
                       new BytesPair(replyWithoutAinNumber[22], replyWithoutAinNumber[23]).LowFirstSignedValue / 1000.0m,
                       new BytesPair(replyWithoutAinNumber[24], replyWithoutAinNumber[25]).LowFirstSignedValue,
                       new BytesPair(replyWithoutAinNumber[26], replyWithoutAinNumber[27]).LowFirstSignedValue / 1000.0m,
                       new BytesPair(replyWithoutAinNumber[28], replyWithoutAinNumber[29]).LowFirstUnsignedValue,
                       new BytesPair(replyWithoutAinNumber[30], replyWithoutAinNumber[31]).LowFirstUnsignedValue,
                       new BytesPair(replyWithoutAinNumber[32], replyWithoutAinNumber[33]).LowFirstSignedValue,
                       new BytesPair(replyWithoutAinNumber[34], replyWithoutAinNumber[35]).LowFirstSignedValue,
                       new BytesPair(replyWithoutAinNumber[36], replyWithoutAinNumber[37]).LowFirstSignedValue,
                       new BytesPair(replyWithoutAinNumber[38], replyWithoutAinNumber[39]).LowFirstSignedValue,

                       // TauR:
                       new BytesPair(replyWithoutAinNumber[40], replyWithoutAinNumber[41]).LowFirstSignedValue / 10000.0m,
                       // Lm:
                       new BytesPair(replyWithoutAinNumber[42], replyWithoutAinNumber[43]).LowFirstSignedValue / 100000.0m,
                       // Lsl:
                       new BytesPair(replyWithoutAinNumber[44], replyWithoutAinNumber[45]).LowFirstSignedValue / 1000000.0m,
                       // Lrl:
                       new BytesPair(replyWithoutAinNumber[46], replyWithoutAinNumber[47]).LowFirstSignedValue / 1000000.0m,

                       // reserved 24:
                       new BytesPair(replyWithoutAinNumber[48], replyWithoutAinNumber[49]),
                       BytesPairToDecimalQ8Converter.ConvertBytesPairToDecimalQ8(new BytesPair(replyWithoutAinNumber[50],
                                                                                               replyWithoutAinNumber[51])),
                       // kiFi:
                       (replyWithoutAinNumber[52] + (replyWithoutAinNumber[53] << 8) + (replyWithoutAinNumber[54] << 16) +
                        (replyWithoutAinNumber[55] << 24)) / 16777216.0m,

                       // reserved 28:
                       new BytesPair(replyWithoutAinNumber[56], replyWithoutAinNumber[57]),

                       // kpId:
                       BytesPairToDecimalQ8Converter.ConvertBytesPairToDecimalQ8(new BytesPair(replyWithoutAinNumber[58],
                                                                                               replyWithoutAinNumber[59])),
                       // kiId:
                       (replyWithoutAinNumber[60] + (replyWithoutAinNumber[61] << 8) + (replyWithoutAinNumber[62] << 16) +
                        (replyWithoutAinNumber[63] << 24)) / 16777216.0m,

                       // reserverd 32:
                       new BytesPair(replyWithoutAinNumber[64], replyWithoutAinNumber[65]),

                       // kpIq:
                       BytesPairToDecimalQ8Converter.ConvertBytesPairToDecimalQ8(new BytesPair(replyWithoutAinNumber[66],
                                                                                               replyWithoutAinNumber[67])),
                       // kiIq:
                       (replyWithoutAinNumber[68] + (replyWithoutAinNumber[69] << 8) + (replyWithoutAinNumber[70] << 16) +
                        (replyWithoutAinNumber[71] << 24)) / 16777216.0m,
                       new BytesPair(replyWithoutAinNumber[72], replyWithoutAinNumber[73]).LowFirstSignedValue * 0.1m,
                       new BytesPair(replyWithoutAinNumber[74], replyWithoutAinNumber[75]).LowFirstSignedValue * 0.1m,
                       // Unom:
                       new BytesPair(replyWithoutAinNumber[76], replyWithoutAinNumber[77]).LowFirstSignedValue /
                       (decimal)Math.Sqrt(2.0),
                       // TauFlLim:
                       new BytesPair(replyWithoutAinNumber[78], replyWithoutAinNumber[79]).LowFirstSignedValue / 10000.0m,
                       // Rs:
                       new BytesPair(replyWithoutAinNumber[80], replyWithoutAinNumber[81]).LowFirstSignedValue / 10000.0m,
                       // fmin:
                       new BytesPair(replyWithoutAinNumber[82], replyWithoutAinNumber[83]).LowFirstSignedValue / 10.0m,
                       new BytesPair(replyWithoutAinNumber[84], replyWithoutAinNumber[85]).LowFirstSignedValue / 10000.0m,
                       new BytesPair(replyWithoutAinNumber[86], replyWithoutAinNumber[87]).LowFirstSignedValue / 10000.0m,
                       new BytesPair(replyWithoutAinNumber[88], replyWithoutAinNumber[89]).LowFirstSignedValue / 10000.0m,
                       new BytesPair(replyWithoutAinNumber[90], replyWithoutAinNumber[91]).LowFirstSignedValue / 10000.0m,
                       new BytesPair(replyWithoutAinNumber[92], replyWithoutAinNumber[93]).LowFirstSignedValue,
                       new BytesPair(replyWithoutAinNumber[94], replyWithoutAinNumber[95]).LowFirstSignedValue,
                       new BytesPair(replyWithoutAinNumber[96], replyWithoutAinNumber[97]),
                       new BytesPair(replyWithoutAinNumber[98], replyWithoutAinNumber[99]),

                       // reserverd 50:
                       new BytesPair(replyWithoutAinNumber[100], replyWithoutAinNumber[101]),

                       // reserverd 51:
                       new BytesPair(replyWithoutAinNumber[102], replyWithoutAinNumber[103]),

                       // Param52 (np and others):
                       np,
                       nimpFloorCode,
                       fanMode,
                       directCurrentMagnetization,
                       new BytesPair(replyWithoutAinNumber[106], replyWithoutAinNumber[107]).LowFirstSignedValue / 1000.0m,
                       new BytesPair(replyWithoutAinNumber[108], replyWithoutAinNumber[109]).LowFirstSignedValue / 10.0m,
                       new BytesPair(replyWithoutAinNumber[110], replyWithoutAinNumber[111]).LowFirstSignedValue,
                       new BytesPair(replyWithoutAinNumber[112], replyWithoutAinNumber[113]).LowFirstSignedValue,

                       // Status byte:
                       (replyWithoutAinNumber[114] & 0x01) == 0x01,
                       (replyWithoutAinNumber[114] & 0x02) == 0x02,
                       (replyWithoutAinNumber[114] & 0x04) == 0x04
                       ));
        }
Exemplo n.º 2
0
        public byte[] Serialize()
        {
            var settingsSerialized = new byte[114];

            settingsSerialized[0] = _settings.Reserved00.First;
            settingsSerialized[1] = _settings.Reserved00.Second;
            Console.WriteLine("_settings.KpW = " + _settings.KpW.ToString("f10"));
            var bpKpW = BytesPairToDecimalQ8Converter.ConvertToBytesPairQ8(_settings.KpW);

            settingsSerialized[2] = bpKpW.First;
            settingsSerialized[3] = bpKpW.Second;
            Console.WriteLine("_settings.KpW SERIALIZED = " + bpKpW);


            var ushbp52 = (ushort)(_settings.Np | (_settings.NimpFloorCode << 5) |
                                   ((_settings.FanMode.ToIoBits() & 0x03) << 8));

            Console.WriteLine("NPRM without DCM = " + ushbp52.ToString("X4"));
            ushbp52 = _settings.DirectCurrentMagnetization ? ushbp52.SetBit(11) : ushbp52.ResetBit(11);
            var bp52 = BytesPair.FromUnsignedShortLowFirst(ushbp52);

            Console.WriteLine("NPRM = " + bp52);


            settingsSerialized.SerializeIntLowFirst(4, (int)(_settings.KiW * 16777216.0m));
            settingsSerialized.SerializeShortLowFirst(8, (short)(_settings.FiNom * 1000.0m));
            settingsSerialized.SerializeShortLowFirst(10, _settings.Imax);
            settingsSerialized.SerializeShortLowFirst(12, _settings.UdcMax);
            settingsSerialized.SerializeShortLowFirst(14, _settings.UdcMin);

            settingsSerialized.SerializeUshortLowFirst(16, (ushort)(_settings.Fnom * 10.0m));
            settingsSerialized.SerializeUshortLowFirst(18, (ushort)(_settings.Fmax * 10.0m));

            settingsSerialized.SerializeShortLowFirst(20, (short)(_settings.DflLim * 1000.0m));
            settingsSerialized.SerializeShortLowFirst(22, (short)(_settings.FlMinMin * 1000.0m));

            settingsSerialized.SerializeShortLowFirst(24, _settings.IoutMax);
            settingsSerialized.SerializeShortLowFirst(26, (short)(_settings.FiMin * 1000.0m));

            settingsSerialized.SerializeUshortLowFirst(28, _settings.DacCh);
            settingsSerialized.SerializeUshortLowFirst(30, _settings.Imcw);

            settingsSerialized.SerializeShortLowFirst(32, _settings.Ia0);
            settingsSerialized.SerializeShortLowFirst(34, _settings.Ib0);
            settingsSerialized.SerializeShortLowFirst(36, _settings.Ic0);
            settingsSerialized.SerializeShortLowFirst(38, _settings.Udc0);
            settingsSerialized.SerializeShortLowFirst(40, (short)(_settings.TauR * 10000.0m));
            settingsSerialized.SerializeShortLowFirst(42, (short)(_settings.Lm * 100000.0m));
            settingsSerialized.SerializeShortLowFirst(44, (short)(_settings.Lsl * 1000000.0m));
            settingsSerialized.SerializeShortLowFirst(46, (short)(_settings.Lrl * 1000000.0m));

            settingsSerialized[48] = _settings.Reserved24.First;
            settingsSerialized[49] = _settings.Reserved24.Second;

            var bpKpFi = BytesPairToDecimalQ8Converter.ConvertToBytesPairQ8(_settings.KpFi);

            settingsSerialized[50] = bpKpFi.First;
            settingsSerialized[51] = bpKpFi.Second;

            settingsSerialized.SerializeIntLowFirst(52, (int)(_settings.KiFi * 16777216.0m));

            settingsSerialized[56] = _settings.Reserved28.First;
            settingsSerialized[57] = _settings.Reserved28.Second;

            var bpKpId = BytesPairToDecimalQ8Converter.ConvertToBytesPairQ8(_settings.KpId);

            settingsSerialized[58] = bpKpId.First;
            settingsSerialized[59] = bpKpId.Second;

            settingsSerialized.SerializeIntLowFirst(60, (int)(_settings.KiId * 16777216.0m));


            settingsSerialized[64] = _settings.Reserved32.First;
            settingsSerialized[65] = _settings.Reserved32.Second;


            var bpKpIq = BytesPairToDecimalQ8Converter.ConvertToBytesPairQ8(_settings.KpIq);

            settingsSerialized[66] = bpKpIq.First;
            settingsSerialized[67] = bpKpIq.Second;

            settingsSerialized.SerializeIntLowFirst(68, (int)(_settings.KiIq * 16777216.0m));

            settingsSerialized.SerializeShortLowFirst(72, (short)(_settings.AccDfDt * 10.0m));
            settingsSerialized.SerializeShortLowFirst(74, (short)(_settings.DecDfDt * 10.0m));
            settingsSerialized.SerializeUshortLowFirst(76,
                                                       (ushort)Math.Round(_settings.Unom * (decimal)Math.Sqrt(2.0)));

            settingsSerialized.SerializeShortLowFirst(78, (short)(_settings.TauFlLim * 10000.0m));

            settingsSerialized.SerializeUshortLowFirst(80, (ushort)(_settings.Rs * 10000.0m));

            settingsSerialized.SerializeUshortLowFirst(82, (ushort)(_settings.Fmin * 10.0m));
            settingsSerialized.SerializeShortLowFirst(84, (short)(_settings.TauM * 10000.0m));
            settingsSerialized.SerializeShortLowFirst(86, (short)(_settings.TauF * 10000.0m));
            settingsSerialized.SerializeShortLowFirst(88, (short)(_settings.TauFSet * 10000.0m));
            settingsSerialized.SerializeShortLowFirst(90, (short)(_settings.TauFi * 10000.0m));
            settingsSerialized.SerializeShortLowFirst(92, _settings.IdSetMin);
            settingsSerialized.SerializeShortLowFirst(94, _settings.IdSetMax);


            settingsSerialized[96] = _settings.UchMin.First;
            settingsSerialized[97] = _settings.UchMin.Second;

            settingsSerialized[98] = _settings.UchMax.First;
            settingsSerialized[99] = _settings.UchMax.Second;


            settingsSerialized[100] = _settings.Reserved50.First;
            settingsSerialized[101] = _settings.Reserved50.Second;

            settingsSerialized[102] = _settings.Reserved51.First;
            settingsSerialized[103] = _settings.Reserved51.Second;


            settingsSerialized[104] = bp52.First;
            settingsSerialized[105] = bp52.Second;


            settingsSerialized.SerializeShortLowFirst(106, (short)(_settings.UmodThr * 1000.0m));
            settingsSerialized.SerializeShortLowFirst(108, (short)(_settings.EmdecDfdt * 10.0m));
            settingsSerialized.SerializeShortLowFirst(110, _settings.TextMax);
            settingsSerialized.SerializeShortLowFirst(112, _settings.ToHl);

            var result = new byte[115];

            result[0] = OneBasedAinNumber;
            settingsSerialized.CopyTo(result, 1);
            return(result);
        }