예제 #1
0
        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();
            }
        }
예제 #2
0
        public void Close()
        {
            if (IsOpen)
            {
                _connection.MemberSerializing   -= OnMemberSerializing;
                _connection.MemberSerialized    -= OnMemberSerialized;
                _connection.MemberDeserializing -= OnMemberDeserializing;
                _connection.MemberDeserialized  -= OnMemberDeserialized;

                _connection.Close();
                _connection.Dispose();
                _connection = null;
            }
        }
예제 #3
0
        /// <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;
            }
        }