예제 #1
0
        private async void UARTconnect()
        {
            try
            {
                var dis = await DeviceInformation.FindAllAsync(
                    SerialDevice.GetDeviceSelector("UART0"));

                serialPort = await SerialDevice.FromIdAsync(dis[0].Id);

                if (serialPort == null)
                {
                    throw new IOException();
                }
                // Configure serial settings
                serialPort.WriteTimeout = TimeSpan.FromMilliseconds(13);
                serialPort.ReadTimeout  = TimeSpan.FromMilliseconds(13);
                serialPort.BaudRate     = 76800;
                serialPort.Parity       = SerialParity.None;
                serialPort.StopBits     = SerialStopBitCount.One;
                serialPort.DataBits     = 8;
                serialPort.Handshake    = SerialHandshake.None;

                // Create the DataWriter object and attach to OutputStream
                dataWriteObject = new DataWriter(serialPort.OutputStream);
                BusReady?.Invoke();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
예제 #2
0
 public Handle(IEnumerable <HostHandle> hostHandles, IEnumerable <ReceiveEndpointHandle> endpointHandles, IEnumerable <ConnectHandle> observerHandles,
               IBus bus, IBusObserver busObserver, BusReady ready)
 {
     _bus             = bus;
     _busObserver     = busObserver;
     _endpointHandles = endpointHandles.ToArray();
     _hostHandles     = hostHandles.ToArray();
     _observerHandles = observerHandles.ToArray();
     Ready            = ready.Ready;
 }
예제 #3
0
        public async Task <BusHandle> StartAsync(CancellationToken cancellationToken)
        {
            if (_busHandle != null)
            {
                _log.Warn($"The bus was already started, additional Start attempts are ignored: {Address}");
                return(_busHandle);
            }

            await _busObservable.PreStart(this).ConfigureAwait(false);

            Handle busHandle = null;

            var endpoints = new List <ReceiveEndpointHandle>();
            var hosts     = new List <HostHandle>();
            var observers = new List <ConnectHandle>();
            var busReady  = new BusReady(_receiveEndpoints);

            try
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Starting bus hosts...");
                }

                foreach (IBusHostControl host in _hosts)
                {
                    HostHandle hostHandle = host.Start();

                    hosts.Add(hostHandle);
                }

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Starting receive endpoints...");
                }

                foreach (IReceiveEndpoint endpoint in _receiveEndpoints)
                {
                    ConnectHandle observerHandle = endpoint.ConnectReceiveObserver(_receiveObservers);
                    observers.Add(observerHandle);

                    ReceiveEndpointHandle handle = endpoint.Start();

                    endpoints.Add(handle);
                }

                busHandle = new Handle(hosts, endpoints, observers, this, _busObservable, busReady);

                await busHandle.Ready.WithCancellation(cancellationToken).ConfigureAwait(false);

                await _busObservable.PostStart(this, busReady.Ready).ConfigureAwait(false);

                _busHandle = busHandle;

                return(_busHandle);
            }
            catch (Exception ex)
            {
                try
                {
                    if (busHandle != null)
                    {
                        busHandle.Stop(TimeSpan.FromSeconds(60));
                    }
                    else
                    {
                        var handle = new Handle(hosts, endpoints, observers, this, _busObservable, busReady);

                        handle.Stop(TimeSpan.FromSeconds(60));
                    }
                }
                catch (Exception stopException)
                {
                    _log.Error("Failed to stop partially created bus", stopException);
                }

                await _busObservable.StartFaulted(this, ex).ConfigureAwait(false);

                throw;
            }
        }
예제 #4
0
        BusHandle IBusControl.Start()
        {
            if (_busHandle != null)
            {
                _log.Warn($"The bus was already started, additional Start attempts are ignored: {Address}");
                return(_busHandle);
            }

            TaskUtil.Await(() => _busObservable.PreStart(this));

            Exception exception = null;

            var endpoints = new List <ReceiveEndpointHandle>();
            var hosts     = new List <HostHandle>();
            var observers = new List <ConnectHandle>();
            var busReady  = new BusReady(_receiveEndpoints);

            try
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Starting bus hosts...");
                }

                foreach (IBusHostControl host in _hosts)
                {
                    try
                    {
                        HostHandle hostHandle = host.Start();

                        hosts.Add(hostHandle);
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                }

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Starting receive endpoints...");
                }

                foreach (IReceiveEndpoint endpoint in _receiveEndpoints)
                {
                    try
                    {
                        ConnectHandle observerHandle = endpoint.ConnectReceiveObserver(_receiveObservers);
                        observers.Add(observerHandle);

                        ReceiveEndpointHandle handle = endpoint.Start();

                        endpoints.Add(handle);
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            if (exception != null)
            {
                try
                {
                    var handle = new Handle(hosts, endpoints, observers, this, _busObservable, busReady);

                    handle.Stop(TimeSpan.FromSeconds(60));
                }
                catch (Exception ex)
                {
                    _log.Error("Failed to stop partially created bus", ex);
                }

                TaskUtil.Await(() => _busObservable.StartFaulted(this, exception));

                throw new MassTransitException("The service bus could not be started.", exception);
            }

            _busHandle = new Handle(hosts, endpoints, observers, this, _busObservable, busReady);

            TaskUtil.Await(() => _busObservable.PostStart(this, busReady.Ready));

            return(_busHandle);
        }