public void Connect(string mac) { LogMessage($"Connecting to {mac}"); Mac = mac; if (Connection == ConnectionStatus.Connected || Connection == ConnectionStatus.Online) { throw new InvalidOperationException("Already connected to an interface"); } TxCommunication?.Dispose(); TxCommunication = new TxCommunication(SerialAdapter); try { TxCommunication.OpenConnection(mac); Connection = TxCommunication.Connected ? ConnectionStatus.Connected : ConnectionStatus.NotConnected; } catch (Exception e) { LogMessage($"Exception while connecting: {e.Message}"); Connection = ConnectionStatus.Invalid; HandleException(e); return; } _masterInterface.ResetValues(); LogMessage("Connected"); _connected?.Invoke(this, new EventArgs()); }
public void Disconnect() { LogMessage("Disconnecting"); ThrowWhenNotConnected(); try { TxCommunication.CloseConnection(); Connection = ConnectionStatus.NotConnected; } catch (Exception e) { LogMessage($"Exception while disconnecting: {e.Message}"); Connection = ConnectionStatus.Invalid; HandleException(e); } TxCommunication.Dispose(); TxCommunication = null; _disconnected?.Invoke(this, new EventArgs()); LogMessage("Disconnected"); _masterInterface.ResetValues(); }
public void Dispose() { Connection = ConnectionStatus.NotConnected; if (TxCommunication != null) { TxCommunication.Dispose(); TxCommunication = null; } if (SerialAdapter != null) { SerialAdapter.Dispose(); } _masterInterface.ResetValues(); }