private void btnCloseSeri_Click(object sender, EventArgs e) { if (this.btnCloseSeri.Text.Trim() == "打开串口") { if (Public.openCom(serialPort1)) { this.btnCloseSeri.Text = "关闭串口"; return; } else { MessageBox.Show("打开串口失败"); this.btnCloseSeri.Text = "打开串口"; return; } } if (this.btnCloseSeri.Text == "关闭串口") { if (this.serialPort1.IsOpen) { this.serialPort1.Close(); } this.btnCloseSeri.Text = "打开串口"; } }
private bool opencomm() { bool ret = false; try { if (!Public.openCom(serialPort1)) { MessageBox.Show("打开串口失败!"); this.btnCloseSeri.Text = "打开串口"; return(false); } else { this.btnCloseSeri.Text = "关闭串口"; return(true); } } catch (Exception ex) { this.btnCloseSeri.Text = "打开串口"; MessageBox.Show("打开串口失败!" + ex.Message); return(false); } return(ret); }
private void checkBox2_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked) { if (!s.IsOpen) { if (!Public.openCom(s)) { MessageBox.Show("打开失败!"); return; } else { MessageBox.Show("打开成功!"); } } } else { if (!checkBox1.Checked) { if (s.IsOpen) { s.Close(); } } } }