示例#1
0
        private bool IsValidChecksum()
        {
            ushort checkSum = GetChecksum(dataLength - 2);

            return(checkSum == Crc16.ComputeChecksum(buffer, 1, dataLength - 2));
        }
示例#2
0
        public bool Read(byte data)
        {
            if (position == 0 && data != 0x7E)
            {
                // we haven't started yet, wait for the start flag (no need to capture any data yet)
                return(false);
            }
            else
            {
                // We have completed reading of one package, so clear and be ready for the next
                if (dataLength > 0 && position >= dataLength + 2)
                {
                    Clear();
                }

                // Check if we're about to run into a buffer overflow
                if (position >= buffer.Length)
                {
                    Clear();
                }

                // Check if this is a second start flag, which indicates the previous one was a stop from the last package
                if (position == 1 && data == 0x7E)
                {
                    // just return, we can keep the one byte we had in the buffer
                    return(false);
                }

                // We have started, so capture every byte
                buffer[position++] = data;

                if (position == 1)
                {
                    // This was the start flag, we're not done yet
                    return(false);
                }
                else if (position == 2)
                {
                    // Capture the Frame Format Type
                    frameFormatType = (byte)(data & 0xF0);
                    if (!IsValidFrameFormat(frameFormatType))
                    {
                        Clear();
                    }
                    return(false);
                }
                else if (position == 3)
                {
                    // Capture the length of the data package
                    dataLength = ((buffer[1] & 0x0F) << 8) | buffer[2];
                    return(false);
                }
                else if (destinationAddress == null)
                {
                    // Capture the destination address
                    destinationAddress = GetAddress(3);
                    if (destinationAddress?.Length > 3)
                    {
                        Clear();
                    }
                    return(false);
                }
                else if (sourceAddress == null)
                {
                    // Capture the source address
                    sourceAddress = GetAddress(3 + destinationAddress.Length);
                    if (sourceAddress?.Length > 3)
                    {
                        Clear();
                    }
                    return(false);
                }
                else if (position == 4 + destinationAddress.Length + sourceAddress.Length + 2)
                {
                    // Verify the header checksum
                    var headerChecksum = GetChecksum(position - 3);
                    if (headerChecksum != Crc16.ComputeChecksum(buffer, 1, position - 3))
                    {
                        Clear();
                    }
                    return(false);
                }
                else if (position == dataLength + 1)
                {
                    // Verify the data package checksum
                    var checksum = GetChecksum(position - 3);
                    if (checksum != Crc16.ComputeChecksum(buffer, 1, position - 3))
                    {
                        Clear();
                    }
                    return(false);
                }
                else if (position == dataLength + 2)
                {
                    // We're done, check the stop flag and signal we're done
                    if (data == 0x7E)
                    {
                        return(true);
                    }
                    else
                    {
                        Clear();
                        return(false);
                    }
                }
            }
            return(false);
        }