public bool SearchAndConnectToArduino()
        {
            for (var i = 0; i <= 20; i++)
            {
                _serialTransport = new SerialTransport
                {
                    CurrentSerialSettings = { PortName = "COM" + i.ToString(), BaudRate = 115200 }
                };
                _cmdMessenger = new CmdMessenger(_serialTransport)
                {
                    BoardType = BoardType.Bit16
                };
                AttachCommandCallBacks();
                _cmdMessenger.Connect();

                var commandCheck = new SendCommand((int)Command.pCheckArduino);
                _cmdMessenger.SendCommand(commandCheck);
                Thread.Sleep(100);//wait 100 ms for the response
                if (_arduinoFound)
                {
                    return(true);
                }
                //else
                _cmdMessenger.Disconnect();
                _cmdMessenger.Dispose();
                _serialTransport.Dispose();
            }
            return(false);
        }
예제 #2
0
 public void Dispose()
 {
     cmdMessenger.StopListening();
     cmdMessenger.Dispose();
     serialTransport.Dispose();
     cmdMessenger    = null;
     serialTransport = null;
 }
예제 #3
0
        // Exit function
        public void Exit()
        {
            // Stop listening
            _cmdMessenger.Disconnect();

            // Dispose Command Messenger
            _cmdMessenger.Dispose();

            // Dispose Serial Port object
            _serialTransport.Dispose();
        }
        public void Exit()
        {
            // Stop listening
            _cmdMessenger.StopListening();

            // Dispose Command Messenger
            _cmdMessenger.Dispose();

            // Dispose Serial Port object
            Arduino.Dispose();
        }
 public void Stop()
 {
     if (_cmdMessenger != null)
     {
         _cmdMessenger.Disconnect();
         _cmdMessenger.Dispose();
     }
     if (_serialTransport != null)
     {
         _serialTransport.Dispose();
     }
 }
예제 #6
0
 public void Exit()
 {
     try
     {
         _serialTransport.Disconnect();
         _cmdMessenger.Dispose();
         _serialTransport.Dispose();
     }
     catch
     {
     }
 }
예제 #7
0
 public void Connect()
 {
     connected = true;
     if (st != null)
     {
         st.Dispose();
     }
     st = new SerialTransport();
     st.CurrentSerialSettings.PortName  = foundComm;
     st.CurrentSerialSettings.BaudRate  = 9600;
     st.CurrentSerialSettings.DtrEnable = false;
     if (cmdMessenger != null)
     {
         cmdMessenger.Dispose();
     }
     cmdMessenger           = new CmdMessenger(st);
     cmdMessenger.BoardType = BoardType.Bit16;
     AttachCommandCallBacks();
     cmdMessenger.StartListening();
     measurementThread = new Thread(new ThreadStart(MeasurementThreadMethod));
     measurementThread.Start();
 }
예제 #8
0
        // Exit function
        public void Exit()
        {
            // Stop listening
            _cmdMessenger.Disconnect();

            // Dispose Command Messenger
            _cmdMessenger.Dispose();

            // Dispose Serial Port object
            _serialTransport.Dispose();

            // Pause before stop
            Console.WriteLine("Press any key to stop...");
            Console.ReadKey();
        }
예제 #9
0
 /// <summary>
 /// Connects to the provided communication port, attached necessary command callbacks and send an event for the connection status change
 /// </summary>
 /// <param name="foundComm">Communication Port to connect to</param>
 private void Connect(string foundComm)
 {
     connected = true;
     if (st != null)
     {
         st.Dispose();
     }
     st = new SerialTransport();
     st.CurrentSerialSettings.PortName  = foundComm;
     st.CurrentSerialSettings.BaudRate  = 9600;
     st.CurrentSerialSettings.DtrEnable = false;
     if (cmdMessenger != null)
     {
         cmdMessenger.Dispose();
     }
     cmdMessenger           = new CmdMessenger(st);
     cmdMessenger.BoardType = BoardType.Bit16;
     AttachCommandCallBacks();
     cmdMessenger.StartListening();
     if (ConnectionStatusChanged != null)
     {
         ConnectionStatusChanged(connected);
     }
 }
예제 #10
0
        // Exit function
        public void Exit()
        {
            // Stop listening
            _cmdMessenger.Disconnect();

            // Dispose Command Messenger
            _cmdMessenger.Dispose();

            // Dispose Serial Port object
            _serialTransport.Dispose();
            Properties.Settings.Default.initRed     = _controllerForm._RedValue;
            Properties.Settings.Default.initGreen   = _controllerForm._GreenValue;
            Properties.Settings.Default.initBlue    = _controllerForm._BlueValue;
            Properties.Settings.Default.initGlitter = _controllerForm.glitter;
            Properties.Settings.Default.Save();
        }
예제 #11
0
        // Exit function
        public void Exit()
        {
            if (_cmdMessenger != null)
            {
                // Stop listening
                _cmdMessenger.Disconnect();

                // Dispose Command Messenger
                _cmdMessenger.Dispose();
            }
            // Dispose Serial Port object
            // Dispose Serial Port object
            if (_serialTransport != null)
            {
                _serialTransport.Dispose();
            }
        }
예제 #12
0
        private void Update()
        {
            bool   success = false;
            string port    = null;
            int    attempt = 0;

            while (true)
            {
                attempt++;
                Thread.Sleep(updateTimeout);
                System.Console.WriteLine($"[{DateTime.Now.ToString("HH':'mm':'ss'.'fffffff")}] [UPDATER] !! attempt {attempt}...");
                try
                {
                    if (transport == null || messenger == null || !transport.IsConnected())
                    {
                        //messenger?.Disconnect();
                        messenger?.Dispose();
                        messenger = null;
                        //transport?.Disconnect();
                        transport?.Dispose();
                        transport = null;

                        port      = AutodetectArduinoPort();
                        transport = GetTransport(port);
                        messenger = GetMessenger(transport);
                    }
                    if (transport.IsConnected())
                    {
                        success = true;
                    }
                }
                catch (Exception e)
                {
                    System.Console.WriteLine($"[{DateTime.Now.ToString("HH':'mm':'ss'.'fffffff")}] [UPDATER] !! {e}");
                    success = false;
                }
                if (success)
                {
                    System.Console.WriteLine($"[{DateTime.Now.ToString("HH':'mm':'ss'.'fffffff")}] [UPDATER] !! connected at port {port} at {attempt} attempt");
                    break;
                }
            }
        }
예제 #13
0
        private static GaugeDevice DetectArduinoGaugeDevice(string portname)
        {
            SerialTransport serialTransport = new SerialTransport()
            {
                CurrentSerialSettings = { PortName = portname, BaudRate = BAUD_RATE, DtrEnable = false }
            };
            CmdMessenger cmdMessenger = new CmdMessenger(serialTransport)
            {
                BoardType = BoardType.Bit16
            };

            try
            {
                cmdMessenger.StartListening();

                var command = new SendCommand((int)Command.HandshakeRequest, (int)Command.HandshakeResponse, 1000);
                var handshakeResultCommand = cmdMessenger.SendCommand(command);

                if (handshakeResultCommand.Ok)
                {
                    // Read response
                    var software     = handshakeResultCommand.ReadStringArg();
                    var serialNumber = handshakeResultCommand.ReadStringArg();

                    if (software.Contains("ArduinoGauge"))
                    {
                        // Create ArduinoGaugeDevice
                        GaugeDevice device = new GaugeDevice()
                        {
                            PortName     = portname,
                            SerialNumber = serialNumber
                        };

                        return(device);
                    }
                    else
                    {
                        Debug.WriteLine("Connected to Arduino, but not an ArduinoGauge device.");
                        return(null);
                    }
                }
                else
                {
                    Debug.WriteLine("Handshake FAILED");
                    return(null);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return(null);
            }
            finally
            {
                cmdMessenger.StopListening();
                cmdMessenger.Dispose();
                cmdMessenger = null;
                serialTransport.Dispose();
                serialTransport = null;
            }
        }