Exemple #1
0
        public static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: Arduino.sample <PortName>");
                Console.WriteLine("i.e.: Arduino.sample COM4");
                return;
            }

            string portName = args[0];

            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.AddConsole();
            });

            // Statically register our factory. Note that this must be done before instantiation of any class that wants to use logging.
            LogDispatcher.LoggerFactory = loggerFactory;

            using (var port = new SerialPort(portName, 115200))
            {
                Console.WriteLine($"Connecting to Arduino on {portName}");
                try
                {
                    port.Open();
                }
                catch (UnauthorizedAccessException x)
                {
                    Console.WriteLine($"Could not open COM port: {x.Message} Possible reason: Arduino IDE connected or serial console open");
                    return;
                }

                ArduinoBoard board = new ArduinoBoard(port.BaseStream);
                try
                {
                    // This implicitly connects
                    Console.WriteLine($"Connecting... Firmware version: {board.FirmwareVersion}, Builder: {board.FirmwareName}");
                    while (Menu(board))
                    {
                    }
                }
                catch (TimeoutException x)
                {
                    Console.WriteLine($"No answer from board: {x.Message} ");
                }
                finally
                {
                    port.Close();
                    board?.Dispose();
                }
            }
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: Arduino.sample <PortName>");
                Console.WriteLine("i.e.: Arduino.sample COM4");
                return;
            }

            string portName = args[0];

            using (var port = new SerialPort(portName, 115200))
            {
                Console.WriteLine($"Connecting to Arduino on {portName}");
                try
                {
                    port.Open();
                }
                catch (UnauthorizedAccessException x)
                {
                    Console.WriteLine($"Could not open COM port: {x.Message} Possible reason: Arduino IDE connected or serial console open");
                    return;
                }

                ArduinoBoard board = new ArduinoBoard(port.BaseStream);
                try
                {
                    board.LogMessages += BoardOnLogMessages;
                    // This implicitly connects
                    Console.WriteLine($"Connecting... Firmware version: {board.FirmwareVersion}, Builder: {board.FirmwareName}");
                    while (Menu(board))
                    {
                    }
                }
                catch (TimeoutException x)
                {
                    Console.WriteLine($"No answer from board: {x.Message} ");
                }
                finally
                {
                    port.Close();
                    board?.Dispose();
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Main entry point
        /// </summary>
        /// <param name="args">The first argument gives the Port name. Default "COM4"</param>
        public static void Main(string[] args)
        {
            string portName = "COM4";

            if (args.Length > 0)
            {
                portName = args[0];
            }

            using (var port = new SerialPort(portName, 115200))
            {
                Console.WriteLine($"Connecting to Arduino on {portName}");
                try
                {
                    port.Open();
                }
                catch (UnauthorizedAccessException x)
                {
                    Console.WriteLine($"Could not open COM port: {x.Message} Possible reason: Arduino IDE connected or serial console open");
                    return;
                }

                ArduinoBoard board = new ArduinoBoard(port.BaseStream);
                try
                {
                    board.LogMessages += BoardOnLogMessages;
                    Console.WriteLine($"Firmware version: {board.FirmwareVersion}, Builder: {board.FirmwareName}");
                    DisplayModes(board);
                }
                catch (TimeoutException x)
                {
                    Console.WriteLine($"No answer from board: {x.Message} ");
                }
                finally
                {
                    port.Close();
                    board?.Dispose();
                }
            }
        }
Exemple #4
0
        private static void ConnectWithStream(Stream stream)
        {
            ArduinoBoard board = new ArduinoBoard(stream);

            try
            {
                Console.WriteLine(
                    $"Connection successful. Firmware version: {board.FirmwareVersion}, Builder: {board.FirmwareName}");
                while (Menu(board))
                {
                }
            }
            catch (TimeoutException x)
            {
                Console.WriteLine($"No answer from board: {x.Message} ");
            }
            finally
            {
                stream.Close();
                board?.Dispose();
            }
        }
Exemple #5
0
 public void Dispose()
 {
     _board.Dispose();
 }