Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// If digital I/O line (DIO0) is enabled: returns true if digital 0 is HIGH (ON); false if it is LOW (OFF).
        /// If digital I/O line is not enabled this method returns false.
        /// </summary>
        /// <remarks>
        /// Important: the pin number corresponds to the logical pin (e.g. D4), not the physical pin number.
        /// Digital I/O pins seem to report high when open circuit (unconnected)
        /// </remarks>
        /// <param name="pin"></param>
        /// <returns></returns>
        public bool IsDigitalOn(Pin pin)
        {
            if (!IsDigitalEnabled(pin))
            {
                return(false);
            }

            var pinNumber = (byte)pin;

            if (pinNumber >= 0 && pinNumber <= 7)
            {
                return(ByteUtils.GetBit(UshortUtils.Lsb(Digital), pinNumber));
            }

            return(ByteUtils.GetBit(UshortUtils.Msb(Digital), pinNumber - 8));
        }
Exemplo n.º 3
0
        public bool IsDigitalEnabled(Pin pin)
        {
            var pinNumber = (byte)pin;

            if (pinNumber >= 0 && pinNumber <= 7)
            {
                return(ByteUtils.GetBit(UshortUtils.Lsb(DigitalChannelMask), pinNumber));
            }

            if (pinNumber >= 10 && pinNumber <= 12)
            {
                return(ByteUtils.GetBit(UshortUtils.Msb(DigitalChannelMask), pinNumber - 8));
            }

            throw new ArgumentOutOfRangeException("Unsupported pin: " + pin);
        }