Example #1
0
        private async Task <string> SerialPortReadLine()
        {
            // this is specific to Element board:
            serialChannel.NewLineIn = "\r\n>";
            string str = await serialChannel.ReadLine();

            if (str.StartsWith("\r\n"))
            {
                return(str.Length > 2 ? str.Substring(2) : String.Empty);    // remove trailing CRLF, coming from Element board
            }
            else
            {
                return(str);
            }
        }
Example #2
0
        private async void ThreadProc(CancellationToken ct)
        {
            Debug.WriteLine("PixyCamera: Started Worker Task");

            while (!ct.IsCancellationRequested)
            {
                if (!running)
                {
                    break;
                }

                string line = await serialChannel.ReadLine();     // *70 128 26 18 1

                //Debug.WriteLine(line);
                interpretPixyValues(line);
            }
            running = false;

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

            cancellationTokenSource = cts;

            //serialPort = new CommunicationChannelBT();

            serialChannel = new CommunicationChannelSerial(cts)
            {
                Name       = portName,
                BaudRate   = (uint)baudRate,
                NewLineIn  = "\r\n>",
                NewLineOut = "\r"
            };

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

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

                //await Task.Delay(2000);

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

                //commandsBufferBlock = new BufferBlock<CommandAndResponse>(
                //                        new DataflowBlockOptions() { CancellationToken = cts.Token });

                actionBlock = new ActionBlock <CommandAndResponse>(
                    async prompt =>
                {
                    await serialChannel.WriteLine(prompt.command);
                    string response = await serialChannel.ReadLine();
                    prompt.completionSource.SetResult(response);
                },
                    new ExecutionDataflowBlockOptions()
                {
                    CancellationToken = cts.Token, BoundedCapacity = 10
                });

                //commandsBufferBlock.LinkTo(actionBlock, new DataflowLinkOptions { PropagateCompletion = true });

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

                string resp       = null;
                int    count      = 10;
                bool   boardFound = false;

                while (count-- > 0)
                {
                    // try to sync up with the board
                    resp = await SendAndReceive("reset");

                    Debug.WriteLine("OK: ArduinoBrick:CommunicationTask: 'reset' -> '" + resp + "'");

                    if (string.Equals(resp, "Arduino firmware Plucky Wheels"))
                    {
                        boardFound = true;
                        break;
                    }
                }

                if (boardFound)
                {
                    Debug.WriteLine("OK: ArduinoBrick:CommunicationTask: found Plucky Wheels Arduino brick");
                }
                else
                {
                    throw new CommunicationException("CommunicationTask: Could not find Plucky Wheels Arduino brick, invalid response to 'reset' at serial port " + portName);
                }

                stopWatch.Start();
                running = true;
            }
            catch (AggregateException aexc)
            {
                throw new CommunicationException("CommunicationTask: Could not start communication");
            }
            catch (Exception exc)
            {
                Debug.WriteLine("Error: ArduinoBrick:CommunicationTask: exception while opening serial port " + portName + " : " + exc);
                //await Stop();
                //throw new CommunicationException("CommunicationTask: Could not start communication");
                throw;
            }
        }
        public async Task Start(string portName, int baudRate, CancellationTokenSource cts)
        {
            Debug.WriteLine("ArduinoBrick:CommunicationTask: Start:   port: {0}  baudRate: {1}", portName, baudRate);

            cancellationTokenSource = cts;

            //serialPort = new CommunicationChannelBT();

            serialChannel = new CommunicationChannelSerial(cts)
            {
                Name = portName,
                BaudRate = (uint)baudRate,
                NewLineIn = "\r\n>",
                NewLineOut = "\r"
            };

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

                //await Task.Delay(2000);

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

                //commandsBufferBlock = new BufferBlock<CommandAndResponse>(
                //                        new DataflowBlockOptions() { CancellationToken = cts.Token });

                actionBlock = new ActionBlock<CommandAndResponse>(
                                        async prompt =>
                                        {
                                            await serialChannel.WriteLine(prompt.command);
                                            string response = await serialChannel.ReadLine();
                                            prompt.completionSource.SetResult(response);
                                        },
                                        new ExecutionDataflowBlockOptions() { CancellationToken = cts.Token, BoundedCapacity = 10 });

                //commandsBufferBlock.LinkTo(actionBlock, new DataflowLinkOptions { PropagateCompletion = true });

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

                string resp = null;
                int count = 10;
                bool boardFound = false;

                while (count-- > 0)
                {
                    // try to sync up with the board
                    resp = await SendAndReceive("reset");

                    Debug.WriteLine("OK: ArduinoBrick:CommunicationTask: 'reset' -> '" + resp + "'");

                    if (string.Equals(resp, "Arduino firmware Plucky Wheels"))
                    {
                        boardFound = true;
                        break;
                    }
                }

                if (boardFound)
                {
                    Debug.WriteLine("OK: ArduinoBrick:CommunicationTask: found Plucky Wheels Arduino brick");
                }
                else
                {
                    throw new CommunicationException("CommunicationTask: Could not find Plucky Wheels Arduino brick, invalid response to 'reset' at serial port " + portName);
                }

                stopWatch.Start();
                running = true;
            }
            catch (AggregateException aexc)
            {
                throw new CommunicationException("CommunicationTask: Could not start communication");
            }
            catch (Exception exc)
            {
                Debug.WriteLine("Error: ArduinoBrick:CommunicationTask: exception while opening serial port " + portName + " : " + exc);
                //await Stop();
                //throw new CommunicationException("CommunicationTask: Could not start communication");
                throw;
            }
        }