コード例 #1
0
ファイル: XBeeController.cs プロジェクト: splitice/XBee
        /// <summary>
        /// Create a node.
        /// </summary>
        /// <param name="address">The address of the node or null for the controller node.</param>
        /// <param name="autodetectHardwareVersion">If true query node for hardware version.  Otherwise assume controller version.</param>
        /// <returns>The specified node.</returns>
        public async Task <XBeeNode> GetNodeAsync(NodeAddress address = null, bool autodetectHardwareVersion = true)
        {
            await Initialize();

            if (address == null)
            {
                return(Local);
            }

            HardwareVersion version;

            if (autodetectHardwareVersion)
            {
                if (!IsOpen)
                {
                    throw new InvalidOperationException("Connection closed.");
                }

                version = await
                          TaskExtensions.Retry(async() => await GetHardwareVersion(address), TimeSpan.FromSeconds(5),
                                               typeof(TimeoutException), typeof(AtCommandException));
            }
            else
            {
                version = Local.HardwareVersion;
            }

            return(await Task.FromResult(CreateNode(version, address)));
        }
コード例 #2
0
ファイル: XBeeController.cs プロジェクト: macntouch/XBee
        /// <summary>
        /// Open a local node.
        /// </summary>
        /// <param name="port">The COM port of the node</param>
        /// <param name="baudRate">The baud rate, typically 9600 or 115200 depending on the model</param>
        /// <returns></returns>
        public async Task OpenAsync(string port, int baudRate)
#endif
        {
            if (IsOpen)
            {
                throw new InvalidOperationException("The controller is already conntected, please close the existing connection.");
            }

#if WINDOWS_UWP
            _connection = new SerialConnection(device);
#else
            _connection = new SerialConnection(port, baudRate);
#endif

            try
            {
                _connection.MemberSerializing   += OnMemberSerializing;
                _connection.MemberSerialized    += OnMemberSerialized;
                _connection.MemberDeserializing += OnMemberDeserializing;
                _connection.MemberDeserialized  += OnMemberDeserialized;

                _connection.FrameReceived += OnFrameReceived;
                _connection.Open();

                /* Unfortunately the protocol changes based on what type of hardware we're using... */
                HardwareVersionResponseData response =
                    await ExecuteAtQueryAsync <HardwareVersionResponseData>(new HardwareVersionCommand());

                HardwareVersion = response.HardwareVersion;
                _connection.CoordinatorHardwareVersion = HardwareVersion;

                /* We want the receiver to have the hardware version in context so cycle the connection */
                _connection.Close();

                /* Stupid serial ports... */
                await
                TaskExtensions.Retry(() => _connection.Open(), typeof(UnauthorizedAccessException),
                                     TimeSpan.FromSeconds(3));

                Local = CreateNode(HardwareVersion);
            }
            catch (Exception)
            {
                Close();
                throw;
            }
        }
コード例 #3
0
ファイル: XBeeController.cs プロジェクト: splitice/XBee
        private async Task Initialize()
        {
            if (_isInitialized)
            {
                return;
            }

            await _initializeSemaphoreSlim.WaitAsync();

            if (_isInitialized)
            {
                return;
            }

            try
            {
                /* Unfortunately the protocol changes based on what type of hardware we're using... */
                HardwareVersion = await GetHardwareVersion();

                _connection.CoordinatorHardwareVersion = HardwareVersion;

                /* We want the receiver to have the hardware version in context so cycle the connection */
                _connection.Close();

                // This is to address a .net (windows?) serial port management issue
                await TaskExtensions.Retry(() => _connection.Open(),
                                           TimeSpan.FromSeconds(3), typeof(UnauthorizedAccessException));

                // ReSharper disable PossibleInvalidOperationException
                Local = CreateNode(HardwareVersion.Value);
                // ReSharper restore PossibleInvalidOperationException

                _isInitialized = true;
            }
            finally
            {
                _initializeSemaphoreSlim.Release();
            }
        }