Exemple #1
0
        /// <summary>
        /// Sends a CID packet to the device.
        /// </summary>
        /// <param name="packet">The packet to send.</param>
        public void SendPacket(CidPacket packet)
        {
            m_baseStream.Write(new byte[] { 0x02 }, 0, 1);
            m_baseStream.Write(packet.Command, 0, packet.Command.Length);
            int fcs = 0;

            for (int i = 0; i < packet.Command.Length; i++)
            {
                fcs = fcs ^ packet.Command[i];
            }
            byte len = 0;

            if (packet.Body != null && packet.Body.Length > 0)
            {
                len = (byte)packet.Body.Length;
                fcs = fcs ^ len;
                m_baseStream.Write(new byte[] { len }, 0, 1);
                m_baseStream.Write(packet.Body, 0, packet.Body.Length);
                for (int i = 0; i < packet.Body.Length; i++)
                {
                    fcs = fcs ^ packet.Body[i];
                }
            }
            else
            {
                m_baseStream.Write(new byte[] { 0x00 }, 0, 1);
            }
            fcs = 0xFF;
            m_baseStream.Write(new byte[] { (byte)fcs }, 0, 1);
            System.Diagnostics.Debug.WriteLine("Sending CMD: 0x{0}\tPAYLOAD: [{1}]",
                                               string.Join("", (from c in packet.Command select c.ToString("X2"))),
                                               packet.Body == null ? "<EMPTY>" : string.Join(",", (from c in packet.Body select c.ToString("X2"))));
        }
Exemple #2
0
        public Task <T> SendPacketAndReceiveAsync <T>(CidPacket packet, int timeout = 60) where T : ZigbeeCommand
        {
            TaskCompletionSource <T>     tcs     = new TaskCompletionSource <T>();
            EventHandler <ZigbeeCommand> handler = null;

            handler = (s, e) =>
            {
                if (e.GetType() == typeof(T))
                {
                    ResponseReceived -= handler;
                    tcs.SetResult((T)e);
                }
            };
            Task.Delay(timeout * 1000).ContinueWith(_ =>
            {
                if (!tcs.Task.IsCompleted && !tcs.Task.IsCanceled)
                {
                    ResponseReceived -= handler;
                    tcs.SetException(new TimeoutException());
                }
                ;
            });
            ResponseReceived += handler;
            SendPacket(packet);
            return(tcs.Task);
        }