コード例 #1
0
        public void Test()
        {
            XBeeGetDetailedVersionCommand command = new XBeeGetDetailedVersionCommand();

            command.SetFrameId(0);
            _output.WriteLine(command.ToString());

            int[] arrayToTestAgainst         = new int[] { 0, 4, 8, 0, 86, 76, 85 };
            int[] commandSerializationResult = command.Serialize();

            Assert.True(arrayToTestAgainst.SequenceEqual(commandSerializationResult));
        }
コード例 #2
0
        public ZigBeeStatus Initialize()
        {
            Log.Debug("XBee device initialize.");

            if (!_serialPort.Open())
            {
                Log.Error("Unable to open XBee serial port");
                return(ZigBeeStatus.COMMUNICATION_ERROR);
            }

            // Create and start the frame handler
            _frameHandler = new XBeeFrameHandler();
            _frameHandler.Start(_serialPort);
            _frameHandler.AddEventListener(this);

            // Reset to a known state
            // Device sends WATCHDOG_TIMER_RESET event
            // A retry mechanism is used as sometimes the reset response is not received.
            // This appears to happen if there are other events queued in the stick.
            int resetCount = 0;

            do
            {
                if (resetCount >= MAX_RESET_RETRIES)
                {
                    Log.Information($"XBee device reset failed after {++resetCount}");
                    return(ZigBeeStatus.NO_RESPONSE);
                }
                Log.Debug($"XBee device reset {++resetCount}");
                XBeeSetSoftwareResetCommand resetCommand = new XBeeSetSoftwareResetCommand();
                _frameHandler.SendRequest(resetCommand);
            } while (_frameHandler.EventWait(typeof(XBeeModemStatusEvent)) == null);


            // Enable the API with escaping
            XBeeSetApiEnableCommand apiEnableCommand = new XBeeSetApiEnableCommand();

            apiEnableCommand.SetMode(2);
            _frameHandler.SendRequest(apiEnableCommand);

            // Set the API mode so we receive detailed data including ZDO
            XBeeSetApiModeCommand apiModeCommand = new XBeeSetApiModeCommand();

            apiModeCommand.SetMode(3);
            _frameHandler.SendRequest(apiModeCommand);

            // Get the product information
            XBeeGetHardwareVersionCommand hwVersionCommand = new XBeeGetHardwareVersionCommand();

            _frameHandler.SendRequest(hwVersionCommand);

            XBeeGetFirmwareVersionCommand fwVersionCommand = new XBeeGetFirmwareVersionCommand();

            _frameHandler.SendRequest(fwVersionCommand);

            XBeeGetDetailedVersionCommand versionCommand = new XBeeGetDetailedVersionCommand();

            _frameHandler.SendRequest(versionCommand);

            // Get Ieee Address
            XBeeGetIeeeAddressHighCommand ieeeHighCommand  = new XBeeGetIeeeAddressHighCommand();
            XBeeIeeeAddressHighResponse   ieeeHighResponse = (XBeeIeeeAddressHighResponse)_frameHandler.SendRequest(ieeeHighCommand);

            XBeeGetIeeeAddressLowCommand ieeeLowCommand  = new XBeeGetIeeeAddressLowCommand();
            XBeeIeeeAddressLowResponse   ieeeLowResponse = (XBeeIeeeAddressLowResponse)_frameHandler.SendRequest(ieeeLowCommand);

            if (ieeeHighResponse == null || ieeeLowCommand == null)
            {
                Log.Error("Unable to get XBee IEEE address");
                return(ZigBeeStatus.BAD_RESPONSE);
            }

            byte[] tmpAddress = new byte[8];
            tmpAddress[0] = (byte)ieeeLowResponse.GetIeeeAddress()[3];
            tmpAddress[1] = (byte)ieeeLowResponse.GetIeeeAddress()[2];
            tmpAddress[2] = (byte)ieeeLowResponse.GetIeeeAddress()[1];
            tmpAddress[3] = (byte)ieeeLowResponse.GetIeeeAddress()[0];
            tmpAddress[4] = (byte)ieeeHighResponse.GetIeeeAddress()[3];
            tmpAddress[5] = (byte)ieeeHighResponse.GetIeeeAddress()[2];
            tmpAddress[6] = (byte)ieeeHighResponse.GetIeeeAddress()[1];
            tmpAddress[7] = (byte)ieeeHighResponse.GetIeeeAddress()[0];
            IeeeAddress   = new IeeeAddress(tmpAddress);

            Log.Debug($"XBee IeeeAddress={IeeeAddress}");

            // Set the ZigBee stack profile
            XBeeSetZigbeeStackProfileCommand stackProfile = new XBeeSetZigbeeStackProfileCommand();

            stackProfile.SetStackProfile(2);
            _frameHandler.SendRequest(stackProfile);

            // Enable Security
            XBeeSetEncryptionEnableCommand enableEncryption = new XBeeSetEncryptionEnableCommand();

            enableEncryption.SetEnableEncryption(true);
            _frameHandler.SendRequest(enableEncryption);

            XBeeSetEncryptionOptionsCommand encryptionOptions = new XBeeSetEncryptionOptionsCommand();

            encryptionOptions.AddEncryptionOptions(EncryptionOptions.ENABLE_TRUST_CENTRE);
            _frameHandler.SendRequest(encryptionOptions);

            // Enable coordinator mode
            XBeeSetCoordinatorEnableCommand coordinatorEnable = new XBeeSetCoordinatorEnableCommand();

            coordinatorEnable.SetEnable(true);
            _frameHandler.SendRequest(coordinatorEnable);

            // Set the network key
            XBeeSetNetworkKeyCommand networkKey = new XBeeSetNetworkKeyCommand();

            networkKey.SetNetworkKey(new ZigBeeKey());
            _frameHandler.SendRequest(networkKey);

            // Set the link key
            XBeeSetLinkKeyCommand setLinkKey = new XBeeSetLinkKeyCommand();

            setLinkKey.SetLinkKey(LinkKey);
            _frameHandler.SendRequest(setLinkKey);

            // Save the configuration in the XBee
            XBeeSetSaveDataCommand saveData = new XBeeSetSaveDataCommand();

            _frameHandler.SendRequest(saveData);

            // Get network information
            XBeeGetPanIdCommand getPanId      = new XBeeGetPanIdCommand();
            XBeePanIdResponse   panIdResponse = (XBeePanIdResponse)_frameHandler.SendRequest(getPanId);

            // TODO: Check if cast is appropriate. Possible loss of data.
            PanID = (ushort)panIdResponse.GetPanId();

            XBeeGetExtendedPanIdCommand getEPanId      = new XBeeGetExtendedPanIdCommand();
            XBeeExtendedPanIdResponse   epanIdResponse = (XBeeExtendedPanIdResponse)_frameHandler.SendRequest(getEPanId);

            ExtendedPanId = epanIdResponse.GetExtendedPanId();

            return(ZigBeeStatus.SUCCESS);
        }