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); }
public void Dispose() { cmdMessenger.StopListening(); cmdMessenger.Dispose(); serialTransport.Dispose(); cmdMessenger = null; serialTransport = null; }
// 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(); } }
public void Exit() { try { _serialTransport.Disconnect(); _cmdMessenger.Dispose(); _serialTransport.Dispose(); } catch { } }
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(); }
// 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(); }
/// <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); } }
// 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(); }
// 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(); } }
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; } } }
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; } }