コード例 #1
0
ファイル: Packet.cs プロジェクト: Inventors-Way/Inventors.ECP
        public byte[] ToArray()
        {
            byte[] retValue;

            if (Extended)
            {
                int offset = GetDataOffset();
                retValue    = new byte[GetPacketSize()];
                retValue[0] = Code;
                EncodeFormat(retValue);
                EncodeLength(retValue);

                if (AddressEnabled)
                {
                    retValue[2 + GetLengthSize()] = Address;
                }

                for (int i = 0; i < _length; ++i)
                {
                    retValue[i + offset] = data[i];
                }

                switch (ChecksumAlgorithm)
                {
                case ChecksumAlgorithmType.Additive:
                    _checksum = AdditiveChecksum.Calculate(retValue, retValue.Length - 1);
                    retValue[retValue.Length - 1] = _checksum;
                    break;

                case ChecksumAlgorithmType.CRC8CCIT:
                    _checksum = CRC8CCITT.Calculate(retValue, retValue.Length - 1);
                    retValue[retValue.Length - 1] = _checksum;
                    break;

                default:
                    break;
                }
            }
            else
            {
                int offset = 2;
                retValue    = new byte[_length + offset];
                retValue[0] = Code;
                retValue[1] = (byte)_length;

                for (int i = 0; i < _length; ++i)
                {
                    retValue[i + offset] = data[i];
                }
            }

            return(retValue);
        }
コード例 #2
0
ファイル: Packet.cs プロジェクト: Inventors-Way/Inventors.ECP
        public Packet(byte[] frame)
        {
            if (!(frame is object))
            {
                throw new ArgumentException(Resources.FRAME_IS_NULL);
            }

            if (frame.Length < 2)
            {
                throw new PacketFormatException(Resources.INVALID_FRAME_TOO_SHORT);
            }

            // Parsing the code byte
            _code = frame[0];

            // Parsing the format byte
            if (frame[1] < 128)
            {
                _length         = frame[1];
                _lengthEncoding = LengthEncodingType.UInt8Encoding;
                Address         = 0;
                _checksumType   = ChecksumAlgorithmType.None;
            }
            else
            {
                // Parse the length encoding
                switch ((LengthEncodingType)(0x03 & frame[1]))
                {
                case LengthEncodingType.UInt8Encoding:
                    _lengthEncoding = LengthEncodingType.UInt8Encoding;
                    break;

                case LengthEncodingType.UInt16Encoding:
                    _lengthEncoding = LengthEncodingType.UInt16Encoding;
                    break;

                case LengthEncodingType.UInt32Encoding:
                    _lengthEncoding = LengthEncodingType.UInt32Encoding;
                    break;

                default:
                    throw new InvalidOperationException($"Invalid length encoding [ {0x03 & frame[1]} ]");
                }

                // Parse the checksum field
                switch ((ChecksumAlgorithmType)(0x0C & frame[1]))
                {
                case ChecksumAlgorithmType.None:
                    _checksumType = ChecksumAlgorithmType.None;
                    break;

                case ChecksumAlgorithmType.Additive:
                    _checksumType = ChecksumAlgorithmType.Additive;
                    break;

                case ChecksumAlgorithmType.CRC8CCIT:
                    _checksumType = ChecksumAlgorithmType.CRC8CCIT;
                    break;

                default:
                    throw new InvalidOperationException($"Invalid checksum type [ {0x0C & frame[1]} ]");
                }

                // Parse the address bit.
                if ((0x10 & frame[1]) != 0)
                {
                    Address = frame[2 + GetLengthSize()];
                }
            }

            if (Extended)
            {
                int offset = GetDataOffset();
                _length = DecodeLength(frame);
                data    = new byte[_length];


                for (int i = 0; i < _length; ++i)
                {
                    data[i] = frame[i + offset];
                }

                // Validate checksum
                switch (ChecksumAlgorithm)
                {
                case ChecksumAlgorithmType.Additive:
                {
                    _checksum = frame[GetChecksumOffset()];
                    byte actualChecksum = AdditiveChecksum.Calculate(frame, frame.Length - 1);

                    if (_checksum != actualChecksum)
                    {
                        throw new InvalidOperationException($"Checksum incorrect (expected: {_checksum}, actual: {actualChecksum})");
                    }
                }
                break;

                case ChecksumAlgorithmType.CRC8CCIT:
                {
                    _checksum = frame[GetChecksumOffset()];
                    byte actualChecksum = CRC8CCITT.Calculate(frame, frame.Length - 1);

                    if (_checksum != actualChecksum)
                    {
                        throw new InvalidOperationException($"Checksum incorrect (expected: {_checksum}, actual: {actualChecksum})");
                    }
                }
                break;

                default:
                    break;
                }
            }
            else // Standard frame
            {
                data = new byte[_length];
                int offset = 2;

                for (int i = 0; i < _length; ++i)
                {
                    data[i] = frame[i + offset];
                }
            }
        }