private void btnTest_Click(object sender, EventArgs e) { if (backgroundWorker.IsBusy) { btnTest.Enabled = false; lbResult.Text = "Aborting"; backgroundWorker.CancelAsync( ); //hack - must force the serial port to stop from this thread - hmmm _testCommsPort.EnsureClosed( ); } else { var comPort = ((ComboBinder <string>)cobCOMPorts.SelectedItem).Value; if (comPort.Equals(ComboBinder <string> .None.Value)) { return; } //if we are testing the currently selected port then close it _commsPortWasOpen = comPort.Equals(Settings.Default.COMPort); if (_commsPortWasOpen) { _commsPort.EnsureClosed( ); } btnTest.Enabled = true; btnTest.Text = "Abort"; lbResult.ForeColor = Color.Black; lbResult.Text = "Searching for NXTCam..."; lbResult.Refresh( ); _hasDoneTest = true; //setup a new port to test var baud = ((ComboBinder <Int32>)cobBaudRate.SelectedItem).Value; var parity = (Parity)((ComboBinder <Int32>)cobParity.SelectedItem).Value; var dataBits = ((ComboBinder <Int32>)cobDataBits.SelectedItem).Value; var stopBits = (StopBits)((ComboBinder <Int32>)cobStopBits.SelectedItem).Value; var handshake = (Handshake)((ComboBinder <Int32>)cobHandshake.SelectedItem).Value; _testCommsPort = _commsPortFactory.Create(new CommsPortSettings(comPort, baud, parity, dataBits, stopBits, handshake)); backgroundWorker.RunWorkerAsync(); } }
private void disconnect() { //this is not so pretty, but we want to clear away any junk //that would cause a connect to fail later do { _commsPort.ReadExisting(); Thread.Sleep(100); } while (_commsPort.BytesToRead > 0); _appState.State = State.NotConnected; _commsPort.EnsureClosed(); OnCompeted(); }
protected void OnDisconnectActionActivated(object sender, EventArgs e) { // TODO: This code is duplicated from WinForms.StripCommands.DisconnectStripCommand // TODO: Check for background tasks that are using the serial port //this is not so pretty, but we want to clear away any junk //that would cause a connect to fail later while (commsPort.BytesToRead > 0) { commsPort.ReadExisting(); Thread.Sleep(100); } appState.State = NXTCamView.Core.State.NotConnected; commsPort.EnsureClosed(); }
private void mainFormFormClosed(object sender, FormClosedEventArgs e) { _commsPort.EnsureClosed(); Settings.Default.Save(); }