Beispiel #1
0
        /// <summary>
        /// 指定のバッファから初期化
        /// </summary>
        /// <param name="buffer">フレームのバッファ</param>
        /// <param name="index">フレームの開始位置</param>
        /// <param name="count">フレームのバイト数</param>
        public Frame(byte[] buffer, int index, int count)
        {
            using (var memoryStream = new MemoryStream(buffer, index, count, false))
                using (var binaryReader = new BinaryReader(memoryStream))
                {
                    // データ長チェック
                    if (count < MinimumSize)
                    {
                        throw new DamagedDataException($"データ長が 共通フレームの長さに満たない。{nameof(count)}={count} {nameof(MinimumSize)}={MinimumSize}");
                    }
                    //
                    Header = binaryReader.ReadUInt16();

                    DebugWriteLine($"{nameof(Header)}={Header.ToString("X4")}");

                    if (Header != MagicNumber)
                    {
                        // 対応してるデータではない
                        throw new NotSupportedException($"Header={Header.ToString("X4")}");
                    }
                    Length = binaryReader.ReadUInt16();

                    DebugWriteLine($"{nameof(Length)}={Length}");

                    // データ長チェック
                    {
                        // 本来あるべき全体のサイズ
                        var requestedLength = Length + TopSize;
                        if (count < requestedLength)
                        {
                            throw new DamagedDataException($"データ長が短い。{nameof(count)}={count} {nameof(requestedLength)}={requestedLength}");
                        }
                    }
                    // CRCチェック
                    {
                        memoryStream.Seek(0, SeekOrigin.Begin);// 最初に戻る
                        var tempBuffer = new byte[Length + TopSize - CRC16Size];
                        memoryStream.Read(tempBuffer, 0, tempBuffer.Length);
                        var computedCRC16 = crc16.ComputeHash(tempBuffer);
                        CRC16 = binaryReader.ReadUInt16();

                        DebugWriteLine($"{nameof(CRC16)}={CRC16}");

                        if (CRC16 != computedCRC16)
                        {
                            // CRCの結果 データ破損
                            throw new DamagedDataException($"CRC値の不一致。{nameof(CRC16)}={CRC16.ToString("X4")} {nameof(computedCRC16)}={computedCRC16.ToString("X4")}");
                        }
                    }
                    // Payload読み込み
                    {
                        Payload = ResponsePayload.Create(binaryReader, TopSize);
                    }
                }
        }
Beispiel #2
0
        private void btnCRC16_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            Encoding enc = Encoding.GetEncoding(950);

            byte[] source = enc.GetBytes(txtCrcSource.Text);
            watch.Start();
            Crc16 crc = new Crc16();

            labCrc16Res.Text  = Crc16.Compute(source).ToString("X");
            labCrc16Byte.Text = crc.ComputeHash(source).ToHexString();
            watch.Stop();
            labCrc16Time.Text = watch.ElapsedTicks.ToString() + " Ticks";
            watch.Start();
            Crc16 modbus = new Crc16(Crc16.DefaultPolynomial, Crc16.ModbusSeed);

            labCrc16ResM.Text  = Crc16.Compute(source, Crc16.DefaultPolynomial, Crc16.ModbusSeed).ToString("X");
            labCrc16ByteM.Text = modbus.ComputeHash(source).ToHexString();
            watch.Stop();
            labCrc16TimeM.Text = watch.ElapsedTicks.ToString() + " Ticks";
            watch.Start();
            Crc32 crc32 = new Crc32();

            labCrc32Res.Text  = Crc32.Compute(source).ToString("X");
            labCrc32Byte.Text = crc32.ComputeHash(source).ToHexString();
            watch.Stop();
            labCrc32Time.Text = watch.ElapsedTicks.ToString() + " Ticks";
        }
Beispiel #3
0
        public void Default()
        {
            string expected = "0x178C";
            var    crc      = new Crc16();

            crc.ComputeHash(Encoding.ASCII.GetBytes("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
            Assert.Equal(expected, $"0x{crc.HashAsInt16:X4}");
            Assert.Equal(expected, "0x" + BitConverter.ToString(crc.Hash).Replace("-", ""));
        }
Beispiel #4
0
 internal bool ChecksumsMatch(IMessage message, byte[] messageFrame)
 {
     if (message.MessageFrame.Length < (messageFrame.Length - 2))
     {
         return(false);
     }
     // Compute the hash
     using (Crc16 Hash = new Crc16())
     {
         Hash.Initialize();
         var crc = BitConverter.ToUInt16(Hash.ComputeHash(message.MessageFrame, 0, (messageFrame.Length - 2)), 0);
         return(BitConverter.ToUInt16(messageFrame, messageFrame.Length - 2) == crc);
     }
 }
Beispiel #5
0
        internal byte[] BuildMessageFrame(IMODBUSMessage message)
        {
            List <byte> messageBody = new List <byte>();

            messageBody.AddRange(message.MessageFrame);

            using (Crc16 Hash = new Crc16())
            {
                Hash.Initialize();
                messageBody.AddRange(Hash.ComputeHash(message.MessageFrame, 0, messageBody.Count));
            }

            return(messageBody.ToArray());
        }
Beispiel #6
0
        public void ShouldComputeCrc16()
        {
            var crc16 = new Crc16();

            // ISO/IEC13239: This should lead to the CRC 0x52ED
            byte[] source =
            {
                0x02, 0x07, 0x01, 0x03, 0x01, 0x02, 0x00, 0x34, 0x07, 0x07, 0x1C, 0x59, 0x34, 0x6F, 0xE1,
                0x83, 0x00, 0x00, 0x41, 0x06, 0x06, 0x7B, 0x3C, 0xFF, 0xCF, 0x3C, 0xC0
            };

            var hash1 = crc16.ComputeHash(source);

            Assert.AreEqual("52ED", BitConverter.ToString(hash1).Replace("-", ""));
        }
Beispiel #7
0
        private void btnCrcFile_Click(object sender, EventArgs e)
        {
            btnFile_Click(btnCrcFile, new EventArgs());
            Application.DoEvents();
            int times = 1000;

            byte[]       bs = File.ReadAllBytes(txtCrcFile.Text);
            FileStream   fs = File.OpenRead(txtCrcFile.Text);
            MemoryStream ms = new MemoryStream(bs);

            byte[] res = null;
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            Crc16 crc16 = Crc16.Create();

            watch.Start();
            for (int i = 0; i < times; i++)
            {
                res = crc16.ComputeHash(bs);
            }
            watch.Stop();
            lab16B.Text = res.ToHexString("") + " > " + (watch.ElapsedTicks / times).ToString();
            Application.DoEvents();
            watch.Restart();
            for (int i = 0; i < times; i++)
            {
                res = crc16.ComputeHash(fs);
            }
            watch.Stop();
            lab16S.Text = res.ToHexString("") + " > " + (watch.ElapsedTicks / times).ToString();
            Application.DoEvents();
            watch.Restart();
            for (int i = 0; i < times; i++)
            {
                res = crc16.ComputeHash(ms);
            }
            watch.Stop();
            lab16M.Text = res.ToHexString("") + " > " + (watch.ElapsedTicks / times).ToString();
            Application.DoEvents();
            Crc32 crc32 = Crc32.Create();

            watch.Start();
            for (int i = 0; i < times; i++)
            {
                res = crc32.ComputeHash(bs);
            }
            watch.Stop();
            lab32B.Text = res.ToHexString("") + " > " + (watch.ElapsedTicks / times).ToString();
            Application.DoEvents();
            watch.Restart();
            for (int i = 0; i < times; i++)
            {
                res = crc32.ComputeHash(fs);
            }
            watch.Stop();
            lab32S.Text = res.ToHexString("") + " > " + (watch.ElapsedTicks / times).ToString();
            Application.DoEvents();
            watch.Restart();
            for (int i = 0; i < times; i++)
            {
                res = crc32.ComputeHash(ms);
            }
            watch.Stop();
            lab32M.Text = res.ToHexString("") + " > " + (watch.ElapsedTicks / times).ToString();
            Application.DoEvents();
        }
        /// <summary>
        /// Removes the Hdlc Transparency from a List of Byte
        /// </summary>
        /// <param name="Bytes">The List of Byte to remove the Hdlc Transparency from</param>
        /// <param name="IncludeHdlcFields">Specifies the Hdlc Fields retrieved should be included in the the returned Bytes</param>
        /// <returns>A List of Bytes which have been decoded from a Transparent state</returns>
        public static List <byte> PmppDecode(this List <byte> Bytes, bool IncludeHdlcFields, Transport.SnmpMessenger messengerSession)
        {
            if (Bytes.Count == 0)
            {
                return(Bytes);
            }
            if (Bytes.First() != 0x7e && Bytes.Last() != 0x7e)
            {
                int firstByte = Bytes.IndexOf(PMPP);
                int lastByte  = Bytes.LastIndexOf(PMPP);
                if (firstByte == lastByte)
                {
                    //offsets are the same....
                    return(Bytes);
                }
                Bytes = Bytes.GetRange(firstByte, lastByte);
            }

            //Removes PMPP Frameing Chars
            Bytes.RemoveAt(0);
            Bytes.RemoveAt(Bytes.Count - 1);

            //Remove Transparency
            Int32 Position = 0;

            List <byte> Decoded = new List <byte>();

            if (!Bytes.Any(b => b == PMPP || b == PMPP_ESCAPE))
            {
                Decoded.AddRange(Bytes);
            }
            else
            {
                for (int end = Bytes.Count; Position < end; ++Position)
                {
                    if (Bytes[Position] == PMPP_ESCAPE)
                    {
                        if (Bytes[Position + 1] == 0x5e)
                        {
                            Decoded.Add(PMPP);
                            ++Position;
                        }
                        else if (Bytes[Position + 1] == 0x5d)
                        {
                            Decoded.Add(PMPP_ESCAPE);
                            ++Position;
                        }
                    }
                    else
                    {
                        Decoded.Add(Bytes[Position]);
                    }
                }
            }



            //Get Checksum
            List <byte> Checksum = Decoded.GetRange(Decoded.Count - 2, 2);

            //Remove Checksum from the working range
            Decoded.RemoveRange(Decoded.Count - 2, 2);

            //Calculate a Crc16 on the InformationPayload
            Crc16 alg = new Crc16();

            alg.ComputeHash(Decoded.ToArray());

            int?packetChecksum = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToUInt16(Checksum.ToArray(), 0));
            int?pseudoChecksum = System.Net.IPAddress.HostToNetworkOrder((int)alg.CrcValue);

            alg      = null;
            Checksum = null;

            //Compare Checksums
            if (packetChecksum != pseudoChecksum)
            {
                if (null != messengerSession)
                {
                    messengerSession.PmppCrcErrors++;
                }
            }
            packetChecksum = null;
            pseudoChecksum = null;

            //back at the beginning to decode the Hdlc fields
            Position = 0;

            List <byte> HdlcAddress = new List <byte>();
            Byte?       HdlcControlField;
            List <byte> HdlcProtocolIdentifier = new List <byte>();

            HdlcAddress            = GetHdlcField(ref Decoded, ref Position);
            HdlcControlField       = Bytes[Position++];
            HdlcProtocolIdentifier = GetHdlcField(ref Decoded, ref Position);


            if (!IncludeHdlcFields)
            {
                //Remove the fields we retrieved
                Decoded.RemoveRange(0, Position);
            }

            HdlcAddress            = null;
            HdlcControlField       = null;
            HdlcProtocolIdentifier = null;

            Bytes.Clear();

            Bytes.AddRange(Decoded);

            return(Bytes);
        }