.NET里有个线程安全的机制。
默认Control.CheckForIllegalCrossThreadCalls = true。你也可以直接关闭线程安全检查。
不过更好地方式是写一个委托,用Invoke函数来执行该委托。在之前例子的基础上,我是这么改的,你可以参考参考。但请你务必
要处理好IO异常。
        
// 委托
delegate void InvokReceive(string data);

程序代码:
void serialPort_DataReceived(object sender,  e)
{
    try
    {
         if (this.serialPort.IsOpen)
         {
             // 实例化委托对象
             InvokReceive ir = new InvokReceive(ReceiveString);
             // 构建传递的参数
             Object[] args = new Object[1];
             args[0] = this.serialPort.ReadLine();
             this.Invoke(ir, args);
          }
      catch (System.Exception ex)
      {
                // Eat exception
      }
}

程序代码:
        void ReceiveString(string str)
        {
            this.txtReceiveData.AppendText(str);
        }
注册该事件,不过如果在打开串口之后注册事件,应该在关闭串口后取消事件,不然会显示重复。

程序代码:
        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                this.serialPort.Open();
                if (this.serialPort.IsOpen)
                {
                    // 注册事件
                    this.serialPort.DataReceived +=new (serialPort_DataReceived);
                }
                this.btnOpen.Enabled = false;
                this.btnClose.Enabled = true;
                this.btnSend.Enabled = true;
                this.btnReceive.Enabled = true;
            }
            catch ( ex)
            {
                MessageBox.Show("打开串口失败!请检查是否已经正常连接!", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
你应该多多在网上找些资料~.NET已经帮我们做得很多了,不会难的。
[
 本帖最后由 zhoufeng1988 于 2011-8-12 14:45 编辑 ]