C#中自动读取串口

时间:2017-05-12 13:46:57

标签: c#

以下是自动读取串口的非常简单的代码: -

public partial class MainForm : Form
    {
        public MainForm()
        {
        //
        // The InitializeComponent() call is required for Windows Forms designer support.
        //
        InitializeComponent();

        //
        // TODO: Add constructor code after the InitializeComponent() call.
        //


        serialPort1.PortName="COM11";
        serialPort1.Open();

    }
    void SerialPort1DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        rtBox.Text="Data Received\n";
    }
    void BtnReadClick(object sender, EventArgs e)
    {
        rtBox.Text += "Read click" + "\n";
    }

System.InvalidOperationException:跨执行绪作业无效:存取控制项'rtBox'时所使用的执行绪与建立控制项的执行绪不同。

Google中的英文翻译来自中文字符 “不完整的线程作业:用于访问控件'rtBox'的线程与建立控件的线程不同。”

我只想在Rich测试框中显示“数据接收”消息。但是,每当收到数据时,都会出现以下异常:

你知道为什么吗?感谢

0 个答案:

没有答案