예제 #1
0
        private void Connect(string port, int baudRate)
        {
            _connection = new SerialConnection(port, baudRate);

            if (_frameMemberDeserialized != null)
            {
                _connection.MemberDeserialized += _frameMemberDeserialized;
            }

            if (_frameMemberDeserializing != null)
            {
                _connection.MemberDeserializing += _frameMemberDeserializing;
            }

            if (_frameMemberSerialized != null)
            {
                _connection.MemberSerialized += _frameMemberSerialized;
            }

            if (_frameMemberDeserialized != null)
            {
                _connection.MemberDeserialized += _frameMemberDeserialized;
            }

            _connection.FrameReceived += OnFrameReceived;
        }
예제 #2
0
        public async Task OpenAsync(string port, int baudRate)
        {
            if (IsOpen)
            {
                throw new InvalidOperationException(
                          "The controller is already connected, please close the existing connection.");
            }

            _connection?.Dispose();

            if (_connection != null)
            {
                _connection.Open();
                return;
            }

            _connection = new SerialConnection(port, baudRate);

            try
            {
                Connect(port, baudRate);

                _connection.Open();

                await Initialize();
            }
            catch (Exception)
            {
                Close();
                throw;
            }
        }
예제 #3
0
        public void Close()
        {
            if (IsOpen)
            {
                _connection.MemberSerializing   -= OnMemberSerializing;
                _connection.MemberSerialized    -= OnMemberSerialized;
                _connection.MemberDeserializing -= OnMemberDeserializing;
                _connection.MemberDeserialized  -= OnMemberDeserialized;

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