Ejemplo n.º 1
0
        /// <summary>
        /// Try to connect
        /// </summary>
        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                if (_connection != null)
                {
                    _connection.Disconnect();
                }

                _connection = new SharerConnection(cbPort.Text, (int)udBaud.Value);

                _connection.UserDataReceived += _connection_UserDataReceived;

                _connection.Connect();

                Properties.Settings.Default.ComPort = cbPort.Text;
                Properties.Settings.Default.Save();

                refreshGUI();
            }
            catch (Exception ex)
            {
                handleException(ex);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
        public void connect()
        {
            try
            {
                connection = new SharerConnection(SharedVariables.ComPort, 38400);
                //connection = new SharerConnection(cmb_Port.Text, 115200);
                connection.Connect();
                connection.RefreshFunctions();
            }
            catch (Exception)
            {
                MessageBox.Show("Please Check Connection!!");
            }


            if (connection.Connected)
            {
                UpdateUItmr          = new DispatcherTimer();
                UpdateUItmr.Interval = new TimeSpan(0, 0, 0, 0, 100);
                UpdateUItmr.Tick    += UpdateUItmr_Tick;
                UpdateUItmr.Start();
                Arduino          = new DispatcherTimer();
                Arduino.Interval = new TimeSpan(0, 0, 0, 0, 100);
                Arduino.Tick    += Arduino_Tick;
                Arduino.Start();
                SampleBreak          = new DispatcherTimer();
                SampleBreak.Interval = new TimeSpan(0, 0, 0, 0, 1000);
                SampleBreak.Tick    += SampleBreak_Tick;
                SampleBreak.Start();
            }
            else
            {
                MessageBox.Show("Cannot Connect, Please check Cabling and Retry");
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Refresh available COM ports on drop down
 /// </summary>
 private void cbPort_DropDown(object sender, EventArgs e)
 {
     try
     {
         Cursor = Cursors.WaitCursor;
         cbPort.Items.Clear();
         cbPort.Items.AddRange(SharerConnection.GetSerialPortNames());
         refreshGUI();
     }
     catch (Exception ex)
     {
         handleException(ex);
     }
     finally
     {
         Cursor = Cursors.Default;
     }
 }
Ejemplo n.º 4
0
        public void Connect()
        {
            if (_connection != null && _connection.Connected)
            {
                return;
            }

            var config = GetHardwareConfig();

            if (config.Simulated)
            {
                return;
            }
            _connection = new SharerConnection(config.COMPort, config.BaudRate);

            _connection.Connect();

            _tmr = new Timer((state) => PeriodicGetInfo(), null, 0, 100);
        }
        private void ConnectionForm_Load(object sender, EventArgs e)
        {
            Cursor       = Cursors.WaitCursor;
            lbError.Text = "";
            cbPort.Items.Clear();
            cbPort.Items.AddRange(SharerConnection.GetSerialPortNames());
            if (cbPort.Items.Count > 0)
            {
                cbPort.SelectedIndex = 0;
            }
            else
            {
                lbError.Text = "Error: Arduino not found";
            }

            tbImageFolder.Text          = Application.StartupPath + "\\images";
            ArduinoSettings.ImageFolder = tbImageFolder.Text;

            Cursor = Cursors.Default;
        }
Ejemplo n.º 6
0
        private void conectarBoton_Click(object sender, EventArgs e)
        {
            try
            {
                string _puerto  = puerto.Text;
                int    _baudios = int.Parse(baudios.Text);
                connection = new SharerConnection(_puerto, _baudios);
                connection.Connect();
                trackBar1.Enabled     = true;
                desconectar.Enabled   = true;
                conectarBoton.Enabled = false;
            }

            catch (FormatException ex)
            {
                MessageBox.Show("Introducir valores de COM y baudios: " + ex.Message);
            }
            catch (System.IO.IOException ex)
            {
                MessageBox.Show("Introducir los valores correctos para la placa: " + ex.Message);
            }
        }
Ejemplo n.º 7
0
        public static int Main(string[] args)
        {
            try
            {
                Console.WriteLine("Sharer_Example build 11");
                Console.WriteLine("Connect Arduino to PC");
                Console.WriteLine("[Press any key to continue...]");
                Console.ReadKey();
                Console.WriteLine();
                List <string> _listPorts = new List <string>();
                foreach (var portName in SerialPort.GetPortNames())
                {
                    _listPorts.Add(portName);
                }
                Console.WriteLine("Serial ports available:");
                if (_listPorts.Count == 0)
                {
                    Console.WriteLine("No Serial Port Available");
                    Environment.Exit(-1);
                }
                string _portName = String.Empty; //Serial port
                do
                {
                    for (int i = 1; i <= _listPorts.Count; i++)
                    {
                        Console.WriteLine(String.Format("{0}. {1}", i, _listPorts[i - 1]));
                    }
                    Console.Write("Select the COM-port number and press Enter: ");
                    var _strSelectNumberPort = Console.ReadLine();
                    Console.WriteLine();
                    int _intSelectNumberPort;
                    if (int.TryParse(_strSelectNumberPort, out _intSelectNumberPort))
                    {
                        if (_intSelectNumberPort < _listPorts.Count + 1)
                        {
                            _portName = _listPorts[_intSelectNumberPort - 1];
                        }
                    }
                } while (_portName == String.Empty);
                // Connect to Arduino board
                Console.WriteLine("Select Port: " + _portName);
                var connection = new SharerConnection(_portName, 115200);
                //
                connection.Ready         += _connection_Ready;
                connection.InternalError += _connection_InternalError;
                Console.WriteLine("Connect...");
                connection.Connect();
                // Scan all functions shared
                if (connection != null && connection.Connected)
                {
                    //Only required for Linux
                    Task.Delay(2000).Wait(); // Wait 2 seconds with blocking
                    connection.RefreshFunctions();
                    //Only required for Linux
                    Task.Delay(2000).Wait(); // Wait 2 seconds with blocking
                    connection.RefreshVariables();
                }
                else
                {
                    Console.WriteLine("No Connection");
                    Environment.Exit(-1);
                }
                int _intSelectTask = 0;
                //
                do
                {
                    //Select Task
                    Console.WriteLine("Tasks:");
                    Console.WriteLine("1. The sum of two numbers A = 10 B = 12");
                    Console.WriteLine("2. Turn on LED");
                    Console.WriteLine("3. Turn off LED");
                    Console.WriteLine("4. Get temperature value from DS18B20 sensor");
                    Console.WriteLine("5. Exit");
                    Console.Write("Select a task number and press Enter: ");
                    var _strSelectTask = Console.ReadLine();
                    Console.WriteLine();
                    if (int.TryParse(_strSelectTask, out _intSelectTask))
                    {
                        switch (_intSelectTask)
                        {
                        case 1:
                            // remote call function on Arduino and wait for the result
                            var result = connection.Call("Sum", 10, 12);
                            // Display the result
                            Console.WriteLine("Status : " + result.Status);
                            Console.WriteLine("Type : " + result.Type);
                            Console.WriteLine("Value : " + result.Value);
                            // Status : OK
                            // Type : int
                            // Value : 22
                            Console.WriteLine();
                            break;

                        case 2:
                            connection.Call("setLed", true);
                            Console.WriteLine("LED ON");
                            Console.WriteLine();
                            break;

                        case 3:
                            connection.Call("setLed", false);
                            Console.WriteLine("LED OFF");
                            Console.WriteLine();
                            break;

                        case 4:
                            // remote call function on Arduino and wait for the result
                            var _objTemperature = connection.Call("getTemperature").Value.ToString();
                            // Type : float
                            float _floatTemperature = (float)Convert.ToDouble(_objTemperature);
                            // Display the result
                            Console.WriteLine("Value : " + _floatTemperature);
                            Console.WriteLine();
                            break;

                        case 5:
                            Console.WriteLine("Exit App");
                            Environment.Exit(0);
                            break;

                        default:
                        {
                            Console.WriteLine("Other number");
                            break;
                        }
                        }
                    }
                } while (_intSelectTask != 5);
                //
                if (connection != null)
                {
                    //Only required for Linux
                    Task.Delay(2000).Wait(); // Wait 1 seconds with blocking
                    connection.Disconnect();
                }
                return(0);
            }
            catch (Exception ex)
            {
                handleException(ex);
                return(-1);
            }
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            WriteHeader();

            SharerConnection connection;

LBL_START:

            try
            {
                // ask for COM port
                string port = null;
                while (port == null)
                {
                    var ports = SharerConnection.GetSerialPortNames();
                    port = DisplayOptions("On which COM port your Arduino is connected to ?", ports);
                }

                WriteHeader();

                // ask for baurate
                int bauds = 0;
                while (bauds == 0)
                {
                    bauds = DisplayOptions("Choose a baudrate.", new int[] { 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, 115200 });
                }

                connection = new SharerConnection(port, bauds);
                connection.Connect();
            }
            catch (Exception ex)
            {
                ShowException(ex);
                goto LBL_START;
            }

LBL_SHOW_OPTIONS:

            try
            {
                WriteHeader();

                // Build option list
                var options = new List <string>();
                options.Add(GET_INFO);
                options.AddRange(connection.Functions.Select((x) => $"{CALL}{x.Name}"));
                options.AddRange(connection.Variables.SelectMany((x) => new string[] { $"{READ}{x.Name}", $"{WRITE}{x.Name}" }));

                var answer = DisplayOptions("What would you like to do ?", options);
                Console.WriteLine();

                if (answer == null)
                {
                    goto LBL_SHOW_OPTIONS;
                }
                else if (answer.StartsWith(GET_INFO))
                {
                    Console.WriteLine(connection.GetInfos());                                   // print board info
                }
                else if (answer.StartsWith(CALL))
                {
                    // call a function
                    var name     = answer.Substring(CALL.Length);
                    var function = connection.Functions.FirstOrDefault((x) => string.Equals(x.Name, name));
                    if (function == null)
                    {
                        throw new Exception($"No function called {name}");
                    }

                    Console.WriteLine($"Call function: {function.Prototype}");

                    var argValues = new List <string>();
                    foreach (var arg in function.Arguments)
                    {
                        Console.Write($"  {arg.Name}= ");
                        argValues.Add(Console.ReadLine());
                    }

                    var result = connection.Call(name, argValues.ToArray());

                    Console.Write("--> ");
                    Console.WriteLine(result);
                }
                else if (answer.StartsWith(READ))
                {
                    // read variable
                    var name     = answer.Substring(READ.Length);
                    var variable = connection.Variables.FirstOrDefault((x) => string.Equals(x.Name, name));
                    if (variable == null)
                    {
                        throw new Exception($"No variable called {name}");
                    }

                    var value = connection.ReadVariable(name);
                    Console.WriteLine($"  {variable.Type.ToString()} {name} = {value?.ToString()}");
                }
                else if (answer.StartsWith(WRITE))
                {
                    // write variable
                    var name = answer.Substring(WRITE.Length);
                    Console.Write($"Enter a new value for {name}: ");
                    var value = Console.ReadLine();

                    if (connection.WriteVariable(name, value))
                    {
                        Console.WriteLine("OK");
                    }
                    else
                    {
                        Console.WriteLine("Failed to write variable");
                    }
                }
                else
                {
                    Console.Write("Answer not understood !?");
                }
            }
            catch (Exception ex) { ShowException(ex); }

            Console.WriteLine("Press any key to continue testing Sharer...");
            Console.Read();
            goto LBL_SHOW_OPTIONS;
        }