Example #1
0
        public async Task Start(string portName, int baudRate, CancellationTokenSource cts)
        {
            Debug.WriteLine("Element:CommunicationTask: Start:   port: {0}  baudRate: {1}", portName, baudRate);

            //serialPort = new CommunicationChannelBT();

            serialChannel = new CommunicationChannelSerial(cts)
            {
                Name     = portName,
                BaudRate = (uint)baudRate
            };

            try
            {
                Debug.WriteLine("IP: Element:CommunicationTask: serialPort " + portName + " opening...");
                await serialChannel.Open();

                Debug.WriteLine("OK: Element:CommunicationTask: serialPort " + portName + " opened");

                //await Task.Delay(2000);

                // Notify users to initialize any devices
                // they have before we start processing commands:
                element.StartingCommunication(serialChannel);

                running = true;
                ClearJobQueues();

                Task workerTask = new Task(() => ThreadProc(cts.Token), cts.Token);
                workerTask.Start();

                Debug.WriteLine("IP: Element:CommunicationTask: trying to synch up with the board - resetting...");

                // try to synch up with the board
                string resp = EnqueueCommJobAndWait(Priority.High, "reset");

                //await Task.Delay(3000); // let other processes continue

                // This HAS TO BE HERE...Since it takes the Element about a second to boot up
                // using the new Tiny Bootloader...
                //Thread.Sleep(1000);
                await Task.Delay(2000);

                // Clear receive buffer out, since the bootloader can send
                // some junk characters, which might hose subsequent command responses:
                serialChannel.DiscardInBuffer();
                stopWatch.Start();
            }
            catch (AggregateException aexc)
            {
                throw new CommunicationException("CommunicationTask: Could not start communication");
            }
            catch (Exception exc)
            {
                Debug.WriteLine("Error: Element:CommunicationTask: exception while opening serial port " + portName + " : " + exc);
                //await Stop();
                //throw new CommunicationException("CommunicationTask: Could not start communication");
                throw;
            }
        }
Example #2
0
        public async Task Stop()
        {
            running = false;
            stopWatch.Stop();
            //commandsBufferBlock.Complete();
            actionBlock.Complete();

            Flush();

            await Task.Delay(100);

            // Notify users to clean up any devices
            // connected to the serial port if need be, before closing it.
            arduinoBrick.StoppingCommunication(serialChannel);

            if (serialChannel != null)
            {
                serialChannel.DiscardInBuffer();
                serialChannel.Close();
                serialChannel = null;
            }
        }
        public async Task Start(string portName, int baudRate, CancellationTokenSource cts)
        {
            Debug.WriteLine("Element:CommunicationTask: Start:   port: {0}  baudRate: {1}", portName, baudRate);

            //serialPort = new CommunicationChannelBT();

            serialChannel = new CommunicationChannelSerial(cts)
            {
                Name = portName,
                BaudRate = (uint)baudRate
            };

            try
            {
                Debug.WriteLine("IP: Element:CommunicationTask: serialPort " + portName + " opening...");
                await serialChannel.Open();
                Debug.WriteLine("OK: Element:CommunicationTask: serialPort " + portName + " opened");

                //await Task.Delay(2000);

                // Notify users to initialize any devices
                // they have before we start processing commands:
                element.StartingCommunication(serialChannel);

                running = true;
                ClearJobQueues();

                Task workerTask = new Task(() => ThreadProc(cts.Token), cts.Token);
                workerTask.Start();

                Debug.WriteLine("IP: Element:CommunicationTask: trying to synch up with the board - resetting...");

                // try to synch up with the board
                string resp = EnqueueCommJobAndWait(Priority.High, "reset");

                //await Task.Delay(3000); // let other processes continue

                // This HAS TO BE HERE...Since it takes the Element about a second to boot up
                // using the new Tiny Bootloader...
                //Thread.Sleep(1000);
                await Task.Delay(2000);

                // Clear receive buffer out, since the bootloader can send
                // some junk characters, which might hose subsequent command responses:
                serialChannel.DiscardInBuffer();
                stopWatch.Start();
            }
            catch (AggregateException aexc)
            {
                throw new CommunicationException("CommunicationTask: Could not start communication");
            }
            catch (Exception exc)
            {
                Debug.WriteLine("Error: Element:CommunicationTask: exception while opening serial port " + portName + " : " + exc);
                //await Stop();
                //throw new CommunicationException("CommunicationTask: Could not start communication");
                throw;
            }
        }