使用C ++的简单串行接收解析器

时间:2013-02-06 01:13:39

标签: c++ delegates serial-port

我是C ++和.NET的新手。我正在使用Visual Studio 2008并创建了一个Visual C ++ CLR Windows窗体应用程序。

我在表单上的工具箱中放置了一个文本框和一个serialport。经过大量的网络搜索,我能够添加适当的代码来创建一个接收事件处理程序,并成功地从COM端口接收字符。

我遇到的问题是将收到的已解析文本放入表单的文本框中。从我的研究来看,问题是多线程之一。我查看了使用Delegates和Invokes等的示例,但还未能使其工作。

我的代码看起来像

private: void SetText(String ^text) { 
    // InvokeRequired required compares the thread ID of the 
    // calling thread to the thread ID of the creating thread. 
    // If these threads are different, it returns true. 
    if (this->InvokeRequired)   
    { 
        AddCommandToTextBox ^d = gcnew AddCommandToTextBox(text); 
        this->Invoke(d, gcnew object[] { text }); 
    } 
    else 
    { 
        this->textBox1->Text = this->textBox1->Text + text; 
    } 
}

0 个答案:

没有答案