private void Calibrate()
 {
     try
     {
         connection.WriteVariable("Cal_Weight", SharedVariables.LoadFac);
         connection.Call("Calibrate");
         SharedVariables.CalWinCal = false;
     }
     catch (Exception i)
     {
         MessageBox.Show("Could not Write" + i);
     }
 }
Ejemplo n.º 2
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.º 3
0
 public SharerFunctionReturn <bool> BlockingMove(TimeSpan timeout, byte m1, long r1, byte m2, long r2)
 {
     AssertConnectedAndConfigured();
     return(_connection.Call <bool>("blockingMove", timeout, m1, r1, m2, r2));
 }
Ejemplo n.º 4
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!connection.Connected)
            {
                connection.Connect();
                // Scan all functions shared
                connection.RefreshFunctions();
                connection.RefreshVariables();
            }

            if (isTimerRunning)
            {
                rfidValue            = connection.Call("returnRfid").Value.ToString(); //ok
                txtAttendeeRFID.Text = rfidValue.ToString().PadLeft(10, '0');          //ok

                var attendees = GetAttendees().Where(a => a.AttendeeRFID.ToString() == rfidValue &&
                                                     a.AttendeeRFID.ToString() != "0" &&
                                                     a.AttendeeRFID.ToString() != string.Empty);
                var isRfidRegistered = attendees.Any();

                if (!isRfidRegistered && txtAttendeeRFID.Text != "0000000000" && txtAttendeeRFID.Text != "")
                {
                    timer1.Stop();
                    var addAttendeeForm = new AddAttendeeForm(AttendeeRepository);
                    addAttendeeForm.FormClosed     += AddAttendeeForm_FormClosed;
                    addAttendeeForm.txtRFID.Enabled = false;
                    addAttendeeForm.txtRFID.Text    = rfidValue;
                    addAttendeeForm.ShowDialog();
                }
                else if (isRfidRegistered)
                {
                    txtName.Text    = attendees.SingleOrDefault().Name;
                    txtAge.Text     = attendees.SingleOrDefault().Age.ToString();
                    txtAddress.Text = attendees.SingleOrDefault().Address;

                    selectedAttendeeId = attendees.SingleOrDefault().Id;
                }

                //txtName.Enabled = !isRfidRegistered;
                //txtAge.Enabled = !isRfidRegistered;
                //txtAddress.Enabled = !isRfidRegistered;
            }


            if (txtAttendeeRFID.Text != "0000000000" && txtAttendeeRFID.Text != "") //ok
            {
                if (isTimerRunning)
                {
                    connection.WriteVariable("i", 2);
                    //connection.Call("modifyI", 2);
                }

                isTimerRunning = false; //ok

                proximityValue = connection.Call("returnProximity").Value.ToString();

                if (proximityValue == "0")
                {
                    bodytempValue = connection.Call("returnTemperature").Value.ToString(); //ok
                    //bodytempValue = connection.ReadVariable("bodytemp").Value.ToString(); //ok
                    txtTemperature.Text = bodytempValue;
                }
                else
                {
                    txtTemperature.Text = string.Empty;
                }
            }

            //try
            //{
            //    if (isTimerRunning)
            //    {

            //    }

            //    if (value.ToString() != "0" || value.ToString() != "")
            //    {
            //        isTimerRunning = false;



            //        var runFunctionChangeVariableIToNum = connection.WriteVariable("i", 2);

            //        connection.WriteVariable("rfid", 0);
            //    }

            //}
            //catch(Exception ex)
            //{

            //}
        }
Ejemplo n.º 5
0
        private void TimerTick()
        {
            try
            {
                if (!connection.Connected)
                {
                    connection.Connect();
                    // Scan all functions shared
                    connection.RefreshFunctions();
                    connection.RefreshVariables();
                }

                if (isTimerRunning)
                {
                    rfidValue            = connection.Call("returnRfid").Value.ToString(); //ok
                    txtAttendeeRFID.Text = rfidValue.ToString().PadLeft(10, '0');          //ok

                    var attendees = GetAttendees().Where(a => a.AttendeeRFID.ToString() == rfidValue &&
                                                         a.AttendeeRFID.ToString() != "0" &&
                                                         a.AttendeeRFID.ToString() != string.Empty);
                    var isRfidRegistered = attendees.Any();

                    if (!isRfidRegistered && txtAttendeeRFID.Text != "0000000000" && txtAttendeeRFID.Text != "")
                    {
                        //connectionTimer.Stop();
                        //var addAttendeeForm = new AddAttendeeForm(AttendeeRepository);
                        //addAttendeeForm.FormClosed += AddAttendeeForm_FormClosed;
                        //addAttendeeForm.txtRFID.Enabled = false;
                        //addAttendeeForm.txtRFID.Text = rfidValue;
                        //addAttendeeForm.ShowDialog();
                        btnAdd.Text = "RFID Not Registered";
                        connection.WriteVariable("i", 5);
                        Thread.Sleep(2000);
                        AutomaticReset();
                    }
                    else if (isRfidRegistered)
                    {
                        txtName.Text    = attendees.SingleOrDefault().Name;
                        txtAge.Text     = attendees.SingleOrDefault().Age.ToString();
                        txtAddress.Text = attendees.SingleOrDefault().Address;

                        selectedAttendeeId     = attendees.SingleOrDefault().Id;
                        selectedAttendeeStatus = attendees.SingleOrDefault().Status;

                        if (selectedAttendeeStatus == "POSITIVE")
                        {
                            btnAdd.Text = "POSITIVE";
                            connection.WriteVariable("i", 4);
                            Thread.Sleep(2000);
                            AutomaticReset();
                        }
                        else if (selectedAttendeeStatus == "PUI")
                        {
                            btnAdd.Text = "PUI";
                            connection.WriteVariable("i", 3);
                            Thread.Sleep(2000);
                            AutomaticReset();
                        }
                    }

                    //txtName.Enabled = !isRfidRegistered;
                    //txtAge.Enabled = !isRfidRegistered;
                    //txtAddress.Enabled = !isRfidRegistered;
                }


                if (txtAttendeeRFID.Text != "0000000000" && txtAttendeeRFID.Text != "") //ok
                {
                    if (isTimerRunning)
                    {
                        connection.WriteVariable("i", 2);
                        //connection.Call("modifyI", 2);
                    }

                    isTimerRunning = false; //ok

                    proximityValue = connection.Call("returnProximity").Value.ToString();

                    if (proximityValue == "0")
                    {
                        bodytempValue = connection.Call("returnTemperature").Value.ToString(); //ok

                        txtTemperature.Text = bodytempValue;

                        btnAdd.PerformClick();
                    }
                    else
                    {
                        txtTemperature.Text = string.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
Ejemplo n.º 6
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;
        }
Ejemplo n.º 7
0
 private void trackBar1_Scroll(object sender, EventArgs e)
 {
     connection.Call("analogWrite", 3, trackBar1.Value / 4);
     valor.Text = ((trackBar1.Value / 4) * 100 / 255).ToString() + "%";
 }