示例#1
0
        public byte[] GetHeader()
        {
            byte[] destinationEndPoint = null;
            byte[] profileId           = null;
            if (m_endPoint == null)
            {
                destinationEndPoint = DESTINATION_ENDPOINT;
                profileId           = PROFILE_ID;
            }
            else
            {
                destinationEndPoint    = new byte[sizeof(byte)];
                destinationEndPoint[0] = m_endPoint.Id;
                profileId = AdapterHelper.ToXbeeFrame(m_endPoint.CommandProfileId);
            }
            // set destination (mac address and network address)
            byte[] macAddress     = null;
            byte[] networkAddress = null;
            if (null != m_destination)
            {
                // send to specific device
                macAddress     = AdapterHelper.ToXbeeFrame(m_destination.MacAddress);
                networkAddress = AdapterHelper.ToXbeeFrame(m_destination.NetworkAddress);
            }
            else
            {
                // broadcast
                macAddress     = AdapterHelper.ToXbeeFrame(AdapterHelper.BROADCAST_MAC_ADDRESS);
                networkAddress = AdapterHelper.ToXbeeFrame(AdapterHelper.UNKNOWN_NETWORK_ADDRESS);
            }

            // set cluster ID
            byte[] clusterId = AdapterHelper.ToXbeeFrame(m_clusterId);

            // create and set header buffer
            int offset     = 0;
            int headerSize = macAddress.Length + networkAddress.Length + SOURCE_ENDPOINT.Length + DESTINATION_ENDPOINT.Length +
                             clusterId.Length + PROFILE_ID.Length + BROADCAST_RADIUS.Length + TRANSMIT_OPTIONS.Length;

            byte[] header = new byte[headerSize];

            offset = AdapterHelper.AddByteToBuffer(ref header, ref macAddress, offset);
            offset = AdapterHelper.AddByteToBuffer(ref header, ref networkAddress, offset);
            offset = AdapterHelper.AddByteToBuffer(ref header, ref SOURCE_ENDPOINT, offset);
            offset = AdapterHelper.AddByteToBuffer(ref header, ref destinationEndPoint, offset);
            offset = AdapterHelper.AddByteToBuffer(ref header, ref clusterId, offset);
            offset = AdapterHelper.AddByteToBuffer(ref header, ref profileId, offset);
            offset = AdapterHelper.AddByteToBuffer(ref header, ref BROADCAST_RADIUS, offset);
            offset = AdapterHelper.AddByteToBuffer(ref header, ref TRANSMIT_OPTIONS, offset);

            return(header);
        }
示例#2
0
        private async void SendATCmdLocalInternal(XBeeATCommand command, bool responseRequired, byte[] tempCmdId)
        {
            int headerSize      = 0;
            int commandSize     = 0;
            int nbOfBytesToSend = 0;
            int offset          = 0;

            byte[] atCommand = command.ATCommand;

            // allocate send buffer
            headerSize  = COMMAND_PREFIX.Length + sizeof(UInt16);
            commandSize = AT_COMMAND_LOCAL_SEND.Length + m_commandId.Length + atCommand.Length;
            if (null != command.Payload)
            {
                commandSize += command.Payload.Length;
            }
            nbOfBytesToSend = headerSize + commandSize + CHECKSUM_LENGTH;
            byte[] buffer = new byte[nbOfBytesToSend];

            // add headers
            offset = SetCommandHeader(ref buffer, offset, commandSize, ref AT_COMMAND_LOCAL_SEND, ref tempCmdId);

            // add payload
            offset = AdapterHelper.AddByteToBuffer(ref buffer, ref atCommand, offset);
            if (null != command.Payload)
            {
                byte[] payload = command.Payload;
                offset = AdapterHelper.AddByteToBuffer(ref buffer, ref payload, offset);
            }

            // add checksum
            buffer[offset] = CheckSum(ref buffer, headerSize, commandSize);

            if (responseRequired)
            {
                lock (m_locker)
                {
                    // add new awaited response in list
                    if (!m_pendingATCmdList.ContainsKey(tempCmdId[0]))
                    {
                        m_pendingATCmdList.Add(tempCmdId[0], command);
                    }
                }
            }

            // send command
            await m_serialController.WriteAsync(buffer);

            Debug.WriteLineIf(Logger.IsVerbose(), "command sent", command.ToString());
        }
示例#3
0
        private int SetCommandHeader(ref byte[] buffer, int initialOffset, int commandSize, ref byte[] xbeeCommand, ref byte[] commandId)
        {
            int offset = initialOffset;

            // add headers (frame and at command)
            offset = AdapterHelper.AddByteToBuffer(ref buffer, ref COMMAND_PREFIX, offset);

            UInt16 tempWord = Convert.ToUInt16(commandSize);

            byte[] tempBytes = AdapterHelper.ToXbeeFrame(tempWord);
            offset = AdapterHelper.AddByteToBuffer(ref buffer, ref tempBytes, offset);

            offset = AdapterHelper.AddByteToBuffer(ref buffer, ref xbeeCommand, offset);
            offset = AdapterHelper.AddByteToBuffer(ref buffer, ref commandId, offset);

            return(offset);
        }
示例#4
0
        private async void SendZigBeeCommandInternal(ZigBeeCommand command, bool responseRequired, byte[] tempSequenceNumber)
        {
            int headerSize      = 0;
            int commandSize     = 0;
            int nbOfBytesToSend = 0;
            int offset          = 0;

            // allocate send buffer
            headerSize = COMMAND_PREFIX.Length + sizeof(UInt16);
            byte[] zigBeeCommandHeader = command.GetHeader();
            commandSize = EXPLICIT_ADDRESSING_FRAME.Length + m_commandId.Length + zigBeeCommandHeader.Length;
            if (command.IsZdoCommand)
            {
                // note that sequence number is part of payload in case of ZCL command
                commandSize += tempSequenceNumber.Length;
            }
            if (null != command.Payload)
            {
                commandSize += command.Payload.Length;
            }
            nbOfBytesToSend = headerSize + commandSize + CHECKSUM_LENGTH;
            byte[] buffer = new byte[nbOfBytesToSend];

            // add headers (frame and XBee command)
            byte[] tempCmdId = NextCommandId();
            offset = SetCommandHeader(ref buffer, offset, commandSize, ref EXPLICIT_ADDRESSING_FRAME, ref tempCmdId);

            // add payload
            offset = AdapterHelper.AddByteToBuffer(ref buffer, ref zigBeeCommandHeader, offset);
            if (command.IsZdoCommand)
            {
                offset = AdapterHelper.AddByteToBuffer(ref buffer, ref tempSequenceNumber, offset);
            }
            if (null != command.Payload)
            {
                byte[] payload = command.Payload;
                if (!command.IsZdoCommand &&
                    payload.Length >= 2)
                {
                    // sequence number is 2nd byte of payload for ZCL command
                    payload[1] = tempSequenceNumber[0];
                }
                offset = AdapterHelper.AddByteToBuffer(ref buffer, ref payload, offset);
            }

            // add checksum
            buffer[offset] = CheckSum(ref buffer, headerSize, commandSize);

            if (responseRequired)
            {
                lock (m_locker)
                {
                    // add new awaited response in list
                    if (!m_pendingZigBeeCmdList.ContainsKey(tempSequenceNumber[0]))
                    {
                        m_pendingZigBeeCmdList.Add(tempSequenceNumber[0], command);
                    }
                }
            }

            LoggingServices loggingServices = new LoggingServices();

            loggingServices.WriteLine <XBeeModule>("XBeeModule Send = [" + BitConverter.ToString(buffer) + "]");

            // send command
            await m_serialController.WriteAsync(buffer);

            Debug.WriteLineIf(Logger.IsVerbose(), "command sent", command.ToString());
        }