コード例 #1
0
        private readonly byte[] _packetData;    // TSIP data packet

        public TsipPacket()
        {
            _counter    = 0;
            _length     = 0;
            _status     = TsipParserStatus.Empty;
            PacketType  = 0;
            _packetData = new byte[MaxRptbuf];
        }
コード例 #2
0
        private TsipParserStatus _status; // TSIP packet format/parse status

        #endregion Fields

        #region Constructors

        public TsipPacket()
        {
            _counter = 0;
            _length = 0;
            _status = TsipParserStatus.Empty;
            PacketType = 0;
            _packetData = new byte[MaxRptbuf];
        }
コード例 #3
0
        /// <summary>
        /// Accumulates bytes from the receiver, strips control bytes (DLE)
        /// and checks for a valid packet end sequence (DLE ETX).
        /// Note: Use the IsComplete property to determine if a complete packet is available.
        /// </summary>
        public void AddByte(UInt16 inbyte)
        {
            // avoid bogus bytes
            if ((inbyte & (UInt16)0xFF00) != 0)
            {
                return;
            }

            var newbyte = (byte)(inbyte & 0x00FF);

            switch (this._status)
            {
            case TsipParserStatus.DLE1:
            {
                switch (newbyte)
                {
                case 0:
                case (byte)TsipControlBytes.ETX:                // illegal TSIP id
                    //Debug.Print("Parse Error: Illegal TSIP id = {0:X}", newbyte);
                    _length = 0;
                    _status = TsipParserStatus.Empty;
                    break;

                case (byte)TsipControlBytes.DLE:                // try normal message start again
                    _length = 0;
                    _status = TsipParserStatus.DLE1;
                    break;

                default:                                        // legal TSIP ID; start message
                    PacketType = newbyte;
                    _length    = 0;
                    _status    = TsipParserStatus.Data;
                    break;
                }
                break;
            }

            case TsipParserStatus.Data:
            {
                switch (newbyte)
                {
                case (byte)TsipControlBytes.DLE:             // expect DLE or ETX next
                    _status = TsipParserStatus.DLE2;
                    break;

                default:             // normal data here
                    _packetData[this._length++] = newbyte;
                    break;
                }
                break;
            }

            case TsipParserStatus.DLE2:
            {
                switch (newbyte)
                {
                case (byte)TsipControlBytes.DLE:             // normal data byte
                    _packetData[this._length++] = newbyte;
                    _status = TsipParserStatus.Data;
                    break;

                case (byte)TsipControlBytes.ETX:             // end of message
                    _status = TsipParserStatus.Full;
                    break;

                default:             // error: treat as DLE1; start a new report packet
                    Debug.Print("Parse Error: Treat as DLE1");
                    PacketType = newbyte;
                    _length    = 0;
                    _status    = TsipParserStatus.Data;
                    break;
                }
                break;
            }

            case TsipParserStatus.Full:
            case TsipParserStatus.Empty:
            default:
            {
                switch (newbyte)
                {
                case (byte)TsipControlBytes.DLE:             // normal message start
                    _length = 0;
                    _status = TsipParserStatus.DLE1;
                    break;

                default:             // error: ignore the new byte
                    //Debug.Print("Parse Error: {1} Ignore new byte {0:X}", newbyte, this.status.ToString());
                    _length = 0;
                    _status = TsipParserStatus.Empty;
                    break;
                }
                break;
            }
            }
            if (_length >= MaxRptbuf)
            {
                //Debug.Print("Parse Error: {1} Buffer Length Exceeded {0}", this.length, this.status.ToString());
                _length = 0;
                _status = TsipParserStatus.Empty;
            }
        }
コード例 #4
0
        /// <summary>
        /// Accumulates bytes from the receiver, strips control bytes (DLE)
        /// and checks for a valid packet end sequence (DLE ETX).
        /// Note: Use the IsComplete property to determine if a complete packet is available.
        /// </summary>
        public void AddByte(UInt16 inbyte)
        {
            // avoid bogus bytes
            if ((inbyte & (UInt16)0xFF00) != 0)
                return;

            var newbyte = (byte)(inbyte & 0x00FF);
            switch (this._status)
            {
                case TsipParserStatus.DLE1:
                    {
                        switch (newbyte)
                        {
                            case 0:
                            case (byte)TsipControlBytes.ETX:    // illegal TSIP id
                                //Debug.Print("Parse Error: Illegal TSIP id = {0:X}", newbyte);
                                _length = 0;
                                _status = TsipParserStatus.Empty;
                                break;
                            case (byte)TsipControlBytes.DLE:    // try normal message start again
                                _length = 0;
                                _status = TsipParserStatus.DLE1;
                                break;
                            default:                            // legal TSIP ID; start message
                                PacketType = newbyte;
                                _length = 0;
                                _status = TsipParserStatus.Data;
                                break;
                        }
                        break;
                    }
                case TsipParserStatus.Data:
                    {
                        switch (newbyte)
                        {
                            case (byte)TsipControlBytes.DLE: // expect DLE or ETX next
                                _status = TsipParserStatus.DLE2;
                                break;
                            default: // normal data here
                                _packetData[this._length++] = newbyte;
                                break;
                        }
                        break;
                    }
                case TsipParserStatus.DLE2:
                    {
                        switch (newbyte)
                        {
                            case (byte)TsipControlBytes.DLE: // normal data byte
                                _packetData[this._length++] = newbyte;
                                _status = TsipParserStatus.Data;
                                break;
                            case (byte)TsipControlBytes.ETX: // end of message
                                _status = TsipParserStatus.Full;
                                break;
                            default: // error: treat as DLE1; start a new report packet
                                Debug.Print("Parse Error: Treat as DLE1");
                                PacketType = newbyte;
                                _length = 0;
                                _status = TsipParserStatus.Data;
                                break;
                        }
                        break;
                    }
                case TsipParserStatus.Full:
                case TsipParserStatus.Empty:
                default:
                    {
                        switch (newbyte)
                        {
                            case (byte)TsipControlBytes.DLE: // normal message start
                                _length = 0;
                                _status = TsipParserStatus.DLE1;
                                break;
                            default: // error: ignore the new byte
                                //Debug.Print("Parse Error: {1} Ignore new byte {0:X}", newbyte, this.status.ToString());
                                _length = 0;
                                _status = TsipParserStatus.Empty;
                                break;
                        }
                        break;
                    }
            }
            if (_length >= MaxRptbuf)
            {
                //Debug.Print("Parse Error: {1} Buffer Length Exceeded {0}", this.length, this.status.ToString());
                _length = 0;
                _status = TsipParserStatus.Empty;
            }
        }