private Boolean TryConnect(String ComPort) { ConnectError = null; try { Disconnect(); btnConnect.Text = "Connecting..."; if (!String.IsNullOrEmpty(ComPort)) { txtSerialLog.Clear(); Newport = new NewportMeter <WindowsSerialPort>(ComPort); Newport.DataReceived += Newport_DataReceived; Newport.DataSent += Newport_DataSent; cmbSerialPort.Enabled = false; } GetDeviceInfo(); if (Newport.Make.Equals("NEWPORT")) { btnConnect.Text = "Disconnect"; SetConnected(true); return(true); } } catch (Exception e) { ConnectError = e.Message; } Disconnect(); return(false); }
private void Disconnect() { if (Newport != null) { try { Newport.DataReceived -= Newport_DataReceived; Newport.DataSent -= Newport_DataSent; } finally { Newport.Close(); Newport.Dispose(); Newport = null; } } SetConnected(false); ClearDeviceInfo(); cmbSerialPort.Enabled = true; btnConnect.Text = "Connect"; }