Exemple #1
0
        public bool UpdateFromPackData()
        {
            if (pack_data_ == null)
            {
                return(false);
            }

            unpack_data_     = null;
            unpack_elements_ = null;

            /* Pack BitData -> Unpack BitData */
            if (pack_data_ != null)
            {
                if (!OnPackDataToUnpackData(pack_data_, ref unpack_data_))
                {
                    return(false);
                }
            }

            /* Unpack BitData -> Unpack Elements */
            if (unpack_data_ != null)
            {
                if (!OnUnpackDataToUnpackElements(unpack_data_))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #2
0
        public bool UpdateFromUnpackData()
        {
            if (unpack_data_ == null)
            {
                return(false);
            }

            pack_data_       = null;
            unpack_elements_ = null;

            if (unpack_data_ != null)
            {
                if (!OnUnpackDataToUnpackElements(unpack_data_))
                {
                    return(false);
                }

                if (!OnUnpackDataToPackData(unpack_data_, ref pack_data_))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #3
0
        private void ComputeErrorStatistics(BitData dataModel)
        {
            if (dataModel == null)
            {
                return;
            }

            dataModel.FixedBitErrorIndexes = null;
            _codeAlgo.DetectBitErrors(dataModel);
            IncorrectTransmission = DataModel.IncorrectTransmission;
            FixedBitErrorsCount   = DataModel.FixedBitCount;

            var missedErrors = ModifiedBits.Count(b => b) - DataModel.FixedBitCount;

            if (DataModel.FixedBitCount > 0)
            {
                var wronglyDetected = DataModel.FixedBitErrorIndexes.Select(i => ModifiedBits[i]).Count(m => m == false);
                missedErrors          += wronglyDetected;
                DetectedErrorsAreWrong = wronglyDetected > 0;
            }
            else
            {
                DetectedErrorsAreWrong = false;
            }

            MissedBitErrorsCount        = missedErrors;
            WrongTransmissionEvaluation = MissedBitErrorsCount > 0 && IncorrectTransmission != true;
            FixedBits = dataModel.FixedBitErrorIndexes;
        }
Exemple #4
0
 private void ComputeCode()
 {
     byte[] crcBits = null;
     if (_codeAlgo is IByteBasedCode byteBasedCode)
     {
         var bytes = byteBasedCode.ComputeCode(_originalBits.ConvertToBytes());
         crcBits = bytes.ConvertToBits();
     }
     else if (_codeAlgo is IBitBasedCode bitBasedCode)
     {
         crcBits = bitBasedCode.ComputeCode(_originalBits);
     }
     if (_notContagiousData)
     {
         TransmittedBits = crcBits;
         var emptyCrcBits = new byte[crcBits.Length - _originalBits.Length];
         DataModel     = new BitData(emptyCrcBits, crcBits, _originalBits, _notContagiousData);
         _originalBits = (byte[])TransmittedBits.Clone();
     }
     else
     {
         TransmittedBits = _originalBits.ConcatArray(crcBits);
         DataModel       = new BitData(crcBits, TransmittedBits, _originalBits);
     }
     ModifiedBits = new bool[TransmittedBits.Length];
 }
Exemple #5
0
        protected virtual bool OnUnpackElementsToUnpackData(IEnumerable <ProtocolFrameElement> unpack_elements, ref BitData unpack_data)
        {
            var unpack_bitdata_list   = new List <BitData>();
            var unpack_bitdata_length = (uint)0;
            var unpack_bitdata_offset = (uint)0;
            var unpack_bitdata        = (BitData)null;

            foreach (var elem in unpack_elements)
            {
                unpack_bitdata = elem.GetUnpackData();

                if (unpack_bitdata != null)
                {
                    unpack_bitdata_list.Add(unpack_bitdata);
                    unpack_bitdata_length += unpack_bitdata.Length;
                }
            }

            unpack_data = new BitData(unpack_bitdata_length);

            foreach (var bitdata in unpack_bitdata_list)
            {
                unpack_data.SetBitData(unpack_bitdata_offset, bitdata);
                unpack_bitdata_offset += bitdata.Length;
            }

            return(true);
        }
Exemple #6
0
        public bool UpdateFromUnpackElements()
        {
            if (unpack_elements_ == null)
            {
                return(false);
            }

            pack_data_   = null;
            unpack_data_ = null;

            if (unpack_elements_ != null)
            {
                /* 下層全ての要素を更新 */
                unpack_elements_.ForEach(elem => elem.UpdateFromUnpackElements());

                if (!OnUnpackElementsToUnpackData(unpack_elements_, ref unpack_data_))
                {
                    return(false);
                }
            }

            if (unpack_data_ != null)
            {
                if (!OnUnpackDataToPackData(unpack_data_, ref pack_data_))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #7
0
        protected override bool OnUnpackDataToUnpackValue(BitData unpack_data, ref object unpack_value)
        {
            try {
                unpack_value = unpack_data.GetInteger(0);

                return(true);
            } catch {
                return(false);
            }
        }
        protected override bool OnUnpackDataToUnpackValue(BitData unpack_data, ref object unpack_value)
        {
            try {
                unpack_value = Enum.ToObject(typeof(EnumT), unpack_data.GetInteger(0));

                return(true);
            } catch {
                return(false);
            }
        }
        protected override bool OnUnpackValueToUnpackData(object unpack_value, ref BitData unpack_data)
        {
            try {
                unpack_data = new BitData(Convert.ToUInt64(unpack_value), PackDataBitLength);

                return(true);
            } catch {
                return(false);
            }
        }
Exemple #10
0
        public ProtocolFrameElement(ProtocolFrameElement parent, string name, uint pack_bitlen)
        {
            Parent            = parent;
            Name              = name;
            PackDataBitLength = pack_bitlen;

            pack_data_       = new BitData(PackDataBitLength);
            unpack_data_     = pack_data_;
            unpack_elements_ = null;

            if (Parent != null)
            {
                Parent.AddUnpackElement(this);
            }
        }
Exemple #11
0
        protected override bool OnUnpackValueToUnpackData(object unpack_value, ref BitData unpack_data)
        {
            try {
                var unpack_value_i = Convert.ToUInt64(unpack_value);

                unpack_value_i = Math.Max(value_min_, unpack_value_i);
                unpack_value_i = Math.Min(value_max_, unpack_value_i);

                unpack_data = new BitData(unpack_value_i, PackDataBitLength);

                return(true);
            } catch {
                return(false);
            }
        }
Exemple #12
0
        public bool SetUnpackValue(object unpack_value)
        {
            if (unpack_value == null)
            {
                return(false);
            }

            unpack_data_ = null;

            if (!OnUnpackValueToUnpackData(unpack_value, ref unpack_data_))
            {
                return(false);
            }

            return(UpdateFromUnpackData());
        }
Exemple #13
0
        public void GenerateErrors()
        {
            if (_originalBits == null)
            {
                return;
            }

            var crcBits = DataModel.CorrectionBits;

            if (_notContagiousData)
            {
                TransmittedBits = (byte[])_originalBits.Clone();
            }
            else
            {
                TransmittedBits = _originalBits.ConcatArray(crcBits);
            }

            var bitErrors  = new bool[TransmittedBits.Length];
            var errorCount = Math.Min(TextLength.Value, GeneratedErrorCount);

            for (int i = 0; i < errorCount; i++)
            {
                int index;
                do
                {
                    index = _random.Next(TransmittedBits.Length);
                }while (bitErrors[index]);

                bitErrors[index]        = true;
                TransmittedBits[index] ^= 1;
            }
            DataModel    = new BitData(crcBits, TransmittedBits, DataModel.DataBits, _notContagiousData);
            ModifiedBits = bitErrors;
            ComputeErrorStatistics(DataModel);
        }
Exemple #14
0
        public bool SetUnpackData(BitData unpack_data)
        {
            unpack_data_ = unpack_data;

            return(UpdateFromUnpackData());
        }
Exemple #15
0
 protected virtual bool OnUnpackValueToUnpackData(object unpack_value, ref BitData unpack_data)
 {
     return(true);
 }
 public ProtocolDecodeEvent_BitData(ProtocolDecodeChannel channel, DateTime dt_block, DateTime dt_event, BitData bitdata)
     : base(channel, dt_block, dt_event, DecodeEventType.Data)
 {
     Data = bitdata;
 }
Exemple #17
0
        public bool SetPackData(BitData pack_data)
        {
            pack_data_ = new BitData(pack_data.Data, Math.Min(PackDataBitLength, pack_data.Length));

            return(UpdateFromPackData());
        }
Exemple #18
0
        byte[] SearchFor(Bitmap capture, ushort step, ref string extra, ref ushort max, ref uint length, ref ushort width, ref ushort height)
        {
            if (capture == null)
            {
                return(null);
            }

            // BLACK&WHITE
            if (ImageMode == EImageMode.BlackAndWhite)
            {
                /*    using (Graphics gr = Graphics.FromImage(capture))
                 *  {
                 *      float[][] gray_matrix = new float[][]
                 *      {
                 *          new float[] { 0.299f, 0.299f, 0.299f, 0, 0 },
                 *          new float[] { 0.587f, 0.587f, 0.587f, 0, 0 },
                 *          new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
                 *          new float[] { 0,      0,      0,      1, 0 },
                 *          new float[] { 0,      0,      0,      0, 1 }
                 *      };
                 *
                 *      ImageAttributes ia = new ImageAttributes();
                 *      ia.SetColorMatrix(new ColorMatrix(gray_matrix));
                 *      ia.SetThreshold(0.8F); // Change this threshold as needed
                 *      Rectangle rc = new Rectangle(0, 0, capture.Width, capture.Height);
                 *      gr.DrawImage(capture, rc, 0, 0, capture.Width, capture.Height, GraphicsUnit.Pixel, ia);
                 *  }*/
            }

            // BYTE ARRAY
            BitData array = new BitData((ushort)capture.Width, (ushort)capture.Height, ImageMode);

            array.ExtraBN = extra;

            using (MemoryStream ms = new MemoryStream())
            {
                unsafe
                {
                    BitmapData bitmapData = capture.LockBits(new Rectangle(0, 0, capture.Width, capture.Height), ImageLockMode.ReadWrite, capture.PixelFormat);

                    int   bytesPerPixel  = Image.GetPixelFormatSize(capture.PixelFormat) / 8;
                    int   heightInPixels = bitmapData.Height;
                    int   widthInBytes   = bitmapData.Width * bytesPerPixel;
                    byte *ptrFirstPixel  = (byte *)bitmapData.Scan0;

                    for (int y = 0; y < heightInPixels; y++)
                    {
                        byte *currentLine = ptrFirstPixel + (y * bitmapData.Stride);
                        for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
                        {
                            int oldBlue  = currentLine[x];
                            int oldGreen = currentLine[x + 1];
                            int oldRed   = currentLine[x + 2];

                            switch (ImageMode)
                            {
                            case EImageMode.BlackAndWhite:
                                array.DataBN[y][x / bytesPerPixel] = array.ParseBN(oldRed, oldGreen, oldBlue);
                                break;

                            case EImageMode.RGB:
                                array.DataRGB[y][x / bytesPerPixel] = array.ParseRGB(oldRed, oldGreen, oldBlue);
                                break;
                            }
                        }
                    }
                    capture.UnlockBits(bitmapData);
                }

                // SEARCH HEADER
                byte[] HEADER = new byte[]
                {
                    0xB1, 0xA5, // MAGIC NUMBER
                    0x00, 0x00  // STEP
                };

                // FIRST FRAME
                //      {HEADER+STEP}+MAX+WIDTH+HEIGHT+SIZE  => 14*8 = 112
                // NEXT
                //      {HEADER+STEP}                        =>  4*8 =  32
                // 10110001101001010000000000000000
                byte[] bnHeader = BitConverter.GetBytes(step);
                Array.Copy(bnHeader, 0, HEADER, 2, bnHeader.Length);

                BitData.RGB[] rgbHeader = null;
                switch (ImageMode)
                {
                case EImageMode.BlackAndWhite:
                {
                    string sheader = "";
                    foreach (byte b in HEADER)
                    {
                        sheader += Convert.ToString(b, 2).PadLeft(8, '0');
                    }

                    bnHeader = new byte[sheader.Length];
                    for (int x = 0; x < bnHeader.Length; x++)
                    {
                        bnHeader[x] = sheader[x] == '1' ? (byte)1 : (byte)0;
                    }

                    break;
                }

                case EImageMode.RGB:
                {
                    rgbHeader = new BitData.RGB[] { new BitData.RGB(HEADER[0], HEADER[1], HEADER[2]), new BitData.RGB(HEADER[3], 0, 0) };
                    break;
                }
                }
                // Search header or use the BytesPeerLine
                for (int y = 0; y < array.Height; y++)
                {
                    int index;
                    if ((ImageMode == EImageMode.BlackAndWhite && SearchBytes(array.DataBN[y], bnHeader, out index)) ||
                        (ImageMode == EImageMode.RGB && SearchBytes(array.DataRGB[y], rgbHeader, out index)))
                    {
                        array.IndexOfY = y;
                        array.CurrentY = y;

                        array.IndexOfX = array.CurrentX = index;
                        if (ImageMode == EImageMode.BlackAndWhite)
                        {
                            array.CurrentX += bnHeader.Length;
                        }
                        else
                        {
                            BitData.RGB[] row = array.DataRGB[y];
                            BitData.RGB   cur = row[array.IndexOfX + 1];
                            array.ExtraRGB.AddRange(new byte[] { cur.G, cur.B });
                            array.CurrentX += 2;
                        }

                        // First frame
                        if (step == 0)
                        {
                            byte[] b = new byte[2];
                            if (!array.ReadByte(out b[0]) || !array.ReadByte(out b[1]))
                            {
                                extra = array.ExtraBN;
                                return(ms.ToArray());
                            }

                            max = BitConverter.ToUInt16(b, 0);

                            if (!array.ReadByte(out b[0]) || !array.ReadByte(out b[1]))
                            {
                                extra = array.ExtraBN;
                                return(ms.ToArray());
                            }

                            array.Width = width = BitConverter.ToUInt16(b, 0);

                            if (!array.ReadByte(out b[0]) || !array.ReadByte(out b[1]))
                            {
                                extra = array.ExtraBN;
                                return(ms.ToArray());
                            }

                            array.Height = height = BitConverter.ToUInt16(b, 0);

                            b = new byte[4];
                            if (!array.ReadByte(out b[0]) || !array.ReadByte(out b[1]) || !array.ReadByte(out b[2]) || !array.ReadByte(out b[3]))
                            {
                                extra = array.ExtraBN;
                                return(ms.ToArray());
                            }

                            array.Length = length = BitConverter.ToUInt32(b, 0);
                        }
                        else
                        {
                            array.Length = length;
                            array.Width  = width;
                            array.Height = height;
                        }

                        break;
                    }
                }

                if (array.Length == 0)
                {
                    extra = array.ExtraBN;
                    return(ms.ToArray());
                }

                byte r;
                while (array.Length > 0)
                {
                    if (!array.ReadByte(out r))
                    {
                        break;
                    }

                    ms.WriteByte(r);
                    array.Length--;
                }

                length = array.Length;
                extra  = array.ExtraBN;
                return(ms.ToArray());
            }
        }
Exemple #19
0
        protected virtual bool OnPackDataToUnpackData(BitData pack_data, ref BitData unpack_data)
        {
            unpack_data = pack_data;

            return(true);
        }
Exemple #20
0
        protected virtual bool OnUnpackDataToPackData(BitData unpack_data, ref BitData pack_data)
        {
            pack_data = unpack_data_;

            return(true);
        }
Exemple #21
0
 public void UnpackSegment(BitData bitdata)
 {
 }
Exemple #22
0
        protected override bool OnUnpackDataToUnpackValue(BitData unpack_data, ref object unpack_value)
        {
            unpack_value = unpack_data;

            return(true);
        }
Exemple #23
0
 private void OnUnpackSegment(BitData bitdata)
 {
 }
Exemple #24
0
        protected override bool OnUnpackValueToUnpackData(object unpack_value, ref BitData unpack_data)
        {
            unpack_data = unpack_value as BitData;

            return(unpack_data != null);
        }
Exemple #25
0
 public ProtocolFrameElement_BitData(ProtocolFrameElement parent, string name, uint bitlen, BitData value)
     : this(parent, name, bitlen)
 {
     SetUnpackValue(value);
 }
Exemple #26
0
 protected virtual bool OnUnpackDataToUnpackElements(BitData unpack_data)
 {
     return(true);
 }
 public void CreateBitDataEvent(DateTime dt_block, DateTime dt_event, BitData bitdata)
 {
     AddEvent(new ProtocolDecodeEvent_BitData(this, dt_block, dt_event, bitdata));
 }
Exemple #28
0
 public ProtocolFrameElement_BitData(ProtocolFrameElement parent, string name, BitData value)
     : this(parent, name, value.Length, value)
 {
     SetUnpackValue(value);
 }