示例#1
0
        private XBeeResponse ParsePacket()
        {
            try
            {
                ParseStartTime = DateTime.Now;
                BytesRead      = 0;
                _checksum.Clear();

                // length of api structure, starting here (not including start byte or length bytes, or checksum)
                // length doesn't account for escaped bytes
                Length = UshortUtils.ToUshort(Read("Length MSB"), Read("Length LSB"));

                Logger.LowDebug("packet length is " + ByteUtils.ToBase16(Length));

                // total packet length = stated length + 1 start byte + 1 checksum byte + 2 length bytes

                ApiId = (ApiId)Read("API ID");

                Logger.LowDebug("Handling ApiId: " + ApiId);

                // TODO parse I/O data page 12. 82 API Identifier Byte for 64 bit address A/D data (83 is for 16bit A/D data)
                // TODO XBeeResponse should implement an abstract parse method

                _response = GetResponse(ApiId);

                if (_response == null)
                {
                    Logger.Warn("Did not find a response handler for ApiId [" + ByteUtils.ToBase16((byte)ApiId));
                    _response = new GenericResponse();
                }

                _response.Parse(this);
                _response.Checksum = Read("Checksum");

                if (RemainingBytes > 0)
                {
                    throw new XBeeParseException("There are remaining bytes after parsing the packet");
                }

                _response.Finish();
            }
            catch (Exception e)
            {
                Logger.Error("Failed to parse packet due to exception. " + e.Message);
                _response = new ErrorResponse {
                    ErrorMsg = e.Message, Exception = e
                };
            }
            finally
            {
                if (_response != null)
                {
                    _response.Length = Length;
                    _response.ApiId  = ApiId;
                }
            }

            return(_response);
        }