Пример #1
0
 protected void InitFilter(XBeeRequest request)
 {
     if (Filter == null)
     {
         Filter = request is AtCommand
                    ? new AtResponseFilter((AtCommand)request)
                    : new PacketIdFilter(request);
     }
     else if (Filter is PacketIdFilter)
     {
         (Filter as PacketIdFilter).ExpectedPacketId = request.FrameId;
     }
 }
Пример #2
0
        /// <summary>
        /// Performs the necessary activities to construct an XBee packet from the frame data.
        /// This includes: computing the checksum, escaping the necessary bytes, adding the start byte and length bytes.
        /// The format of a packet is as follows:
        /// start byte - msb length byte - lsb length byte - frame data - checksum byte
        /// </summary>
        /// <param name="request"></param>
        public static byte[] GetBytes(XBeeRequest request)
        {
            var frameData = request.GetFrameData();

            // packet size is frame data + start byte + 2 length bytes + checksum byte
            var bytes = new byte[frameData.Length + 4];

            bytes[0] = (byte)SpecialByte.StartByte;

            // Packet length does not include escape bytes or start, length and checksum bytes
            var length = (ushort)frameData.Length;

            // msb length (will be zero until maybe someday when > 255 bytes packets are supported)
            bytes[1] = UshortUtils.Msb(length);
            // lsb length
            bytes[2] = UshortUtils.Lsb(length);

            Array.Copy(frameData, 0, bytes, 3, frameData.Length);

            // set last byte as checksum
            // note: if checksum is not correct, XBee won't send out packet or return error.  ask me how I know.

            bytes[bytes.Length - 1] = Checksum.Compute(frameData, 0, frameData.Length);

            var preEscapeLength = bytes.Length;

            // TODO save escaping for the serial out method. this is an unnecessary operation
            bytes = EscapePacket(bytes);

            var escapeLength = bytes.Length;

            var packetStr = "Packet: ";

            for (var i = 0; i < escapeLength; i++)
            {
                packetStr += ByteUtils.ToBase16(bytes[i]);

                if (i < escapeLength - 1)
                {
                    packetStr += " ";
                }
            }

            Logger.LowDebug(packetStr);
            Logger.LowDebug("pre-escape packet size is " + preEscapeLength + ", post-escape packet size is " + escapeLength);

            return(bytes);
        }
Пример #3
0
        /// <summary>
        /// Performs the necessary activities to construct an XBee packet from the frame data.
        /// This includes: computing the checksum, escaping the necessary bytes, adding the start byte and length bytes.
        /// The format of a packet is as follows:
        /// start byte - msb length byte - lsb length byte - frame data - checksum byte
        /// </summary>
        /// <param name="request"></param>
        public static byte[] GetBytes(XBeeRequest request)
        {
            var frameData = request.GetFrameData();

            // packet size is frame data + start byte + 2 length bytes + checksum byte
            var bytes = new byte[frameData.Length + 4];
            bytes[0] = (byte)SpecialByte.StartByte;

            // Packet length does not include escape bytes or start, length and checksum bytes
            var length = (ushort)frameData.Length;

            // msb length (will be zero until maybe someday when > 255 bytes packets are supported)
            bytes[1] = UshortUtils.Msb(length);
            // lsb length
            bytes[2] = UshortUtils.Lsb(length);

            Array.Copy(frameData, 0, bytes, 3, frameData.Length);

            // set last byte as checksum
            // note: if checksum is not correct, XBee won't send out packet or return error.  ask me how I know.

            bytes[bytes.Length - 1] = Checksum.Compute(frameData, 0, frameData.Length);

            var preEscapeLength = bytes.Length;

            //// TODO save escaping for the serial out method. this is an unnecessary operation
            //bytes = EscapePacket(bytes);

            //var escapeLength = bytes.Length;

            //var packetStr = "Packet: ";
            //for (var i = 0; i < escapeLength; i++)
            //{
            //    packetStr += ByteUtils.ToBase16(bytes[i]);

            //    if (i < escapeLength - 1)
            //        packetStr += " ";
            //}

            //Logger.LowDebug(packetStr);
            //Logger.LowDebug("pre-escape packet size is " + preEscapeLength + ", post-escape packet size is " + escapeLength);

            return bytes;
        }
Пример #4
0
        protected void SendRequest(XBeeRequest request)
        {
            IsRequestSupported(request);

            if (_addressLookupEnabled)
                _currentRequest = request;

            if (Logger.IsActive(LogLevel.Debug))
                Logger.Debug("Sending " + request.GetType().Name + ": " + request);

            var bytes = XBeePacket.GetBytes(request);

            if (Logger.IsActive(LogLevel.LowDebug))
                Logger.LowDebug("Sending " + ByteUtils.ToBase16(bytes));

            _connection.Send(bytes);
        }
Пример #5
0
        protected void IsRequestSupported(XBeeRequest request)
        {
            // can be null when reading Config
            if (Config == null)
                return;

            if (Config.IsSeries1() && request is IZigbeePacket)
                throw new ArgumentException("You are connected to a Series 1 radio but attempting to send Series 2 requests");

            if (Config.IsSeries2() && request is IWpanPacket)
                throw new ArgumentException("You are connected to a Series 2 radio but attempting to send Series 1 requests");
        }
Пример #6
0
 public void SendNoReply(XBeeRequest request)
 {
     // we don't expect any response to this request
     request.FrameId = PacketIdGenerator.NoResponseId;
     SendRequest(request);
 }
Пример #7
0
 public RawRequest Send(XBeeRequest request)
 {
     _rawRequest.Init(request);
     return _rawRequest;
 }
Пример #8
0
 public void BeginSend(XBeeRequest request, ResponseHandler responseHandler, IPacketFilter filter = null, int timeout = 5000)
 {
     AddPacketListener(new PacketListener(filter, timeout, responseHandler));
     SendRequest(request);
 }
Пример #9
0
 public AsyncSendResult BeginSend(XBeeRequest request, IPacketFilter filter = null, int timeout = 5000)
 {
     var responseListener = new PacketListener(filter, timeout);
     AddPacketListener(responseListener);
     SendRequest(request);
     return new AsyncSendResult(this, responseListener);
 }
Пример #10
0
 protected void InitFilter(XBeeRequest request)
 {
     if (Filter == null)
     {
         Filter = request is AtCommand
                    ? new AtResponseFilter((AtCommand)request)
                    : new PacketIdFilter(request);
     }
     else if (Filter is PacketIdFilter)
     {
         (Filter as PacketIdFilter).ExpectedPacketId = request.FrameId;
     }
 }