this.Invoke((EventHandler)(delegate { txtData.Text = data; }));// 更新界面} //string output TheString.Clear(); this.Invoke((EventHandler)(delegate { TheString.Append(Encoding.ASCII.GetString(buff)); this.txtGet.AppendText(TheString.ToString()); }));// 将读到的字符串显示到发送数据文本框中} finally { Listening = false;// 设置标志,通知 ui 可以关闭串口}} private void open_Click(object sender, EventArgs e)// 通过按键来初始串口{ if (sp.IsOpen) { Closing = true; while (Listening) Application.DoEvents(); sp.Close(); Closing = false; } else { try { sp.PortName = Setting.get_port; sp.BaudRate = int.Parse(Setting.get_baudrate); sp.Open(); } catch (Exception ex) { sp= new SerialPort(); MessageBox.Show(" 请先单击\" 设置\" 设置串口参数"," 警告",MessageBoxButtons.OK); }} btn_Open.Text = sp.IsOpen ?" 关闭连接":" 打开连接";// 通过 3 目运算控制按键显示 btn_Send.Enabled = sp.IsOpen;