Пример #1
0
        public CrcViewModel()
        {
            InputData = "123456";
            CrcTypes  = CrcTypes = new List <CrcType>()
            {
                CrcType.Crc16, CrcType.Crc32, CrcType.CrcITU, CrcType.CrcSdlcReverse
            };
            crc16  = new Crc16();
            crc32  = new Crc32();
            crcItu = new CRCITU();
            sdlc   = new SdlcReverse();
            this.RaisePropertyChanged(nameof(CrcTypes));
            crc = crc16;

            EncodeCmd = ReactiveCommand.Create(() =>
            {
                inputBytes    = StringToBytes(InputData);
                Input         = BytesToBinary(inputBytes);
                encodedBytes  = crc.Encode(inputBytes);
                OriginalCrc   = BitHelper.ByteToHex(encodedBytes.Take(crc.keyLength).ToArray());
                Encoded       = BytesToBinary(encodedBytes);
                Disturbed     = Encoded;
                DisturbedBits = GetDisturbedBits();
                this.RaisePropertyChanged(nameof(DisturbedBits));
            }, this.WhenAnyValue(i => i.InputData, i => !string.IsNullOrEmpty(i) && !i.Any(x => (int)x > 127)));

            DecodeCmd = ReactiveCommand.Create(() =>
            {
                var disturbedBytes = ExtractDisturbetBits();
                var decoded        = crc.Decode(disturbedBytes);
                byte[] newCrc;
                crc.Check(disturbedBytes, out newCrc);
                Decoded = BytesToBinary(decoded);
                Stats   = GetStats();
                CalculateNumberOfErrors();
                this.RaisePropertyChanged(nameof(Decoded));
                this.RaisePropertyChanged(nameof(Stats));
                DisturbedCrc = BitHelper.ByteToHex(disturbedBytes.Take(crc.keyLength).ToArray());
                ControlCrc   = BitHelper.ByteToHex(newCrc);
            }, this.WhenAnyValue(e => e.Encoded, e => !string.IsNullOrEmpty(e)));
        }