似乎无法在这里找到我一生的答案。无论如何,事件处理程序如何更改 Form 的 textBox.text?private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { SerialPort sp = (SerialPort)sender; string indata = sp.ReadExisting(); //Do what ever you want with the data textBox2.Text = "test"; //alas, this doesn't work }按下按钮时创建事件处理程序private void button3_Click(object sender, EventArgs e) { serialPort1.PortName = comboBox1.Text; serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text); serialPort1.RtsEnable = true; serialPort1.DtrEnable = true; serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); serialPort1.Open(); } }任何帮助是极大的赞赏
2 回答
SMILET
TA贡献1796条经验 获得超4个赞
这DataReceived是在它自己的线程上,您需要调用 GUI 才能更新文本框。
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string Data = serialPort1.ReadExisting();
this.Invoke((MethodInvoker)delegate
{
textBox2.AppendText(Data);
});
}
- 2 回答
- 0 关注
- 88 浏览
添加回答
举报
0/150
提交
取消