public Task Handle(ConnectionUpdatedEvent notification, CancellationToken cancellationToken) { _connectionRepository.ClearCache(); _routeRepository.ClearCache(); return(Task.CompletedTask); }
// connects to a specific COM port public void Connect(byte portNumber, bool rtsDtrEnable, int attempts) { if (attempts > 0) { Disconnect(); port.PortNumber = portNumber; if (rtsDtrEnable) { port.DtrControl = DTR_CONTROL.ENABLE; port.RtsControl = RTS_CONTROL.ENABLE; //port.DtrControl = true; //port.RtsControl = true; } else { port.DtrControl = DTR_CONTROL.DISABLE; port.RtsControl = RTS_CONTROL.DISABLE; //port.DtrControl = false; //port.RtsControl = false; } port.Open(); while (port.IsOpen == false) { } // wait for port to open Thread.Sleep(300); // give time to Arduinos that reset upon port opening port.Flush(); if (identify()) { queryCapabilities(); queryErrorMessages(); activePortName = port.PortName; ConnectionUpdatedEvent?.Invoke(); // raise connection updated event dataToWrite.Clear(); comLoop.RunWorkerAsync(); } else { port.Close(); while (port.IsOpen) { } // wait for port to close Connect(portNumber, rtsDtrEnable, attempts - 1); // recursively call this function with one less attempt to try } } else { throw new System.IO.IOException("Wrong device"); } }
// connects to a specific COM port public /*async*/ void Connect(byte portNumber, bool rtsDtrEnable) { if (port.IsOpen) // terminates any previous connection { Disconnect(); } port.PortNumber = portNumber; if (rtsDtrEnable) { port.DtrControl = DTR_CONTROL.ENABLE; port.RtsControl = RTS_CONTROL.ENABLE; } else { port.DtrControl = DTR_CONTROL.DISABLE; port.RtsControl = RTS_CONTROL.DISABLE; } port.Open(); while (port.IsOpen == false) { } // wait for port to open Thread.Sleep(300); // give time to Arduinos that reset upon port opening port.ReadExisting(); bool identified = false; for (int i = 0; i < 3; i++) { if (identify()) { identified = true; break; } } if (identified) // three attempts { activePortName = port.PortName; ConnectionUpdatedEvent?.Invoke(); // raise connection updated event this.comLoop.RunWorkerAsync(); } else { port.Close(); while (port.IsOpen) { } // wait for port to close throw new System.IO.IOException("Wrong device"); } }
// disconnect from a COM port public void Disconnect() { if (port.IsOpen) { try { comLoop.CancelAsync(); // stops monitoring for new data port.Close(); while (port.IsOpen) { } // wait for port to close } catch (System.IO.IOException ex) { System.Windows.MessageBox.Show(ex.Message, "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } } // resets all read data current = 0; voltage = 0; temperature = 0; status = 0; UserPins = 0; errorFlags = 0; seriesResistance = 0; // resets all port and device information activePortName = null; firmwareVersion = null; boardRevision = null; DeviceIdentification = string.Empty; maxIdac = 0; maxIadc = 0; maxVdac = 0; maxVadc = 0; maxPower = 0; dvmInputResistance = 0; temperatureThreshold = 0; errorMessages = null; dataToWrite.Clear(); // raise connection updated event ConnectionUpdatedEvent?.Invoke(); // last data update (with reset data) DataUpdatedEvent?.Invoke(); // event for data update complete }