コード例 #1
0
ファイル: ArduinoController.cs プロジェクト: Onkeliroh/DSA
        /// <summary>
        /// Sets up a conntection to a specified serial port.
        /// </summary>
        /// <param name="Dtr">If <c>true</c> use dtr for transmition.</param>
        public static bool Setup(bool Dtr = false)
        {
            if (SerialPortName != null)
            {
                if (_cmdMessenger != null)
                {
                    _cmdMessenger.Disconnect();
                    _cmdMessenger.Dispose();
                }
                if (ConnectionWatchdog != null)
                {
                    ConnectionWatchdog.Dispose();
                }

                _cmdMessenger = new CmdMessenger(new SerialTransport()
                {
                    CurrentSerialSettings =
                    {
                        PortName  = SerialPortName,
                        BaudRate  =         115200,
                        DataBits  =              8,
                        Parity    = Parity.None,
                        DtrEnable = Dtr
                    }
                }, 512, BoardType.Bit32);

                // Attach the callbacks to the Command Messenger
                AttachCommandCallBacks();

                // Attach to NewLinesReceived for logging purposes
                _cmdMessenger.NewLineReceived += NewLineReceived;

                // Attach to NewLineSent for logging purposes
                _cmdMessenger.NewLineSent += NewLineSent;

                // Start listening
                IsConnected = _cmdMessenger.Connect();
                if (!IsConnected)
                {
                    SerialPortName = string.Empty;
                }
                else
                {
                    ConnectionWatchdog = new System.Threading.Timer(new System.Threading.TimerCallback(ConnectionWatchdogCallback), null, 0, 1000);
                }
                return(IsConnected);
            }
            return(false);
        }
コード例 #2
0
 // Exit function
 public void Exit()
 {
     _connectionManager.Dispose();
     _cmdMessenger.Disconnect();
     _cmdMessenger.Dispose();
     _transport.Dispose();
 }
コード例 #3
0
        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);
        }
コード例 #4
0
 public void Dispose()
 {
     cmdMessenger.StopListening();
     cmdMessenger.Dispose();
     serialTransport.Dispose();
     cmdMessenger    = null;
     serialTransport = null;
 }
コード例 #5
0
        public void Stop()
        {
            if (m_CmdMessenger != null)
            {
                m_CmdMessenger.Dispose();
            }

            Log.Information("MQTT Broker Stopped");
        }
コード例 #6
0
ファイル: ArduinoController.cs プロジェクト: solosonic/T-Bone
        // Exit function
        public void Exit()
        {
            // Stop listening
            _cmdMessenger.StopListening();

            // Dispose Command Messenger
            _cmdMessenger.Dispose();

            // Dispose Serial Port object
            _serialPortManager.Dispose();
        }
コード例 #7
0
        // Exit function
        public void Exit()
        {
            // Stop listening
            _cmdMessenger.Disconnect();

            // Dispose Command Messenger
            _cmdMessenger.Dispose();

            // Dispose Serial Port object
            _serialTransport.Dispose();
        }
コード例 #8
0
 public void Stop()
 {
     if (_cmdMessenger != null)
     {
         _cmdMessenger.Disconnect();
         _cmdMessenger.Dispose();
     }
     if (_serialTransport != null)
     {
         _serialTransport.Dispose();
     }
 }
コード例 #9
0
 public void Exit()
 {
     try
     {
         _serialTransport.Disconnect();
         _cmdMessenger.Dispose();
         _serialTransport.Dispose();
     }
     catch
     {
     }
 }
コード例 #10
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();
 }
コード例 #11
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();
        }
コード例 #12
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);
     }
 }
コード例 #13
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();
        }
コード例 #14
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();
            }
        }
コード例 #15
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;
                }
            }
        }
コード例 #16
0
        // Exit function
        public void Exit()
        {
            // Disconnect ConnectionManager
            _connectionManager.Progress          -= LogProgress;
            _connectionManager.ConnectionTimeout -= ConnectionTimeout;
            _connectionManager.ConnectionFound   -= ConnectionFound;

            // Dispose ConnectionManager
            _connectionManager.Dispose();

            // Disconnect Command Messenger
            _cmdMessenger.Disconnect();

            // Dispose Command Messenger
            _cmdMessenger.Dispose();

            // Dispose transport layer
            _transport.Dispose();
        }
コード例 #17
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;
            }
        }
コード例 #18
0
 public void Dispose()
 {
     _messenger.Disconnect();
     _messenger.Dispose();
     _transport.Dispose();
 }
コード例 #19
0
 private void Close()
 {
     cmdMessenger.Disconnect();
     cmdMessenger.Dispose();
     cmdMessenger = null;
 }
コード例 #20
0
 public static void Disconnect()
 {
     LogCommands(false);
     CmdMessenger.Disconnect();
     CmdMessenger.Dispose();
 }
コード例 #21
0
 protected void Dispose()
 {
     _cmdMessenger.Dispose();
     _bluetoothTransport.Dispose();
 }