private void btnOpen_Click(object sender, EventArgs e) { if (btnOpen.Text == Resources.OpenString) { if (NewPortInstance()) { try { portOperatorBase.Open(); btnOpen.Text = Resources.CloseString; EnableControl(false); } catch { } } } else { try { portOperatorBase.Close(); } catch { } btnOpen.Text = Resources.OpenString; EnableControl(true); } }
public override bool Close() { try { _operator.Close(); } catch {} return(true); }
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { if (t != null && !t.IsCompleted) { MessageBox.Show(Resources.LoadingInstrumentResource); e.Cancel = true; return; } cts.Cancel(); try { portOperatorBase?.Close(); } catch { } }
private void openSPbtn_Click(object sender, EventArgs e) { if (openSPbtn.Text == "Open") { bool hasAddress = false; bool hasException = false; string str = devicesCB.SelectedItem.ToString(); Console.WriteLine("address:" + str); if (!str.Contains("ASRL")) { printInfo("Select port ERROR!", Color.Red); return; } try { serialopBase = new RS232PortOperator(str, (int)baudRateCB.SelectedItem, 0, 0, 8); hasAddress = true; } catch (Exception ex) { MessageBox.Show("error1:" + ex.ToString()); hasException = true; } if (!hasException) { hasException = false; serialopBase.Timeout = 5000; try { serialopBase.Open(); openSPbtn.Text = "Close"; //EnableControl(false); } catch (Exception ex) { MessageBox.Show("error2:" + ex.ToString()); hasException = true; } } if (!hasException) { printInfo("Open Serial port successful!", Color.Green); } else { printInfo("Open Serial port fail!", Color.Red); } } else { bool hasException = false; try { serialopBase.Close(); } catch { hasException = true; } openSPbtn.Text = "Open"; //EnableControl(true); if (!hasException) { printInfo("Close Serial port successful!", Color.Green); } else { printInfo("Close Serial port fail!", Color.Red); } } }