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;
            }
        }
Example #2
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;
            }
        }
Example #3
0
 public async void Send(string s)
 {
     await serialChannel.WriteLine(s);
 }